Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Java
  3. Question
Java

How can I implement shape class java?

Asked by Kevin Taylor Oct 10, 2022 1.5K views 2 answers
Share

About this question

To learn more about OOP, @nhgrif challenged me to implement a Shape abstract class (more details in code remarks below. Here is how I did it. Any and all advice appreciated!


Shape.java

/* nhgrif says:
 * Phrancis ready for inheritance/polymorphism?
 * Given the following abstract class:
 *
 *  public abstract class Shape {
 *      public abstract double area();
 *      public abstract double perimeter();
 *  }
 *
 * Implement a Circle, Triangle, and Rectangle class which extends the class Shape.
 * Ex: public class Circle extends Shape ... etc
 */
public abstract class Shape {
    public abstract double area();
    public abstract double perimeter();
}
Rectangle.java
public class Rectangle extends Shape {
    private final double width, length; //sides
    public Rectangle() {
        this(1,1);
    }
    public Rectangle(double width, double length) {
        this.width = width;
        this.length = length;
    }
    @Override
    public double area() {
        // A = w * l
        return width * length;
    }
    @Override
    public double perimeter() {
        // P = 2(w + l)
        return 2 * (width + length);
    }
}
Circle.java
public class Circle extends Shape {
    private final double radius;
    final double pi = Math.PI;
    public Circle() {
        this(1);
    }   
    public Circle(double radius) {
        this.radius = radius;
    }
    @Override
    public double area() {
        // A = π r^2
        return pi * Math.pow(radius, 2);
    }
    public double perimeter() {
        // P = 2πr
        return 2 * pi * radius;
    }
}
Triangle.java
public class Triangle extends Shape {
    private final double a, b, c; // sides
    public Triangle() {
        this(1,1,1);
    }
    public Triangle(double a, double b, double c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }
    @Override
    public double area() {
        // Heron's formula:
        // A = SquareRoot(s * (s - a) * (s - b) * (s - c)) 
        // where s = (a + b + c) / 2, or 1/2 of the perimeter of the triangle 
        double s = (a + b + c) / 2;
        return Math.sqrt(s * (s - a) * (s - b) * (s - c));
    }
    @Override
    public double perimeter() {
        // P = a + b + c
        return a + b + c;
    }
}

And this is what I used to test it all, which it all works as intended:

TestShape.java

public class TestShape {

    public static void main(String[] args) {


      // Rectangle test

        double width = 5, length = 7;

        Shape rectangle = new Rectangle(width, length);

        System.out.println("Rectangle width: " + width + " and length: " + length

                + "nResulting area: " + rectangle.area()

                + "nResulting perimeter: " + rectangle.perimeter() + "n");


        // Circle test

        double radius = 5;

        Shape circle = new Circle(radius);

        System.out.println("Circle radius: " + radius

            + "nResulting Area: " + circle.area()

            + "nResulting Perimeter: " + circle.perimeter() + "n");


        // Triangle test

        double a = 5, b = 3, c = 4;

        Shape triangle = new Triangle(a,b,c);

        System.out.println("Triangle sides lengths: " + a + ", " + b + ", " + c

                + "nResulting Area: " + triangle.area()

                + "nResulting Perimeter: " + triangle.perimeter() + "n");

    }

}


Your answer

2 Answers

Irma Wilkinson Latest answer

Answered on Jun 24, 2025

OOP organizes a computer program into basic, reusable blueprints of code or "classes". This is quite interesting Block Blast Unblocked if you dig deeper than the current knowledge that is being implemented.

Was this helpful?

More Java discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Java Blogs

Guides, tips and career advice on Java from JanBask experts.