Project6
- Due Dec 2, 2017 by 11am
- Points 30
- Submitting on paper
- Available Nov 17, 2017 at 12am - Dec 2, 2017 at 11:59pm
Computer Science 111 Introduction to Algorithms and Programming: Java
Programming Project #6 Polymorphism & Inheritance (30 points total)
For project #5 we will implement a classic Inheritance hierarchy and implement a little polymorphism. Build your classes first, each in their own .java file, then test them with the simple main method provided below.
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
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) {
Project5 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(); // don’t 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 ------------------------