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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

Class MyShape in Fig. 9.45 must be abstract. The only data representing the coordinates<br />

of the shapes in the hierarchy should be defined in class MyShape. Lines, rectangles and ovals can<br />

all be drawn if you know two points in space. Lines require x1, y1, x2 and y2 coordinates. The<br />

drawLine method of the Graphics class will connect the two points supplied with a line. If you<br />

have the same four coordinate values (x1, y1, x2 and y2) for ovals and rectangles, you can calculate<br />

the four arguments needed <strong>to</strong> draw them. Each requires an upper-left x-coordinate value (minimum<br />

of the two x-coordinate values), an upper-left y-coordinate value (minimum of the two y coordinate<br />

values), a width (difference between the two x-coordinate values; must be nonnegative) and a height<br />

(difference between the two y-coordinate values; must be nonnegative). [Note: In Chapter 12, each<br />

x,y pair will be captured by using mouse events from mouse interactions between the user and the<br />

program’s background. These coordinates will be s<strong>to</strong>red in an appropriate shape object as selected<br />

by the user. As you begin the exercise, you will use random coordinate values as arguments <strong>to</strong> the<br />

construc<strong>to</strong>r.]<br />

In addition <strong>to</strong> the data for the hierarchy, class MyShape should define at least the following<br />

methods:<br />

a) A construc<strong>to</strong>r with no arguments that sets the coordinates <strong>to</strong> 0.<br />

b) A construc<strong>to</strong>r with arguments that sets the coordinates <strong>to</strong> the supplied values.<br />

c) Set methods for each individual piece of data that allow the programmer <strong>to</strong> independently<br />

set any piece of data for a shape in the hierarchy (e.g., if you have an instance variable<br />

x1, you should have a method setX1).<br />

d) Get methods for each individual piece of data that allow the programmer <strong>to</strong> independently<br />

retrieve any piece of data for a shape in the hierarchy (e.g., if you have an instance variable<br />

x1, you should have a method getX1).<br />

e) The abstract method<br />

public abstract void draw( Graphics g );<br />

This method will be called from the program’s paint method <strong>to</strong> draw a shape on<strong>to</strong> the<br />

screen.<br />

The preceding methods are required. If you would like <strong>to</strong> provide more methods for flexibility,<br />

please do so. <strong>How</strong>ever, be sure that any method you define in this class is a method that would be<br />

used by all shapes in the hierarchy.<br />

All data must be private <strong>to</strong> class MyShape in this exercise (this forces you <strong>to</strong> use proper<br />

encapsulation of the data and provide proper set/get methods <strong>to</strong> manipulate the data). You are not<br />

allowed <strong>to</strong> define new data that can be derived from existing information. As explained previously,<br />

the upper-left x, upper-left y, width and height needed <strong>to</strong> draw an oval or rectangle can be calculated<br />

if you already know two points in space. All subclasses of MyShape should provide two construc<strong>to</strong>rs<br />

that mimic those provided by class MyShape.<br />

Objects of the MyOval and MyRect classes should not calculate their upper-left x-coordinate,<br />

upper-left y-coordinate, width and height until they are about <strong>to</strong> draw. Never modify the x1, y1, x2<br />

and y2 coordinates of a MyOval or MyRect object <strong>to</strong> prepare <strong>to</strong> draw them. Instead, use the temporary<br />

results of the calculations described above. This will help us enhance the program in Chapter 12<br />

by allowing the user <strong>to</strong> select each shape’s coordinates with the mouse.<br />

There should be no MyLine, MyOval or MyRect references in the program—only MyShape<br />

references that refer <strong>to</strong> MyLine, MyOval and MyRect objects are allowed. The program should<br />

keep an array of MyShape references containing all shapes. The program’s paint method should<br />

walk through the array of MyShape references and draw every shape (i.e., call every shape’s draw<br />

method).<br />

Begin by defining class MyShape, class MyLine and an application <strong>to</strong> test your classes. The<br />

application should have a MyShape instance variable that can refer <strong>to</strong> one MyLine object (created<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!