Page 374 - Introduction to Programming with Java: A Problem Solving Approach
P. 374

                340 Chapter 8 Software Engineering
4. [after §8.3] Assume that the GarageDoorSystem class in Figures 7.25a and 7.25b has another instance
variable:
public final String SYSTEM_ID;
Rewrite the GarageDoorSystem constructor so that it calls a helper method called initialize which asks the user to supply a name for SYSTEM_ID. In that method, also ask the user if the starting position
is to be up. Then, use the user’s input to initialize the state variable, assuming the only possible starting states are down (0) or up (2).
5. [after §8.4] This exercise demonstrates using a reference parameter to pass data back to the calling method. Suppose you want a Car5 class to include a method with this heading:
public boolean copyTo(Car5 newCar)
This method is supposed to be called by an existing Car5 object with an argument to a new Car5 object. If any of the calling car’s instance variables has not been initialized, the desired method should not try to modify any of the new car’s instance variable values, and the method should return false. Otherwise, the method should copy all of the calling car’s instance variable values into the new car and return true. Here’s a driver that illustrates the usage:
/****************************************************************
*
Car5Driver.java
Dean & Dean
This class is a demonstration driver for the Car5 class.
*
*
*
Apago PDF Enhancer
****************************************************************/
public class Car5Driver
{
}
// end class Car5Driver
public static void main(String[] args)
{
}
Car5 annaCar = new Car5();
Car5 nickCar = new Car5();
System.out.println(annaCar.copyTo(nickCar));
annaCar = new Car5("Porsche", 2006, "beige");
System.out.println(annaCar.copyTo(nickCar));
// end main
Output:
false
true
Write the code for the desired copyTo method.
6. [after § 8.5] We recommend that you test frequently, even if it means creating special test code that is not
used in the final program. Why might it be useful to save such special test code?
7. [after §8.6] Assuming it will be called by the draw method in the Square class in Figure 8.7a, write a drawSolidSquare method that asks the user for the character to print and draws the desired solid square all by itself, without calling any separate drawHorizontalLine method.
 



























































   372   373   374   375   376