Upload files to "OOP/Java/Lab/Week8/RecCircleInterface"
This commit is contained in:
parent
27ec687ff7
commit
83be8b948d
@ -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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user