Create an abstract class called Shape with an abstract method area(). Implement two classes Rectangle and Circle that extend the Shape class and implement the area() method. Given the dimensions of a rectangle and a circle, calculate and print the area of each shape.
Computer scienceProgramming languagesJavaCode organizationObject-oriented programmingClass hierarchiesInterfaces and abstract classes
Abstract class
Calculating areas of rectangles and circles
Report a typo
Sample Input 1:
5
3
4Sample Output 1:
15.0
50.26548245743669Sample Input 2:
10
8
6Sample Output 2:
80.0
113.09733552923255Write a program in Java 25
import java.util.Scanner;
// Define the abstract Shape class with an abstract area() method
// Implement the Rectangle class that extends Shape
// Implement the Circle class that extends Shape
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Read the dimensions of the rectangle
double length = scanner.nextDouble();
double width = scanner.nextDouble();
// Read the radius of the circle
double radius = scanner.nextDouble();
// Create instances of Rectangle and Circle
// Calculate and print the area of the rectangle
// Calculate and print the area of the circle
scanner.close();
}
}
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.