ICS 3M / 4M Section 14.2 An Abstract Shape Class As A Base Class 1 Section 14.3 Method Overloading In the Base Class Name __________________ Section 14.2 An Abstract Shape Class As A Base Class To improve upon the // The "Shape" class. programs of Section 14.1, we // Located in: Q:\JavaExamples\Ch14\2.abstract\Shape.java create a base or parent class // This is an abstract class from which all shapes descend. It contains that encapsulates the common // all the methods and data that are common to all shapes. For example, behavior of the rectangle, oval // you can call the show and hide methods of any Shape object, even and triangle shapes, but does // if you do not know the exact type of the object (Rectangle, Oval, not specify how the shapes // or Triangle). are actually drawn. Each shape has its own draw and import java.awt.*; erase method that differs from class to class. public abstract class Shape { Notice that this abstract class protected Component surface; // The surface to draw on. uses the abstract methods protected int x, y; // The x, y location of the shape. draw and erase, even though protected boolean visible = false; // Is the shape currently visible. their implementation is not protected Color color; // The color of the shape. yet available. Why ? protected abstract void draw (); // The method to draw the shape. protected abstract void erase (); // The method to erase the shape. // Create a shape that is to be drawn on the surface (which should // be either an Applet or a Frame). public Shape (Component surface, int x, int y, Color color) { this.surface = surface; this.x = x; this.y = y; this.color = color; } // Shape constructor // If the shape is not already visible, make it visible. public void show () { if (!visible) { draw (); visible = true; } } // show method Why are they also declared as protected ? Which methods appear completed in this abstract class, and therefore MUST have been the same among the 3 different shape classes? List them. Why aren’t the above listed methods also declared as abstract ? // If the shape is visible, hide it. public void hide () { if (visible) Why were they public ? ICS 3M / 4M Section 14.2 An Abstract Shape Class As A Base Class 2 Section 14.3 Method Overloading In the Base Class Name __________________ { What were the instance variables of this abstract class ( look erase (); at the top of the class on page 1 ) ? Are they the same as the visible = false; instance variables of the original rectangle, oval and triangle } classes ? Explain. } // hide method // Redraw the shape if it is visible. public void paint () { if (visible) { draw (); } } // paint method // Move the shape to a new location. public void move (int x, int y) { if (visible) { erase (); this.x = x; this.y = y; draw (); } else { this.x = x; this.y = y; } } // move method } /* Shape class */ // The "Rectangle" class. // Located in: Q:\JavaExamples\Ch14\2.abstract\Rectangle.java // This class handles rectangles on a graphics surface. Once created, they // can be moved, hidden, and shown again. import java.awt.*; public class Rectangle extends Shape { protected int rectWidth, rectHeight; // The rectangle's width and height. // Create a rectangle that is to be drawn on the surface (which should // be either an Applet or a Frame). The Rectangle class has changed, now that it is an extension of the Shape class. What methods are left over from the original Rectangle class? Why were they left over, but others were not ? ICS 3M / 4M Section 14.2 An Abstract Shape Class As A Base Class 3 Section 14.3 Method Overloading In the Base Class Name __________________ public Rectangle (Component surface, int x, int y, int rectWidth, int rectHeight) { Are the instance variables of the former Rectangle class super (surface, x, y, Color.red); found within the new Rectangle class ? If not, where has this.rectWidth = rectWidth; their declarations moved to ? this.rectHeight = rectHeight; show (); } // Rectangle constructor // Draw the shape. protected void draw () { Graphics g = surface.getGraphics (); g.setColor (color); g.fillRect (x, y, rectWidth, rectHeight); } // draw method // Erase the shape. protected void erase () { Graphics g = surface.getGraphics (); g.setColor (surface.getBackground ()); g.fillRect (x, y, rectWidth, rectHeight); } // erase method } /* Rectangle class */ // The "ShapeTest" class. // Located in: Q:\JavaExamples\Ch14\2.abstract\ShapeTest.java // This applet randomly places 10 shapes on the drawing surface. import java.applet.*; import java.awt.*; public class ShapeTest extends Applet { protected final static int NUM_SHAPES = 10; // The array containing the Shape objects. protected Shape[] shapes = new Shape [NUM_SHAPES]; // This method is called when the Applet object is created. public void init () { // Get the width and height of the drawing surface. int surfaceWidth = getSize ().width; int surfaceHeight = getSize ().height; Question: In what ways is this class different than the original ShapeTest class from Section 14.1 ? Answer: There are some differences – but they are not because of the factoring of the different shape classes. (Factoring is the term in object-oriented languages for determining what methods can be put into an abstract class to be used by classes which are extended from it.) The difference lies in the way the painting of the shapes is done and when it is done. Explain. (The difference was not critical as the two ShapeTest classes are in fact interchangeable.) ICS 3M / 4M Section 14.2 An Abstract Shape Class As A Base Class 4 Section 14.3 Method Overloading In the Base Class Name __________________ // Create the shapes and place them randomly on the drawing // surface. for (int count = 0 ; count < NUM_SHAPES ; count++) { int shapeKind = (int) (Math.random () * 3); int x = (int) (Math.random () * surfaceWidth); int y = (int) (Math.random () * surfaceHeight); if (shapeKind == 0) { int rectWidth = (int) (Math.random () * 20) + 10; int rectHeight = (int) (Math.random () * 20) + 10; shapes [count] = new Rectangle (this, x, y, rectWidth, rectHeight); } else if (shapeKind == 1) { int xRadius = (int) (Math.random () * 10) + 10; int yRadius = (int) (Math.random () * 10) + 10; shapes [count] = new Oval (this, x, y, xRadius, yRadius); } else if (shapeKind == 2) { int sideLength = (int) (Math.random () * 20) + 10; shapes [count] = new Triangle (this, x, y, sideLength); } } } // init method // This method is called whenever the applet's drawing surface is // obscured and then revealed. We call the paint method for each // shape to redraw that shape (if the shape is currently visible). public void paint (Graphics g) { // For each shape, call its paint method. for (int count = 0 ; count < NUM_SHAPES ; count++) { shapes [count].paint (); } } // paint method } /* ShapeTest class */ ICS 3M / 4M Section 14.2 An Abstract Shape Class As A Base Class 5 Section 14.3 Method Overloading In the Base Class Name __________________ Section 14.3 Method Overloading In the Base Class Read Section 14.3 carefully and answer these questions: 1. There is a new constructor method being added to the Shape class on page 401. What is its purpose ? 2. The move method is also overloaded in the new Shape class. What is the difference between the two versions which are available ? 3. Here is the new Rectangle class. Note that it now has 3 constructor methods instead of one. // The "Rectangle" class. // Located in: Q:\JavaExamples\Ch14\3.overloading\Rectangle.java // This class handles rectangles on a graphics surface. Once created, they // can be moved, hidden, and shown again. What is the primary difference between the 3 constructor methods ? First: import java.awt.*; public class Rectangle extends Shape { protected int rectWidth, rectHeight; // The rectangle's width and height. // Create a rectangle that is to be drawn on the surface (which should // be either an Applet or a Frame). public Rectangle (Component surface, int x, int y, int rectWidth, int rectHeight) { super (surface, x, y, Color.red); this.rectWidth = rectWidth; this.rectHeight = rectHeight; show (); } // Rectangle constructor // Create a rectangle that is to be drawn on the surface (which should // be either an Applet or a Frame). Randomly place it on the // drawing surface. public Rectangle (Component surface, int rectWidth, int rectHeight) { Second: Third: The word super is used in these methods and it was never used in the previous Rectangle classes – not even in Section 14.2 when Shapes was an abstract class. ICS 3M / 4M Section 14.2 An Abstract Shape Class As A Base Class 6 Section 14.3 Method Overloading In the Base Class Name __________________ super (surface, Color.red); What is the meaning of the this.rectWidth = rectWidth; word super ? this.rectHeight = rectHeight; show (); } // Rectangle constructor // Create a rectangle that is to be drawn on the surface (which should // be either an Applet or a Frame). Randomly place it on the // drawing surface. Randomly determine the width and height // of the rectangle. public Rectangle (Component surface) { super (surface, Color.red); this.rectWidth = (int) (Math.random () * 20) + 10; this.rectHeight = (int) (Math.random () * 20) + 10; show (); } // Rectangle constructor // Draw the shape. protected void draw () { Graphics g = surface.getGraphics (); g.setColor (color); g.fillRect (x, y, rectWidth, rectHeight); } // draw method // Erase the shape. protected void erase () { Graphics g = surface.getGraphics (); g.setColor (surface.getBackground ()); g.fillRect (x, y, rectWidth, rectHeight); } // erase method } /* Rectangle class */ // The "ShapeTest" class. // Located in: Q:\JavaExamples\Ch14\3.overloading\ShapeTest.java // This applet randomly places 10 shapes on the drawing surface. import java.applet.*; import java.awt.*; public class ShapeTest extends Applet { protected final static int NUM_SHAPES = 10; Why is super only used in relation to the surface, and not rectWidth and rectHeight ? ICS 3M / 4M Section 14.2 An Abstract Shape Class As A Base Class 7 Section 14.3 Method Overloading In the Base Class Name __________________ // The array containing the Shape objects. protected Shape[] shapes = new Shape [NUM_SHAPES]; // This method is called when the Applet object is created. public void init () { // Get the width and height of the drawing surface. int surfaceWidth = getSize ().width; int surfaceHeight = getSize ().height; How is this ShapeTest class different from the ShapeTest class of Section 14.2. Answer carefully. Consider all that has happened in the new constructor methods of the individual shapes classes. Use page 409 as a reference, because this difficult question is answered there. Use your own words. // Create the shapes and place them randomly on the drawing surface. for (int count = 0 ; count < NUM_SHAPES ; count++) { int shapeKind = (int) (Math.random () * 3); if (shapeKind == 0) { // The following two lines place the upper-left corner // of the rectangle in the upper-left quadrant. int x = (int) (Math.random () * surfaceWidth / 2); int y = (int) (Math.random () * surfaceHeight / 2); int rectWidth = (int) (Math.random () * 20) + 10; int rectHeight = (int) (Math.random () * 20) + 10; shapes [count] = new Rectangle (this, x, y, rectWidth, rectHeight); } else if (shapeKind == 1) { int xRadius = (int) (Math.random () * 30) + 10; int yRadius = (int) (Math.random () * 10) + 10; shapes [count] = new Oval (this, xRadius, yRadius); } else if (shapeKind == 2) { shapes [count] = new Triangle (this); } } } // init method // This method is called whenever the applet's drawing surface is // obscured and then revealed. We call the paint method for each // shape to redraw that shape (if the shape is currently visible). public void paint (Graphics g) { // For each shape, call its paint method. ICS 3M / 4M Section 14.2 An Abstract Shape Class As A Base Class 8 Section 14.3 Method Overloading In the Base Class Name __________________ for (int count = 0 ; count < NUM_SHAPES ; count++) { shapes [count].paint (); } } // paint method } /* ShapeTest class */