Chapter 2: Review Exercise Solutions R2.1
Chapter 2: Review Exercise Solutions R2.1
Chapter 2: Review Exercise Solutions R2.1
R2.1
An object is an instance (an entity) that defined by a class. A class provides a definition which
includes characteristics (data) and behavior (methods).
R2.2
The public interface consists of all the methods we can apply to any of its objects. Essentially, it
is the set of methods to interact with the class. The implementation is how the methods
accomplish their tasks. The public interface is accessible to all; the implementation should be
private.
R2.3
R2.4
The value of mystery is equal to 0 after the statements are executed. In the first statement (line
1), mystery is initialized to a value of 1. In the assignment statement on line 2, mystery is set to
-1. Finally, mystery is set to 0 in line 3.
R2.5
The variable mystery is being declared twice, first in line 1 and then again in line 2. A variable
can only be initialized once. (If you remove the reserved word int on line 3, the statements will
work just fine, and will set mystery to -3.)
R2.6
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.7
R2.8
R2.9
R2.10
An object contains state information. An object variable contains an object reference, that is, the
location of an object.
R2.11
R2.12
R2.13
R2.14
R2.15
R2.16
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();
d. The method translate takes two integer arguments, not a string argument.
R2.17
R2.18
R2.19
An object contains state information. An object reference is the location of that object in
memory.
R2.20
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.21
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.22
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.23
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.24
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.25
You specify a text color simply by calling the setColor method of the graphics context before
calling the drawString method.