Question: We will implement a classic Inheritance hierarchy and implement a little polymorphism. A simple console based interface is all that is needed. GUI programming is
We will implement a classic Inheritance hierarchy and implement a little polymorphism. A simple console based interface is all that is needed. GUI programming is NOT required for this project, unless you do the optional GUI assignment listed at the bottom. Build your classes first, each in their own .java file, then test them with the simple main method provided below.
Phase 1: Here is the following set of classes you will implement and their inheritance relationship:
-Shape The generic abstract class shape will serve as the parent class to Circle, Rectangle and Triangle. This class MUST be abstract and it will contain abstract methods Variables: x x coordinate for the shape, an integer y y coordinate for the shape, an integer Methods: getX() returns an int for the shapes x position setX() assign the shapes x position getY() returns an int for the shapes y position setY() assign the shapes y position A constructor with no parameters and one with x,y parameters display() an abstract method to display the shape area() an abstract method to calculate the shapes area
-Circle, It must inherit Shape. Variables: radius an int, length of the radius Methods: getRadius() returns an int for the circles radius setRadius () assign the circles radius A constructor with no parameters and one with x, y, radius parameters (in that order) display() - display the circle as a text string containing the word Circle, and the x, y and radius values area() - calculate and return a double of the area.
-Rectangle, It must inherit Shape. Variables: width an int height an int Methods: getHeight() returns an int for the rectangles height setHeight () assign the rectangles height getWidth() returns an int for the rectangles width setWidth () assign the rectangles width A constructor with no parameters and one with x, y, height, width parameters(in that order) display() - display the rectangle as a text string containing the word Rectangle, and the x, y, width and height values area() - calculate and return a double of the area.
-Triangle, It must inherit Shape. Variables: base an int height an int Methods: getHeight() returns an int for the Triangle height setHeight () assign the Triangle height getBase() returns an int for the Triangle base setBase () assign the Triangle base A constructor with no parameters and one with x, y, height, base parameters (in that order) display() - display the triangle as a text string containing the word Triangle, and the x, y, width and height values area() - calculate and return a double of the area.
Note: all display() methods should use System.out.println() to output a text description of the shape to the console. If you build the classes correctly, the following main program should work:
public class Project6 { private Shape [] thearray = new Shape[100]; // 100 Shapes, circle's, tri's and rects public static void main (String [] args) { Project6 tpo = new Project6(); tpo.run(); } // end of main public void run () { int count = 0; // ------------------------ Fill the array section ---------------------- thearray[count++] = new Circle(20, 20, 40); thearray[count++] = new Triangle(70, 70, 20, 30); thearray[count++] = new Rectangle(150, 150, 40, 40); // ------------------------------ array fill done ------------------------ //--------------------------- loop through the array -------------------- for (int i = 0; i < count; i ++ ) { // loop through all objects in the array thearray[i].display(); // dont care what kind of object, display it } // end for loop int offset = 0; double totalarea = 0.0; while (thearray[offset] != null) { // loop through all objects in the array totalarea = totalarea + thearray[offset].area(); // get area for this object offset++; } // end while loop System.out.println("The total area for " + offset + " Shape objects is " + totalarea); } // end of run } // end of class Project6
When get the main program above working make sure it works with different Shape objects placed in the array. When I test the program I will provide a different Fill in the array section of the code and expect your program to work with the new objects. This means you must implement all the methods listed in the class specifications exactly as I ask. The new code I provide will assume they are available and will call them. Remember: I will only change (and you should only change when testing) the following section of code: // ------------------------ Fill the array section ---------------------- thearray[count++] = new Circle(20, 20, 40); thearray[count++] = new Triangle(70, 70, 20, 30); thearray[count++] = new Rectangle(150, 150, 40, 40); // ------------------------------ array fill done ------------------------
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
