Write an Area class that has simply three overloaded static methods: getArea for
ID: 3692478 • Letter: W
Question
Write an Area class that has simply three overloaded static methods: getArea for calculating the areas of the following geometric shapes: Circles Rectangles Cuboids Here are the formulas for calculating the area of the shapes. Area of a circle: Area = pir^2, where, pi is Math.PI and r is the circle's radius. Area of rectangle: Area = Width times Length Area of cuboid: Area = 2 (length times height + width times height + length times width) Because the three methods are to be overloaded, they should each have the same name getArea, but different parameter lists. Demonstrate the class in a complete program.Explanation / Answer
AreaDemo.java
import java.util.Scanner;
public class AreaDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
System.out.println("Please enter radius of circle");
double radius = scan.nextDouble();
System.out.println("Circle Area : "+Area.getArea(radius));
System.out.println("Please enter the width and length of Ractangle");
double width = scan.nextDouble();
double length = scan.nextDouble();
System.out.println("Ractangle Area : "+Area.getArea(width, length));
System.out.println("Please enter the width, length and height of Cuboid");
double cubWidth = scan.nextDouble();
double cubLength = scan.nextDouble();
double cubHeight = scan.nextDouble();
System.out.println("Cuboid Area : "+Area.getArea(cubWidth, cubLength, cubHeight));
}
}
Area.java
public class Area {
public static final double PI = 3.14;
public static double getArea(double radius){
return PI * radius * radius;
}
public static double getArea(double width, double length){
return width * length;
}
public static double getArea(double width, double length, double height){
return 2 * (width * length + length * height + height * width);
}
}
Output:
Please enter radius of circle
2.2
Circle Area : 15.197600000000003
Please enter the width and length of Ractangle
2.2 3.3
Ractangle Area : 7.26
Please enter the width, length and height of Cuboid
2.2 3.3 4.4
Cuboid Area : 62.92
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.