Chapter 5 Introduction to Defining Classes

Slides:



Advertisements
Similar presentations
Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view class.
Advertisements

Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 10 Introduction to Arrays
Chapter 6 Introduction to Defining Classes
Road Map Introduction to object oriented programming. Classes
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
VBA Modules, Functions, Variables, and Constants
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Chapter 10 Classes Continued
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Chapter 8 More Object Concepts
Writing Classes (Chapter 4)
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Creating a Java Application and Applet
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Classes, Interfaces and Packages
1 Guide gus; Creates a “mailbox” to hold the address of a Guide object. null gus.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Object-Oriented Programming: Classes and Objects.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Java Programming: Guided Learning with Early Objects
Java Primer 1: Types, Classes and Operators
Intro To Classes Review
Chapter 3: Using Methods, Classes, and Objects
Can perform actions and provide communication
Chapter 3 Introduction to Classes, Objects Methods and Strings
Can perform actions and provide communication
Corresponds with Chapter 7
Defining Classes and Methods
Lesson 5: Introduction to Defining Classes
Lecture 22 Inheritance Richard Gesick.
Can perform actions and provide communication
Defining Classes and Methods
Object-Oriented Programming
Object Oriented Programming in java
Defining Classes and Methods
Defining Classes and Methods
Chapter 7 Objects and Classes
Presentation transcript:

Chapter 5 Introduction to Defining Classes Fundamentals of Java

Objectives Design and implement a simple class from user requirements. Organize a program in terms of a view class and a model class. Use visibility modifiers to make methods visible to clients and restrict access to data within a class. Fundamentals of Java

Objectives (cont.) Write appropriate mutator methods, accessor methods, and constructors for a class. Understand how parameters transmit data to methods. Use instance variables, local variables, and parameters appropriately. Organize a complex task in terms of helper methods. Fundamentals of Java

Vocabulary Accessor Actual parameter Behavior Constructor Encapsulation Fundamentals of Java

Vocabulary (cont.) Formal parameter Helper method Identity Instantiation Lifetime Fundamentals of Java

Vocabulary (cont.) Mutator Scope State Visibility modifier Fundamentals of Java

Homework Read 5.1, 5.2 Complete Exercises 5.1, 5.2 Fundamentals of Java

Classwork (DO NOW) If you haven’t already finished the Student class, go to bpi.edu and copy it into yours. Write the following functions for the Student class: getTestAverage- has no parameters and returns the average of the 3 tests. getBestTest- has no parameters and returns the score of the highest test. Fundamentals of Java

Classwork (DO NOW) If you haven’t already finished the Student class, go to bpi.edu and copy it into yours. Add to the toString method so that it also includes the students test average and best test. Create a StudentTest class. Create a new Student using the default contructor. Set the name to “Student, Test”, and the test scores to 71, 83, and 91 Verify that your class works by calling and displaying the toString of this object Fundamentals of Java

The Internal Structure of Classes and Objects A class is a template that describes the characteristics of similar objects. Variable declarations define an object’s data. Instance variables Methods define an object’s behavior in response to messages. Fundamentals of Java

The Internal Structure of Classes and Objects (cont.) Encapsulation: Combining data and behavior into a single software package An object is an instance of its class. Instantiation: Process of creating a new object Fundamentals of Java

The Internal Structure of Classes and Objects (cont.) During execution, a computer’s memory holds: All class templates in their compiled form Variables that refer to objects Objects as needed Memory for data is allocated within objects. Objects appear and occupy memory when instantiated. Disappear when no longer needed Fundamentals of Java

The Internal Structure of Classes and Objects (cont.) Garbage collection: JVM’s automated method for removing unused objects Tracks whether objects are referenced by any variables Three characteristics of an object: Behavior (methods) State (data values) Identity (unique ID for each object) Fundamentals of Java

The Internal Structure of Classes and Objects (cont.) When messages sent, two objects involved: Client: The message sender Only needs to know the interface of the server Server: The message receiver Supports and implements an interface Information hiding: Server’s data requirements and method implementation hidden from client Fundamentals of Java

Table 5-1: Interface for the Student class A Student Class Table 5-1: Interface for the Student class Fundamentals of Java

A Student Class: Using Student Objects Declare and instantiate a Student object: Student s1 = new Student(); Sending messages to a Student object: String str = s1.getName(); s1.setName(“Bill”); System.out.println(s1.toString()); Fundamentals of Java

Homework Read 5.3, 5.4 Complete Exercises 5.3, 5.4 Fundamentals of Java

A Student Class: Objects, Assignment, and Aliasing Multiple variables can point at the same object Example: Student s1 = new Student(); Student s2 = s1; To cause a variable to no longer point at any object, set it equal to null,as in: s1 = null; Fundamentals of Java

A Student Class: Objects, Assignment, and Aliasing (cont.) Table 5-2: How variables are affected by assignment statements Fundamentals of Java

A Student Class: Objects, Assignment, and Aliasing (cont.) Table 5-2: How variables are affected by assignment statements (cont.) Fundamentals of Java

Figure 5-2: Difference between primitive and reference variables A Student Class (cont.) Two fundamental data type categories: Primitive types: int, double, boolean, char Shorter and longer versions of these types Reference types: All classes Figure 5-2: Difference between primitive and reference variables Fundamentals of Java

A Student Class (cont.) Figure 5-3: Student variable before and after it has been assigned the value null Fundamentals of Java

Homework Read 5.5, 5.6 Complete Exercises 5.5, 5.6 Fundamentals of Java

A Student Class (cont.) Can compare a reference variable to null Avoid null pointer exceptions Fundamentals of Java

A Student Class: The Structure of a Class Template Fundamentals of Java

A Student Class: The Structure of a Class Template (cont.) public: Class is accessible to anyone Name of class must follow Java naming conventions extends: Optional Java organizes class in a hierarchy. If Class B extends Class A, it inherits instance variables and methods from Class A. Fundamentals of Java

A Student Class: The Structure of a Class Template (cont.) Figure 5.4: Relationship between superclass and subclass Fundamentals of Java

A Student Class: The Structure of a Class Template (cont.) private and public are visibility modifiers. Define whether a method or instance variable can be seen outside of the class Instance variables should generally be private. Fundamentals of Java

A Student Class: Constructors (cont.) Initialize a newly instantiated object’s instance variables Activated (called) only by the keyword new Default constructors: Empty parameter lists A class is easier to use when it has a variety of constructors. Fundamentals of Java

A Student Class: Chaining Constructors (cont.) Fundamentals of Java

Editing, Compiling, and Testing the Student Class Steps: Save source code in Student.java. Run javac Student.java. Run/test the program. Fundamentals of Java

Editing, Compiling, and Testing the Student Class (cont.) Example 5.1: Tester program for the Student class Fundamentals of Java

Editing, Compiling, and Testing the Student Class (cont.) Introduce an error into the Student class: Figure 5-6: Divide by zero run-time error message Fundamentals of Java

The Structure and Behavior of Methods Methods take the following form: If the method returns no value, the return type should be void. Fundamentals of Java

Homework Ch 5 Written Questions Fundamentals of Java

The Structure and Behavior of Methods (cont.) return statements: If a method has a return type, implementation must have at least one return statement that returns a value of that type. A return statement in a void method simply ends the method. Can have multiple return statements Fundamentals of Java

The Structure and Behavior of Methods (cont.) Formal parameters: Parameters listed in a method’s definition Actual parameters (arguments): Values passed to a method when it is invoked Parameter passing example: Fundamentals of Java

The Structure and Behavior of Methods (cont.) Figure 5.8: Parameter passing Fundamentals of Java

The Structure and Behavior of Methods (cont.) Helper methods: Perform a piece of a task Used by another method to perform a larger task Usually private Only methods already defined within the class need to use them When an object is instantiated, it receives own copy of its class’s instance variables Fundamentals of Java

Scope and Lifetime of Variables Global variables: Declared inside a class but outside any method Accessible to any method in the class Local variables: Declared inside a method Accessible only within that method Fundamentals of Java

Scope and Lifetime of Variables (cont.) Scope (of a variable): Region where a variable can validly appear in lines of code Variables declared within any compound statement enclosed in braces have block scope. Visible only within code enclosed by braces Fundamentals of Java

Scope and Lifetime of Variables (cont.) Lifetime: Period when a variable can be used Local variables exist while the method executes. Instance variables exist while the object exists. Duplicate variable names may exist. Local variables in different scopes A local and a global variable Local overrides global Use this keyword to access global variable. Fundamentals of Java

Scope and Lifetime of Variables (cont.) Fundamentals of Java

Scope and Lifetime of Variables (cont.) Use instance variables to retain data. Using local variables will result in lost data. Use local variables for temporary storage. Using global variables could cause difficult-to-resolve logic errors. Use method parameters rather than global variables whenever possible. Fundamentals of Java

Graphics and GUIs: Images To load an image: ImageIcon image = new ImageIcon(fileName); fileName indicates the location and name of a file containing an image. To paint an ImageIcon from a panel class: anImageIcon.paintIcon(this, g, x, y); g is the graphics context. x and y are panel coordinates. Fundamentals of Java

Graphics and GUIs: A Circle Class Useful to represent shapes as objects A shape has attributes (color, size, position). Can create more shapes than the Graphics class can draw Given its graphics context, a shape can draw itself. Easier to manipulate Fundamentals of Java

Graphics and GUIs: A Circle Class (cont.) Table 5-4: Methods in class Circle Fundamentals of Java

Graphics and GUIs: A Circle Class (cont.) Table 5-4: Methods in class Circle (cont.) Fundamentals of Java

Graphics and GUIs: A Circle Class (cont.) Example 5.3: Displays a circle and a filled circle Fundamentals of Java

Graphics and GUIs: A Circle Class (cont.) Figure 5-10: Displaying two Circle objects Fundamentals of Java

Graphics and GUIs: A Circle Class (cont.) repaint method: Forces a refresh of any GUI component Invokes object’s paintComponent method Called automatically by the JVM whenever the component is moved or altered Can programatically call repaint() as well Fundamentals of Java

Graphics and GUIs: Mouse Events Program can detect and respond to mouse events by attaching listener objects to a panel When a particular type of mouse event occurs in a panel, its listeners are informed. Listener class for capturing mouse click events extends MouseAdapter Listener class for capturing mouse motion and dragging events extends MouseMotionAdapter Fundamentals of Java

Graphics and GUIs: Mouse Events (cont.) Table 5-5: Methods for responding to mouse events Fundamentals of Java

Graphics and GUIs: Mouse Events (cont.) Example 5.5: Displays a circle and a filled circle. Allows the user to drag a circle to another position Fundamentals of Java

Graphics and GUIs: Mouse Events (cont.) Example 5.5: Displays a circle and a filled circle. Allows the user to drag a circle to another position (cont.) Fundamentals of Java

Graphics and GUIs: Mouse Events (cont.) Example 5.5: Displays a circle and a filled circle. Allows the user to drag a circle to another position (cont.) Fundamentals of Java

Graphics and GUIs: Mouse Events (cont.) Example 5.5: Displays a circle and a filled circle. Allows the user to drag a circle to another position (cont.) Fundamentals of Java

Summary Java class definitions consist of instance variables, constructors, and methods. Constructors initialize an object’s instance variables when the object is created. A default constructor expects no parameters and sets the variables to default values. Mutator methods modify an object’s instance variables. Fundamentals of Java

Summary (cont.) Accessor methods allow clients to observe the values of these variables. The visibility modifier public makes methods visible to clients. private encapsulates access. Helper methods are called from other methods in a class definition. Usually declared to be private Fundamentals of Java

Summary (cont.) Instance variables track the state of an object. Local variables are used for temporary working storage within a method. Parameters transmit data to a method. A formal parameter appears in a method’s signature and is referenced in its code. Fundamentals of Java

Summary (cont.) Actual parameter is a value passed to a method when it is called. Scope of an instance variable is the entire class within which it is declared. Scope of a local variable or a parameter is the body of the method where it is declared. Fundamentals of Java

Summary (cont.) Lifetime of an instance variable is the same as the lifetime of a particular object. Lifetime of a local variable and a parameter is the time during which a particular call of a method is active. Fundamentals of Java