Shapes area

Report a typo

You have five classes: Shape, Triangle, Circle, Square, and Rectangle. The class Shape has a method area(). This method does nothing. Override the method in all subclasses. Overridden methods should return an area of a particular figure. Use class fields for this.

The area of a triangle is S=bh/2 S = bh / 2 where h h is the height of the triangle, b b is the base of the triangle.

The area of a circle is S=πR2 S = \pi R^2 where R R is the radius of the circle. For π \pi use the Math.PI constant.

The area of a square is S=s2 S = s^2 where s s is the length of the side of the square.

The area of a rectangle is S=wh S = wh where w w is the width of the rectangle and h h the height of the rectangle.

Write a program in Java 17
class Shape {
public double area() {
return 0;
}
}

class Triangle extends Shape {
double height;
double base;

// override the method here
}

class Circle extends Shape {
double radius;

// override the method here
}

class Square extends Shape {
double side;

// override the method here
}

class Rectangle extends Shape {
double width;
double height;

// override the method here
}
___

Create a free account to access the full topic