Shape hierarchy

Report a typo

You have an abstract class Shape with two abstract methods: getPerimeter() and getArea(). See the provided code template.

You need to declare and implement three classes: Triangle, Rectangle and Circle. The classes must extend the Shape class and implement all abstract methods. To implement the methods the standard class Math may help you.

  • The class Triangle must have a constructor with three double arguments for setting the length of each side. To calculate the area of this shape you may use Heron's formula.
  • The class Rectangle must have a constructor with two double arguments for setting the length of different sides.
  • The class Circle must have a constructor with one double argument for setting the radius. You may use Math.PI as the PI constant or you can declare it yourself.

Note: do NOT make your classes public.

Examples

A circle has a radius = 10: perimeter = 62.831853..., area = 314.159265....

A triangle has three sides a = 3, b = 4 and c = 5: perimeter = 12.0, area = 6.0.

A rectangle has two different sides a = 5, b = 10: perimeter = 30.0, area = 50.0.

Write a program in Java 17
abstract class Shape {

abstract double getPerimeter();

abstract double getArea();
}
___

Create a free account to access the full topic