Best web hosting - Chapter 9 Object-Oriented Programming 533 9.22 How does
Chapter 9 Object-Oriented Programming 533 9.22 How does polymorphism promote extensibility? 9.23 You have been asked to develop a flight simulator that will have elaborate graphical outputs. Explain why polymorphic programming would be especially effective for a problem of this nature. 9.24 Develop a basic graphics package. Use the Shape class inheritance hierarchy from Figure 9.3. Limit yourself to two-dimensional shapes such as squares, rectangles, triangles and circles. Interact with the user. Let the user specify the position, size, shape and fill colors to be used in drawing each shape. The user can specify many items of the same shape. As you create each shape, place a Shapereference to each new Shapeobject into an array. Each class has its own drawmethod. Write a polymorphic screen manager that walks through the array sending drawmessages to each object in the array to form a screen image. Redraw the screen image each time the user specifies an additional shape. Investigate the methods of class Graphicsto help draw each shape. 9.25 Modify the payroll system of Fig. 9.16 Fig. 9.21 to add private instance variables birthDate (use class Date from Figure 8.13) and departmentCode (an int) to class Employee. Assume this payroll is processed once per month. Then, as your program calculates the payroll for each Employee (polymorphically), add a $100.00 bonus to the person s payroll amount if this is the month in which the Employee s birthday occurs. 9.26 In Exercise 9.15, you developed a Shape class hierarchy and defined the classes in the hierarchy. Modify the hierarchy so that class Shapeis an abstractsuperclass containing the interface to the hierarchy. Derive TwoDimensionalShape and ThreeDimensionalShape from class Shape these classes should also be abstract. Use an abstractprintmethod to output the type and dimensions of each class. Also include areaand volumemethods so these calculations can be performed for objects of each concrete class in the hierarchy. Write a driver program that tests the Shapeclass hierarchy. 9.27 Rewrite your solution to Exercise 9.26 to use a Shape interface instead of an abstract Shapeclass. 9.28 (Drawing Application) Modify the drawing program of Exercise 8.19 to create a drawing application that draws random lines, rectangles and ovals. [Note: Like an applet, a JFrame has a paintmethod that you can override to draw on the background of the JFrame.] For this exercise, modify the MyLine, MyOval and MyRect classes of Exercise 8.19 to create the class hierarchy in Fig. 9.45. The classes of the MyShapehierarchy should be smart shape classes where objects of these classes know how to draw themselves (if provided with a Graphics object that tells them where to draw). The only switch or if/else logic in this program should be to determine the type of shape object to create (use random numbers to pick the shape type and the coordinates of each shape). Once an object from this hierarchy is created, it will be manipulated for the rest of its lifetime as a superclass MyShapereference. java.lang.Object MyShape MyLine MyOval MyRect Fig. 9.45 The MyShapehierarchy. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01