Q 2) Define abstract class shape with abstract method area ().
Write a java
program to calculate area of circle
abstract class Shape {
abstract double area();
}
class Circle extends Shape {
private double radius;
Circle(double radius) {
this.radius = radius;
}
@Override
double area() {
return Math.PI * radius * radius;
}
public static void main(String[] args) {
Circle circle = new Circle(5); // Example value for radius
System.out.println("Area of the circle: " + circle.area());
}
}
OR
import java.util.Scanner;
public class CalculateCircleArea {
public static void main(String[] args) {
// Create a Scanner for user input
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter the radius
System.out.print("Enter the radius of the circle: ");
double radius = scanner.nextDouble();
// Calculate the area of the circle
double area = Math.PI * Math.pow(radius, 2);
// Display the calculated area
System.out.printf("The area of the circle is: %.2f square units%n", area);
// Close the scanner
scanner.close();
}
}