Object-Oriented Programming/Inheritance/Java

Main.java

edit
// This program demonstrates use of the Polygon, Triangle, Rectangle, and Square classes.

class Main {
    public static void main(String[] args) {
        processPolygons();
        processShapes();
    }

    private static void processPolygons() {
        var triangle = new Polygon(new double[]{3, 4, 5});
        printSides("triangle", triangle.getSides());
        System.out.print("triangle perimeter: ");
        System.out.println(triangle.getPerimeter());

        double [] rectangleSides = {4, 2, 4, 2};
        var rectangle = new Polygon(new double[]{4, 2, 4, 2});
        printSides("rectangle", rectangle.getSides());
        System.out.print("rectangle perimeter: ");
        System.out.println(rectangle.getPerimeter());

        var square = new Polygon(new double[]{2, 2, 2, 2});
        printSides("square", square.getSides());
        System.out.print("square perimeter: ");
        System.out.println(square.getPerimeter());
    }

    private static void processShapes() {
        var triangle = new Triangle(new double[]{3, 4, 5});
        printSides("triangle", triangle.getSides());
        System.out.print("triangle perimeter: ");
        System.out.println(triangle.getPerimeter());
        System.out.print("triangle area: ");
        System.out.println(triangle.getArea());

        var rectangle = new Rectangle(new double[]{4, 2, 4, 2});
        printSides("rectangle", rectangle.getSides());
        System.out.print("rectangle perimeter: ");
        System.out.println(rectangle.getPerimeter());
        System.out.print("rectangle area: ");
        System.out.println(rectangle.getArea());

        var square = new Square(new double[]{2, 2, 2, 2});
        printSides("square", square.getSides());
        System.out.print("square perimeter: ");
        System.out.println(square.getPerimeter());
        System.out.print("square area: ");
        System.out.println(square.getArea());
    }

    private static void printSides(String label, double[] sides) {
        System.out.print(label + " sides: ");
        for (var i = 0; i < sides.length; i++) {
            System.out.print(sides[i]);
            System.out.print(" ");
        }
        System.out.println();
    }
}

Polygon.java

edit
// Supports polygon calculations for perimeter and area.

import java.lang.*;

class Polygon {
    private double[] _sides;

    // Creates a polygon object.
    public Polygon(double[] sides) {
        this._sides = sides;
    }

    // Gets the polygon sides.
    public double[] getSides() {
        return this._sides;
    }

    // Gets the polygon perimeter.
    public double getPerimeter() {
        double perimeter = 0.0;

        for (var i = 0; i < this._sides.length; i++) {
            perimeter += this._sides[i];
        }

        return perimeter;
    }
}

class Triangle extends Polygon {
    public Triangle(double[] sides) {
        super(sides);
    }

    // Gets the area of the triangle.
    // References:
    //  https://www.mathsisfun.com/geometry/herons-formula.html
    public double getArea() {
        var s = this.getPerimeter() / 2;
        var a = this.getSides()[0];
        var b = this.getSides()[1];
        var c = this.getSides()[2];
        var area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
        return area;
    }
}

class Rectangle extends Polygon {
    public Rectangle(double[] sides) {
        super(sides);
    }

    // Gets the area of the rectangle.
    public double getArea() {
        var length = this.getSides()[0];
        var width = this.getSides()[1];
        if (length == width) {
            // Allow for sides in any order.
            width = this.getSides()[2];
        }

        var area = length * width;
        return area;
    }
}

class Square extends Polygon {
    public Square(double[] sides) {
        super(sides);
    }

    // Gets the area of the square.
    public double getArea() {
        var side = this.getSides()[0];
        var area = side * side;
        return area;
    }
}

Try It

edit

Copy and paste the code above into one of the following free online development environments or use your own Java compiler / interpreter / IDE.

See Also

edit