63 lines
		
	
	
	
		
			1.7 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
			
		
		
	
	
			63 lines
		
	
	
	
		
			1.7 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
import java.util.Scanner;
 | 
						|
 | 
						|
// Define the interface with a method to compute the area
 | 
						|
interface AreaComputable {
 | 
						|
    double computeArea();
 | 
						|
}
 | 
						|
 | 
						|
// Rectangle class implementing AreaComputable interface
 | 
						|
class Rectangle implements AreaComputable {
 | 
						|
 | 
						|
    private double length;
 | 
						|
    private double width;
 | 
						|
 | 
						|
    // Constructor to initialize rectangle dimensions
 | 
						|
    public Rectangle(double length, double width) {
 | 
						|
        this.length = length;
 | 
						|
        this.width = width;
 | 
						|
    }
 | 
						|
 | 
						|
    // Implement computeArea() method
 | 
						|
    public double computeArea() {
 | 
						|
        return length * width;
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
// Circle class implementing AreaComputable interface
 | 
						|
class Circle implements AreaComputable {
 | 
						|
 | 
						|
    private double radius;
 | 
						|
 | 
						|
    // Constructor to initialize circle radius
 | 
						|
    public Circle(double radius) {
 | 
						|
        this.radius = radius;
 | 
						|
    }
 | 
						|
 | 
						|
    // Implement computeArea() method
 | 
						|
    public double computeArea() {
 | 
						|
        return Math.PI * radius * radius;
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
// Main class to run the program
 | 
						|
public class RecCircleInterface {
 | 
						|
 | 
						|
    public static void main(String[] args) {
 | 
						|
        Scanner scanner = new Scanner(System.in);
 | 
						|
 | 
						|
        // Input and compute area of the rectangle
 | 
						|
        System.out.println("Enter length and width of the rectangle:");
 | 
						|
        double length = scanner.nextDouble();
 | 
						|
        double width = scanner.nextDouble();
 | 
						|
        Rectangle rectangle = new Rectangle(length, width);
 | 
						|
        System.out.println("Area of Rectangle: " + rectangle.computeArea());
 | 
						|
 | 
						|
        // Input and compute area of the circle
 | 
						|
        System.out.println("Enter radius of the circle:");
 | 
						|
        double radius = scanner.nextDouble();
 | 
						|
        Circle circle = new Circle(radius);
 | 
						|
        System.out.println("Area of Circle: " + circle.computeArea());
 | 
						|
 | 
						|
        scanner.close();
 | 
						|
    }
 | 
						|
}
 |