Lab 11: Inheritance Implementation
Objective
To become familiar with writing classes that inherit from one another.
Task
For this lab, you will implement the classes we designed in Lab 10.
Your UML design should have most of the details on the inheritance as well the variables and methods needed. Here are some notes on building this:
- Shape should be an abstract class, and the getArea method should be marked abstract. The display method should print the name of the shape followed by the area. We can call getArea inside of display even though it's abstract. The constructor should store the name.
- Circle should have its constructor take the
name and radius as parameters. It should pass the name along to
the Shape constructor using the
superkeyword. Note that it should not store the name itself! The constructor should also store the radius. The getArea and getCircumference methods should use the following formulas:- $A = \pi \cdot radius^2$
- $C = 2 \cdot \pi \cdot radius$
- Polygon should be an abstract class. That is because it won't provide an implementation for getArea (which is different for different sorts of polygons). It will also add a new abstract method, which is getPerimeter. Its constructor passes the name of the shape up to the Shape constructor, and stores the number of sides.
- Rectangle will pass the name up to the superclass
constructor, and store the length and width. Its getArea and getPerimeter
can use the following formulas:
- $A = length \cdot width$
- $P = 2 \cdot length + 2 \cdot width$
- Triangle should also pass its name to the
Polygon constructor. It will then save its base and height.
It will also take two more parameters not lists in Lab 11: sideA
and sideB. These will both be doubles and refer to the length of the
other two sides besides the base. These should all be stored inside
the Triangle class. The Triangle can then use the following formulas
to implement its methods:
- $A = \frac{1}{2} \cdot base \cdot height$
- $P = base + sideA + sideB$
Main
You can use the following Main class to test this program. It makes a list of Shape objects, and calls the display method on all of them. It then makes a list of Polygon objects and prints their perimeter.
import java.util.ArrayList;
public class Lab11 {
public static void main(String args[]) {
/* make a big list of shapes */
ArrayList<Shape> shapes = new ArrayList<Shape>();
shapes.add(new Circle("Circle 1", 5));
shapes.add(new Circle("Circle 2", 12));
shapes.add(new Circle("Circle 3", 24));
shapes.add(new Circle("Circle 4", 2));
shapes.add(new Circle("Circle 5", 18));
shapes.add(new Rectangle("Rectangle 1", 3, 5));
shapes.add(new Rectangle("Rectangle 2", 12, 15));
shapes.add(new Rectangle("Rectangle 3", 8, 8));
shapes.add(new Rectangle("Rectangle 4", 45, 17));
shapes.add(new Rectangle("Rectangle 5", 20, 36));
shapes.add(new Triangle("Triangle 1", 5, 18, 2, 6));
shapes.add(new Triangle("Triangle 2", 19, 22, 9, 11));
shapes.add(new Triangle("Triangle 3", 80, 9, 30, 6));
shapes.add(new Triangle("Triangle 4", 25, 42, 13, 9.5));
shapes.add(new Triangle("Triangle 5", 26, 56, 40, 11));
for (Shape s: shapes) {
s.display();
}
/* now we have some plain polygons */
ArrayList<Polygon> polys = new ArrayList<Polygon>();
polys.add(new Triangle("Triangle 6", 2, 3, 4, 5));
polys.add(new Triangle("Triangle 7", 12, 30, 44, 35));
polys.add(new Triangle("Triangle 8", 9, 8, 14, 21));
polys.add(new Rectangle("Rectangle 6", 20, 30));
polys.add(new Rectangle("Rectangle 7", 11, 6.5));
polys.add(new Rectangle("Rectangle 8", 20.2, 88));
for (Polygon p: polys) {
System.out.println("Perimeter = " + p.getPerimeter());
}
}
}
Example Run
When you are done, the output of the program should look like the following:
Circle 1 (78.53981633974483) Circle 2 (452.3893421169302) Circle 3 (1809.5573684677208) Circle 4 (12.566370614359172) Circle 5 (1017.8760197630929) Rectangle 1 (15.0) Rectangle 2 (180.0) Rectangle 3 (64.0) Rectangle 4 (765.0) Rectangle 5 (720.0) Triangle 1 (45.0) Triangle 2 (209.0) Triangle 3 (360.0) Triangle 4 (525.0) Triangle 5 (728.0) Perimeter = 11.0 Perimeter = 91.0 Perimeter = 44.0 Perimeter = 100.0 Perimeter = 35.0 Perimeter = 216.4
Submitting
When you are done, please submit the Java code files for each of the shapes under the assignment in Canvas.