Chapter 2 Review Exercise Solutions
R2.1
An object contains state information. An object reference describes the location of an object.
R2.2
An object contains state information. An object variable contains an object reference, that is, the location of an object.
R2.3
An object is a value that can be created, stored, and manipulated. A class is a programmer-defined data type that describes objects with the same behavior. 
R2.4
new Rectangle(5, 10, 20, 30); // Object 
Rectangle box; // Object variable
R2.5
In the Java programming language, the = operator denotes an action, to replace the value of a variable. This usage differs from the traditional use of the = symbol as a statement about equality.
R2.6
new Rectangle(75, 75, 50, 50)
"Hello, Dave!"
R2.7
Rectangle square = new Rectangle(75, 75, 50, 50);
String greeting = "Hello, Dave!";
R2.8
Rectangle square = new Rectangle(10, 20, 40, 40);
square = new Rectangle(20, 20, 40, 40); // replaces with a new object
R2.9
Rectangle square1 = new Rectangle(20, 20, 40, 40);
Rectangle square2 = square1;  // the same object
R2.10
String message = "Hello";
message = message.toUpperCase();
R2.11
String message = "Hello";
message = message.replace("H", "h");
R2.12
a. new Rectangle is missing
b. Rectangle(5, 10, 15, 20) does not refer to an object. The corrected version should be:
double width = (new Rectangle(5, 10, 15, 20)).getWidth();
c. r has not been initialized; it does not refer to any object.
d. The method translate takes two integer arguments, not a string argument.
R2.13
Possible answers are:
Accessor: getWidth(), getHeight()
Mutator: translate(int, int), add(int, int) 
R2.14
The rectangle to which the variable box refers has the following coordinates:
x = 0;
y = 0;
width = 25;
height = 40;
R2.15
Console applications provide text-only interfaces and cannot display any drawings/figures (except ASCII art). Graphical applications are more user friendly and can display drawings inside frames (windows). 
R2.16
The Swing toolkit calls the paintComponent method whenever the component needs to be repainted. For example, it is called when the window is shown for the first time, when it is resized, or when it is shown again after it was hidden.
R2.17
The designers of Java did not want to inconvenience those programmers who had produced programs that used simple graphics from the Graphics class after they developed the newer, more powerful Graphics2D class, so they did not change the parameter of the paintComponent method to Graphics2D. 
R2.18
The purpose of a graphics context is to store the programmer choices for colors, fonts, and so on, and to draw and fill shapes. 
R2.19
The paintCompoment method of the component class produces a drawing, while the main method of the viewer class constructs a frame and a component, adds the component to the frame, and makes the frame visible. 
R2.20
You specify a text color simply by calling the setColor method of the graphics context before calling the drawString method.