26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 9 Object-Oriented <strong>Program</strong>ming 533<br />

9.22 <strong>How</strong> does polymorphism promote extensibility?<br />

9.23 You have been asked <strong>to</strong> develop a flight simula<strong>to</strong>r that will have elaborate graphical outputs.<br />

Explain why polymorphic programming would be especially effective for a problem of this nature.<br />

9.24 Develop a basic graphics package. Use the Shape class inheritance hierarchy from<br />

Figure 9.3. Limit yourself <strong>to</strong> two-dimensional shapes such as squares, rectangles, triangles and circles.<br />

Interact with the user. Let the user specify the position, size, shape and fill colors <strong>to</strong> be used in<br />

drawing each shape. The user can specify many items of the same shape. As you create each shape,<br />

place a Shape reference <strong>to</strong> each new Shape object in<strong>to</strong> an array. Each class has its own draw method.<br />

Write a polymorphic screen manager that walks through the array sending draw messages <strong>to</strong> each<br />

object in the array <strong>to</strong> form a screen image. Redraw the screen image each time the user specifies an<br />

additional shape. Investigate the methods of class Graphics <strong>to</strong> help draw each shape.<br />

9.25 Modify the payroll system of Fig. 9.16–Fig. 9.21 <strong>to</strong> add private instance variables<br />

birthDate (use class Date from Figure 8.13) and departmentCode (an int) <strong>to</strong> class Employee.<br />

Assume this payroll is processed once per month. Then, as your program calculates the payroll<br />

for each Employee (polymorphically), add a $100.00 bonus <strong>to</strong> the person’s payroll amount if<br />

this is the month in which the Employee’s birthday occurs.<br />

9.26 In Exercise 9.15, you developed a Shape class hierarchy and defined the classes in the hierarchy.<br />

Modify the hierarchy so that class Shape is an abstract superclass containing the interface<br />

<strong>to</strong> the hierarchy. Derive TwoDimensionalShape and ThreeDimensionalShape from<br />

class Shape—these classes should also be abstract. Use an abstract print method <strong>to</strong> output<br />

the type and dimensions of each class. Also include area and volume methods so these calculations<br />

can be performed for objects of each concrete class in the hierarchy. Write a driver program<br />

that tests the Shape class hierarchy.<br />

9.27 Rewrite your solution <strong>to</strong> Exercise 9.26 <strong>to</strong> use a Shape interface instead of an abstract<br />

Shape class.<br />

9.28 (Drawing Application) Modify the drawing program of Exercise 8.19 <strong>to</strong> create a drawing application<br />

that draws random lines, rectangles and ovals. [Note: Like an applet, a JFrame has a<br />

paint method that you can override <strong>to</strong> draw on the background of the JFrame.]<br />

For this exercise, modify the MyLine, MyOval and MyRect classes of Exercise 8.19 <strong>to</strong> create<br />

the class hierarchy in Fig. 9.45. The classes of the MyShape hierarchy should be “smart” shape<br />

classes where objects of these classes know how <strong>to</strong> draw themselves (if provided with a Graphics<br />

object that tells them where <strong>to</strong> draw). The only switch or if/else logic in this program should<br />

be <strong>to</strong> determine the type of shape object <strong>to</strong> create (use random numbers <strong>to</strong> pick the shape type and<br />

the coordinates of each shape). Once an object from this hierarchy is created, it will be manipulated<br />

for the rest of its lifetime as a superclass MyShape reference.<br />

Fig. Fig. Fig. 9.45 9.45 9.45 The MyShape hierarchy.<br />

java.lang.Object<br />

MyShape<br />

MyLine MyOval<br />

MyRect<br />

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!