11.11. Review Questions

 
[Page 368 ( continued )]

Programming Exercises

Comprehensive

10.1* ( Enabling GeometricObject comparable ) Modify the GeometricObject class to implement the Comparable interface, and define a static max method in the GeometricObject class for finding the larger of two GeometricObject objects. Draw the UML diagram and implement the new GeometricObject class. Write a test program that uses the max method to find the larger of two circles and the larger of two rectangles.
10.2* ( The ComparableRectangle class ) Create a class named ComparableRectangle that extends Rectangle and implements Comparable . Draw the UML diagram and implement the compareTo method to compare the rectangles on the basis of area. Write a test class to find the larger of two instances of - ComparableRectangle objects.
10.3* ( The Colorable interface ) Design an interface named Colorable with a void method named howToColor() . Every class of a colorable object must implement the Colorable interface. Design a class named Square that extends GeometricObject and implements Colorable . Implement howToColor to display a message on how to color the square.
[Page 369]

Draw a UML diagram that involves Colorable , Square , and GeometricObject . Write a test program that creates an array of five GeometricObjects . For each object in the array, invoke its howToColor method if it is colorable.

10.4* ( Revising the House class ) Rewrite the House class in Listing 10.4 to perform a deep copy on the whenBuilt field.
10.5* ( Enabling Circle comparable ) Rewrite the Circle class in Listing 9.2 to extend GeometricObject and implement the Comparable interface. Override the equals and hashCode methods in the Object class. Two Circle objects are equal if their radii are the same. Draw the UML diagram that involves Circle , GeometricObject , and Comparable .
10.6* ( Enabling Rectangle comparable ) Rewrite the Rectangle class in Listing 9.3 to extend GeometricObject and implement the Comparable interface. Override the equals and hashCode methods in the Object class. Two Rectangle objects are equal if their areas are the same. Draw the UML diagram that involves Rectangle , GeometricObject , and Comparable .
10.7* ( The Octagon class ) Write a class named Octagon that extends GeometricObject and implements the Comparable and Cloneable interfaces. Assume that all eight sides of the octagon are of equal size . The area can be computed using the following formula:


Draw the UML diagram that involves Octagon , GeometricObject , Comparable , and Cloneable . Write a test program that creates an Octagon object with side value 5 and displays its area and perimeter. Create a new object using the clone method and compare the two objects using the compareTo method.

10.8* ( Summing the areas of geometric objects ) Write a method that sums the areas of all the geometric objects in an array. The method signature is:
   public static double   sumArea(GeometricObject[] a) 

Write a test program that creates an array of three objects (a circle and a rectangle) and computes their total area using the sumArea method.

10.9* ( Finding the largest object ) Write a method that returns the largest object in an array of objects. The method signature is:
   public static   Object max(Object[] a) 

All the objects are instances of the Comparable interface. The order of the objects in the array is determined using the compareTo method.

Write a test program that creates an array of ten strings, an array of ten integers, and an array of ten dates, and finds the largest string, integer, and date in the arrays.

10.10** ( Displaying calendars ) Rewrite the PrintCalendar class in Listing 5.8 to display a calendar for a specified month using the Calendar and GregorianCalendar classes. Your program receives the month and year from the command line. For example:
 java Exercise10_10 6 2005 


[Page 370]

This displays the calendar shown in Figure 10.11.

Figure 10.11. The program displays a calendar for June 2005.


You also can run the program without the year. In this case, the year is the current year. If you run the program without specifying a month and a year, the month is the current month.

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net