diff --git a/OOP/Java/Lab/Week8/RecCircleInterface/RecCircleInterface.java b/OOP/Java/Lab/Week8/RecCircleInterface/RecCircleInterface.java new file mode 100644 index 0000000..1553334 --- /dev/null +++ b/OOP/Java/Lab/Week8/RecCircleInterface/RecCircleInterface.java @@ -0,0 +1,63 @@ +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(); + } +}