Exercise Solutions

advertisement
Fundamentals of Java
Chapter 11: Classes Continued
UNIT 3— ARRAYS, RECURSION, AND COMPLEXITY
CHAPTER 11— CLASSES CONTINUED
EXERCISE 11.1
1. static public final int DEFAULT_NUM_SCORES = 3;
2. Java allocates a separate set of memory cells in each instance for the instance
variables of a class. By contrast, only one set of cells is allocated for the class
variables.
3. First, a static method can be included to allow the user to call the method by
sending a message to the class. This method can serve as a public accessor or
mutator of a class variable. Second, a static method can be included to perform
some useful calculation. The Math class methods are good examples. Third, a
static method can be included in a set of cooperating helper methods to
decompose responsibilities for tasks in a class.
4. The reference to instVar in the static method is a syntax error because static
methods cannot reference instance variables.
5. A public constant is accessible to all clients, and a static constant is accessible by
using the class name as a reference.
EXERCISE 11.3
1. The purpose of an interface is to provide clients with enough information to be
able to use the methods of a class. An interface includes just a set of method
signatures, whereas a class includes the implementations of these methods and
perhaps some data as well.
2.
static private void drawSquare(Pen p, int x, int y, int length){
p.up();
p.move(x, y);
p.down();
p.setDirection(270);
for (int i = 1; i <= 4; i++){
p.move(length);
p.turn(90);
}
}
EXERCISE 11.4
1.
public interface Account{
public double getBalance();
public void deposit(double amount);
public void withdraw(double amount);
1
Fundamentals of Java
Chapter 11: Classes Continued
}
2. The expression implements <an interface> causes the compiler to check the
implementing class for the presence of all the methods in the specified interface.
If any of them are missing, a syntax error occurs.
EXERCISE 11.5
1. A class hierarchy is a set of classes that are organized by the relationship of
subclass and superclass. An example is the hierarchy Object/Circle/Wheel, where
Circle is a subclass of Object and Wheel is a subclass of Circle.
2. The expression extends <a class> is used to inherit the data and behavior of
another class.
3. To run a constructor of the same form in the superclass, one uses the form
super(<parameters>) at the beginning of the corresponding constructor in the
subclass.
4. With methods other than constructors, one uses the form super.<method name>
(<parameters>).
5. The visibility modifier protected allows subclasses in a hierarchy to access
instance and class variables or methods of superclasses without allowing other
clients in the system to access these items.
6. There is a syntax error. The method setSpokes is not included in the Shape
interface. The programmer attempts to send this message to an instance of Wheel,
but this object is masquerading as a Shape. The variable s must be cast to a Wheel
before sending it the setSpokes message.
EXERCISE 11.6
1. a. Syntax error: the array element, as an Object, does not understand the indexOf
message.
b. Syntax error: same as a.
c. Correct.
d. Runtime error: ClassCastException because a Student is cast to a String.
EXERCISE 11.7
1. An abstract class serves as a repository of data and behavior common to a set of
subclasses. Because this class is abstract, it cannot be instantiated. An example is
the AbstractShape class, which is abstract and defines the behavior common to
circles and rectangles.
2. We declare abstract methods for two possible reasons. First, the abstract class in
which they are declared implements an interface, but the implementation of these
methods is deferred to the concrete classes. Second, even if there is no interface,
2
Fundamentals of Java
Chapter 11: Classes Continued
these methods must be implemented in all subclasses, so declaring them abstract
enforces this.
3. A final method cannot be overridden in a subclass. Examples are the accessors for
variables that are defined in an abstract class. These methods should not be
overridden by subclasses.
4. A protected variable is accessible throughout the defining class and any of its
subclasses. An example is a balance for all subclasses of an abstract bank account
class.
EXERCISE 11.8
1. Generally, to locate the right method to execute, the JVM first looks in the class
of the receiver object. If the method is not found there, the JVM look at this
class’s superclass, if there is one. This process is repeated until a method is found
or an exception is thrown (if the method is not eventually found in the Object
class). In this example, the server’s method someMethod is executed first. Within
this method, the JVM executes a call to the method someMethod in the superclass.
2. Student answers will vary, but here are some examples. The main application
class in a terminal-based program depends on the Scanner class for input
operations. A phone book class aggregates objects that consist of a name, address,
and phone number. A colored point inherits data and behavior from a point.
EXERCISE 11.9
1. The rule for passing a parameter object is the same as the rule for assignment,
namely, the class of the actual parameter must be the same as or less inclusive
than the class of the formal parameter.
2. The answer is b.
EXERCISE 11.10
1. Preconditions describe assumptions that must be true before a method can
perform its task correctly. For example, a method that uses a parameter as a
divisor must expect that number to not be zero.
2. Postconditions describe assumptions that must be true after a method executes
correctly. For example, a correctly executed method to deposit money in a bank
account will leave behind a balance that is the sum of the balance before the
deposit was made and the amount deposited.
EXERCISE 11.11
3
Fundamentals of Java
Chapter 11: Classes Continued
1. A method should throw an exception when its preconditions are violated. For
example, if an integer parameter must be less than a certain value, the method
should throw an exception if the parameter is greater than or equal to that value.
2. When a method throws an exception, control is passed immediately to the calling
method and from there goes on up the call chain to the main method, where the
JVM halts execution with a trace of this call chain.
3. An exception can be caught and handled by using a try-catch statement.
4. The @param tag marks a parameter of a method. The @return tag marks the
returned value of a method. The @throws tag marks an exception thrown by the
method.
EXERCISE 11.12
1. The programmer should use equals instead of == when comparing two objects
because == is too restrictive. In cases where two distinct objects are structurally the
same (have the same values for all their instance variables), == would return false
because == returns true only when the operands refer to one object.
2. true
false
false
3. When an object is assigned to a variable, that variable contains a reference to the
object.
4. To obtain a true copy of an object, you must create a new instance of that object’s
class and then copy the original object’s instance variables to the new object’s
variables.
EXERCISE 10.13
1. The advantages of using turtle graphics are that it’s thoroughly object-oriented
and there is no forgetful bitmap to worry about. The disadvantages are that shapes
cannot be filled and the drawing is a bit slow. The advantages of using the
standard graphics classes are that the methods are very fast and the shapes can be
filled. The main disadvantage is that the programmer must refresh the forgetful
bitmap.
REVIEW QUESTIONS
Fill in the Blank
1.
2.
3.
4.
5.
class, static
super
protected
abstract
abstract
4
Fundamentals of Java
Chapter 11: Classes Continued
6. preconditions, postconditions
7. equals, clone
5
Download