1. Area Class Write a class that has three overloaded static methods for calcula
ID: 3859763 • Letter: 1
Question
1. Area Class
Write a class that has three overloaded static methods for calculating the areas of the
following geometric shapes:
- circles
- rectangles
- cylinders
Here are the formulas for calculating the area of the shapes.
Area of a circle: Area = r2
where p is Math.PI and r is the circle's radius
Area of a rectangle: Area = Width x Length
Area of a cylinder: Area = r2 h
where p is Math.PI, r is the radius of the cylinder's base, and
h is the cylinder's height
Because the three methods are to be overloaded, they should each have the same name, but
different parameter lists. Demonstrate the class in a complete program.
This is what I have so far
public class area {
public static double Area (double rad){
return Math.PI*rad*rad;
}
public static double Area (double length,double width){
return length*width;
}
public static double Area (double radi,double height){
return Math.PI*rad*rad*height;
}
}
import java.util.Scanner;
public class main{
public static void main(String args[]){
Scanner input = new Scanner();
System.out.println("Enter·radius·to·calculate·circle·area:");
double rad = input.nextDouble();
System.out.println("·Enter·width·to·calculate·rectangle·area:");
double width = input.nextDouble();
System.out.println("·Enter·length·to·calculate·rectangle·area:");
double length = input.nextDouble();
System.out.println("·Enter·base·radius·to·calculate·cylinder·area:");
double radi = input.nextDouble();
System.out.println("·Enter·height·to·calculate·cylinder·area:");
double height = input.nextDouble();
System.out.println("The·area·of·the·circle·is:·"+ area.Area(double radi);
System.out.println("The·area·of·the·rectangle·is:·"+ area.Area(double length,double width);
System.out.println("The·area·of·the·cylinder·is:·"+ area.Area(double radi,double height);
}
}
This is the error I am getting
SAMPLE RUN #3: java Area Interactive Session Hide Invisibles Highlight: None Highlight None *Show Highlighted Calculator. === Enter.radius.to calculate.circle.area:2.0 Enter width.to calculate.rectangle.area:3.5 . Enter . length-to-calculate . rectangle . area : 4·0- Enter.base.radius.to.calculate.cylinder area:8.0+ Enter-height . to . calculate-cylinder area: 5 . 7 Results:+ The area of the circle.is: 12.57+ The area of.the rectangle.is: 14.00 The area-of-the-cylinder-is:.1146.05Explanation / Answer
import java.util.*;
public class Area
{
public static double area(double radius)
{
return (double)(Math.PI*Math.pow(radius,2));
} // end return (double)(Math.PI*Math.pow(radius,2));
public static int area(int length, int width)
{
return (length * width);
} // end return (length * width);
public static double area(double radius, double height)
{
return (double)(Math.PI*Math.pow(radius,2)*height);
} // end return (double)(Math.PI*Math.pow(radius,2)*height);
public static void main(String args[])
{
System.out.println("This is a program to calculate the area of the following: circle, rectangle, and cylinder. ");
// AREA OF A CIRCLE
Scanner in = new Scanner(System.in);
System.out.println("Circle:");
System.out.println("Please enter the radius of your circle:");
double radius = in.nextDouble();
System.out.printf("The area of this circle is: %.2f ",Area.area(radius));
// AREA OF A RECTANGLE
System.out.println(" Rectangle:");
System.out.println("Please enter the length of your rectangle:");
int length = in.nextInt();
System.out.println("Please enter the width:");
int width = in.nextInt();
System.out.println("The area of this rectangle is: "+Area.area(length,width));
// AREA OF A CYLINDER
System.out.println(" Cylinder:");
System.out.println("Please enter the radius of your cylinder:");
radius = in.nextDouble();
System.out.println("Please enter the height:");
double height = in.nextDouble();
System.out.printf("The area of your cylinder is: %.2f ",Area.area(radius,height));
} // end main
} // end class Areas
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.