444 Object-Based Programming Chapter 8 The data for (Ipower web hosting)
444 Object-Based Programming Chapter 8 The data for class MyLine should include x1, y1, x2 and y2 coordinates. Method drawLine method of class Graphics will connect the two points supplied with a line. The data for classes MyRect and MyOval should include an upper-left x-coordinate value, an upper-left y-coordinate value, a width (must be nonnegative) and a height (must be nonnegative). All data in each class must be private. In addition to the data, each class should define at least the following public methods: a) A constructor with no arguments that sets the coordinates to 0. b) A constructor with arguments that sets the coordinates to the supplied values. c) Set methods for each individual piece of data that allow the programmer to independently set any piece of data in a shape (e.g., if you have an instance variable x1, you should have a method setX1). d) Get methods for each individual piece of data that allow the programmer to independent ly retrieve any piece of data in a shape (e.g., if you have an instance variable x1, you should have a method getX1). e) A draw method with the first line public void draw( Graphics g ) will be called from the applet s paint method to draw a shape onto the screen. The preceding methods are required. If you would like to provide more methods for flexibility, please do so. Begin by defining class MyLine and an applet to test your classes. The applet should have a MyLine instance variable line that can refer to one MyLine object (created in the applet s init method with random coordinates). The applet s paint method should draw the shape with a statement like line.draw( g ); where line is the MyLine reference and g is the Graphicsobject that the shape will use to draw itself on the applet. Next, change the single MyLine reference into an array of MyLine references and hard code several MyLine objects into the program for drawing. The applet s paint method should walk through the array of MyLine objects and draw every one. After the preceding part is working, you should define the MyOval and MyRect classes and add objects of these classes into the MyRect and MyOval arrays. The applet s paint method should walk through each array and draw every shape. Create five shapes of each type. Once the applet is running, select Reload from the appletviewer s Applet menu to reload the applet. This will cause the applet to choose new random numbers for the shapes and draw the shapes again. In Chapter 9, we will modify this exercise to take advantage of the similarities between the classes and to avoid reinventing the wheel. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01