Question
Solution
JAVA
import java.util.Scanner;
class Shape {
// Method to calculate the area of a circle (using radius)
public double calculateArea(double radius) {
return Math.PI * radius * radius;
}
// Method to calculate the area of a rectangle (using length and width)
public double calculateArea(double length, double width) {
return length * width;
}
// Method to calculate the area of a square (using side length)
public double calculateArea(int side) {
return side * side;
}
}
public class MethodOverloadingExample {
public static void main(String[] args) {
Shape shape = new Shape();
Scanner scanner = new Scanner(System.in);
System.out.print("radius of the circle: ");
double radius = scanner.nextDouble();
double circleArea = shape.calculateArea(radius);
System.out.printf("Area of the circle: %.2f\n", circleArea);
System.out.print("length of the rectangle: ");
double length = scanner.nextDouble();
System.out.print("width of the rectangle: ");
double width = scanner.nextDouble();
double rectangleArea = shape.calculateArea(length, width);
System.out.printf("Area of the rectangle: %.2f\n", rectangleArea);
System.out.print("side length of the square: ");
int side = scanner.nextInt();
double squareArea = shape.calculateArea(side);
System.out.printf("Area of the square: %.2f\n", squareArea);
scanner.close();
}
}
2/2 test cases passed
2/2 hidden test cases passed