Question: Computer Science 111 Introduction to Algorithms and Programming: Java Programming Project #5 Polymorphism (10 points total) For project #5 we will implement a classic Inheritance

Computer Science 111 Introduction to Algorithms and Programming: Java

Programming Project #5 Polymorphism (10 points total)

For project #5 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 GUI assignment listed at the bottom. Phase 1 is worth 15 points and additional phases are 5 points each. Build your classes first, each in their own .java file, then test them with the simple main method provided below.

Phase 1 (15 points):

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

Ummm, do I really need to explain this? It is the round thingy, with a center and a radius. Ohhh, and 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

Ummm, do I really need to explain this? It is that boxy thing with 4 sides. Ohhh, and 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

Ummm, do I really need to explain this? It is that pointy thing with 3 sides. Ohhh, and 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 Project5 {

private Shape [] thearray = new Shape[100]; // 100 Shapes, circle's, tri's and rects

public static void main (String [] args) {

Project5 tpo = new Project5();

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 Project5

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!