0% found this document useful (0 votes)
20 views1 page

Q 2)

Uploaded by

maneprerna1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views1 page

Q 2)

Uploaded by

maneprerna1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

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();
}
}

You might also like