Fundamentals of Java Chapter 5: Introduction to Defining Classes UNIT 2—THE NEXT STEP WITH JAVA CHAPTER 5—INTRODUCTION TO DEFINING CLASSES EXERCISE 5.1 1. A class is a template that describes the variables and methods that define a set of objects. An object is an instance of a class. 2. An object's memory storage is returned to the computer by the garbage collector. 3. The three characteristics of an object are state, behavior, and identity. 4. Servers provide resources to clients. Clients obtain these services by instantiating a server and sending it messages. 5. A class’s interface is the set of methods it provides to clients. EXERCISE 5.2 1. A mutator is a method that modifies a variable in a class or object. An accessor is a method that returns a value without modifying a variable in a class or object. An example of an accessor is the method getName(), which returns a student's name. An example of a mutator is the method setScore(anInteger, anInteger), which sets the student’s score at the position specified by the first parameter to the new score specified by the second parameter. 2. The visibility modifier public is used primarily for methods and makes these visible to all clients of a class. The visibility modifier private is used primarily for instance variables and makes these visible only within the implementation of a class. 3. A constructor is run when an instance of a class is created. This operation typically initializes an object’s instance variables. 4. The toString method returns a string representation of an object, typically by concatenating the string representations of the values of its instance variables. This method is often used for debugging a class during development. 5. Two variables can refer to the same object after an assignment of that object to each variable. The object maintains its identity during assignment, which does not create a copy of the object. An example is the following: Student s1 = new Student(); Student s2 = s1; // s1 and s2 refer to the same object 6. A primitive type represents numeric values, such as integers. A reference type represents objects, such as strings. Variables of primitive types serve as names of the storage locations of primitive values, whereas variables of reference types name references or pointers to objects. 7. The null value is a special value that can be assigned to variables of any reference type. The null value indicates that a variable refers to no object. 1 Fundamentals of Java Chapter 5: Introduction to Defining Classes 8. A null pointer exception occurs when a client attempts to send a message to a variable that refers to no object and, hence, is null. This error commonly occurs when the programmer forgets to initialize a variable. 9. A default constructor expects no parameters. This constructor typically initializes instance variables to reasonable default values. 10. When no constructor is provided, Java sets all primitive variables to reasonable defaults, such as 0. Java sets all reference variables to null. 11. A constructor that expects another object of the same class transfers the data contained in that object to the new object being instantiated. It thus serves as a way of creating a copy of another object. EXERCISE 5.4 1. A formal parameter is a parameter’s name as it appears in a method header and in its body. An actual parameter is an expression that is passed to a method when the method is called. 2. The expressions that are the actual parameters are first evaluated. These values are then assigned to temporary storage locations, which are referenced by the corresponding formal parameters of the method. 3. int sum(int first, int second){ int result = 0; for (int i = first; i <= second; i++) result += i; return result; } 4. A local variable is a variable that is declared within the body of a method. Its purpose is to serve as temporary working storage for the operations in that method. EXERCISE 5.5 1. The lifetime of an instance variable is the lifetime of the object that owns it. The lifetime of a local variable is the activation of the method in which it is declared. The lifetime of a parameter is also the activation of the method in which it is declared. 2. Shadowing occurs when a local variable or parameter and a global variable have the same name. Shadowing can cause incorrect behavior if the programmer thinks that a variable reference in a method is to a common pool of variables, whereas it really is to a local variable or a parameter, which likely has a different value. An example is the method setName in the Student class. If the variable name is declared locally in the method, the method will not mutate the instance variable of the same name. 3. a. The instance variables are a and b. The parameters are x and y. The local variables are c and d. 2 Fundamentals of Java Chapter 5: Introduction to Defining Classes b. The scope of the instance variables is the entire class. The scope of the parameters and local variables is the body of the method aMutator. c. The lifetime of the instance variable is the lifetime of an instance of the class. The lifetime of the parameters and local variables is the period of time during which the method aMutator is executing. EXERCISE 5.6 1. The main method should not set the size of the frame, but should run the pack() method instead. After the main method loads the image from the file, use the methods getIconWidth() and getIconHeight() to obtain the width and height of the image. Then pass these values as parameters to the constructor for the panel. The panel’s constructor should use the width and height to set the panel’s preferred size. 2. public void scale(double factor){ radius = (int) (radius * factor); } 3. A listener object can be attached to a panel. When a mouse-pressed event occurs in the region of the panel, its listener is informed and the listener’s mousePressed method is triggered. This method can then perform operations in response to that event. REVIEW QUESTIONS Written Questions 1. A class is a template that provides declarations of the variables and implementations of the methods used by each instance of a class. Each instance of a class is an object that contains its own storage for the variables declared in its class and responds to messages by calling methods implemented in its class. 2. The visibility modifier public allows any client of a class to call the public method or access the public variable. The visibility modifier private restricts the call of the private method or the access to the private variable to the implementation area of the class. 3. An accessor method allows a client to observe but not modify data in an object. An accessor typically expects no parameters and returns a value. A mutator method allows a client to modify data in an object. A mutator typically expects at least one parameter (the data to be used in the modification) and may or may not return a value. 4. Class summary of the class BaseballPlayer: Class: BaseballPlayer Private Instance Variables: String name String team 3 Fundamentals of Java Chapter 5: Introduction to Defining Classes int homeRuns int battingAverage Public Methods: constructors void setName (theName) String getName() void setTeam (theTeam) String getTeam() void setHomeRuns(theHR) int getHomeRuns() void setBattingAverage(theAve) double getBattingAverage() String toString() 5. Before the method executes, the actual parameter is evaluated and its value is saved in a storage location named by the formal parameter. When the method executes, its code can access this value in the formal parameter. 6. A local variable is a variable declared within the body of a method. It is accessible within the method’s code and exists for the duration of the method’s execution. A local variable should be used when temporary working storage is needed within a method. 4