Page 376 - Introduction to Programming with Java: A Problem Solving Approach
P. 376
342 Chapter 8 Software Engineering
12. [after §8.7] Write a generic drawRow method having this heading: private void drawRow(int startCol, int endCol)
startCol and endCol are the column numbers of the left and right borders, respectively. Then modify the Square class’s draw method in Figure 8.7a to draw either a solid square or a solid triangle whose height and width equal the width of the input width of a square container. What about the area now? Is it a redundant value, or is it a legitimate object attribute? Modify the instance variables and the getArea method accordingly. Then drive your modified Square class (call it Square2) with a Square2Driver whose main method looks like this:
public static void main(String[] args)
{
}
Scanner stdIn = new Scanner(System.in);
Square2 square;
System.out.print("Enter width of square container: ");
square = new Square2(stdIn.nextInt());
square.draw();
System.out.println("Area = " + square.getArea());
// end main
Sample session:
Enter
width of square container: 5
Apago PDF Enhancer
Print
(s)quare or (t)riangle? t
*
**
***
****
*****
Area = 15
13. [after §8.9] Write a prototype of the Square program, using just one class called SquarePrototype, with only one method, main. Write the minimum amount of code needed to generate the prescribed output for only the simplest case of a solid square. The sample session should look exactly like it would for the final program described in Figures 8.6, 8.9a, and 8.9b, if the user selects the (s)olid option. If the user selects the (b)order option, however, the prototype should respond by printing a “Not Implemented.”
14. [after §8.9] When you design something, you should select the design methodology that is best able to address the greatest current design concern. (T / F)
15. [after §8.10] Write a separate driver program that executes the Time class shown in Figure 8.10 and sets the time for 17 hours, 30 minutes, and zero seconds. Assume that the main method that appears in Figure 8.10 is still there.
16. [after §8.11] Rewrite the Car class in Figure 7.2 to eliminate the use of this.
17. [after §8.12] The Java API Calendar class contains a method called getTimeInMillisec which enables you to retrieve the absolute time (in milliseconds) at which any Calendar object was created. As indicated in Section 8.12, you can get such an object by calling the getInstance class method. You can use this capability to evaluate the runtime of any chunk of code. All you have to do is create a Calendar object before the test code starts, create another Calendar object right after the code ends, and print
out the difference in those two object’s times. To demonstrate this capability, write a short program called