Код: Выделить всё
//using the concept of method overloading
public class practice10 {
// Method to calculate the area of a rectangle
public static double calculateArea(double length, double width) {
return length * width;
}
// Method to calculate the area of a square
public static double calculateArea(double side) {
return side * side;
}
// Method to calculate the area of a circle
public static double calculateArea(double radius) {
return Math.PI * radius * radius;
}
public static void main(String[] args) {
// Calculate and display the area of a rectangle
System.out.println("Area of rectangle (length=5, width=4): " + calculateArea(5.0, 4.0));
// Calculate and display the area of a square
System.out.println("Area of square (side=4): " + calculateArea(4.0));
// Calculate and display the area of a circle
System.out.println("Area of circle (radius=3): " + calculateArea(3.0));
}
}
Подробнее здесь: https://stackoverflow.com/questions/783 ... ed-in-java