Home CPSC 240

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:


 

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.

Copyright © 2024 Ian Finlayson | Licensed under a Creative Commons BY-NC-SA 4.0 License.