MIT-Curricular/OOP/Java/Lab/Week8/RecCircleAbstract/RecCircleAbstract.java

58 lines
1.2 KiB
Java
Raw Normal View History

import java.util.Scanner;
abstract class Shape {
abstract double calculateArea();
}
class Rectangle extends Shape {
double length;
double width;
Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
double calculateArea() {
return length * width;
}
}
class Circle extends Shape {
double radius;
Circle(double radius) {
this.radius = radius;
}
double calculateArea() {
return Math.PI * radius * radius;
}
}
class RecCircleAbstract {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter rectangle length: ");
double length = scanner.nextDouble();
System.out.print("Enter rectangle width: ");
double width = scanner.nextDouble();
Rectangle rectangle = new Rectangle(length, width);
System.out.println("Rectangle area: " + rectangle.calculateArea());
System.out.print("Enter circle radius: ");
double radius = scanner.nextDouble();
Circle circle = new Circle(radius);
System.out.println("Circle area: " + circle.calculateArea());
scanner.close();
}
}