Write a program titled \"YourLastName_Geometry\" which has the following methods
ID: 3689794 • Letter: W
Question
Write a program titled "YourLastName_Geometry" which has the following methods:
- A static method that accepts the radius of a circle and returns the area of the circle.
- Use the following formula: Area = r 2
- Use Math.PI for and the radius of the circle for r.
- A static method that accepts the length and width of a rectangle and returns the area
of the rectangle. Use the following formula: Area = Length x Width
- A static method that accepts the length of a triangle's base and the triangle's height.
The method should return the area of the triangle. Use the following formula:
Area = Base x Height x 0.5
The methods should display an error message if negative values are used for the circle's
radius, the rectangle's length or width, or the triangle's base or height.
Next, write a driver program titled "YourLastName_Driver" to test the geometry class. The following shows an example interaction of the driver and geometry classes captured in a file by the command
“% script Driver.out” (bolded areas represent the user's input):
Script started on Thu Sep 26 10:23:58 2013
% java Diaz_Driver
Geometry Calculator
1. Calculate the Area of a Circle
2. Calculate the Area of a Rectangle
3. Calculate the Area of a Triangle
4. Quit
Enter your choice (1-4): 1
Enter the radius of the circle: 9
The area of the circle is: 254.47
Geometry Calculator
1. Calculate the Area of a Circle
2. Calculate the Area of a Rectangle
3. Calculate the Area of a Triangle
4. Quit Enter your choice (1-4): 4
% exit
script done on Thu Sep 26 10:24:17 2013
Display an error message if the user enters a number outside the range of 1 through 4 when selecting an item from the menu.
Run the program with different inputs and capture all interaction in a file using the script command.
Explanation / Answer
Diaz_Driver.java
import java.util.Scanner;
public class Diaz_Driver {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
System.out.println("Geometry Calculator");
while(true){
System.out.println("1. Calculate the Area of a Circle 2. Calculate the Area of a Rectangle 3. Calculate the Area of a Triangle 4. Quit");
System.out.println("Enter your choice (1-4): ");
int choice = scan.nextInt();
if(choice == 1){
System.out.println("Please enter the radius of Circle");
double radius = scan.nextDouble();
double area = getCircleArea(radius);
System.out.println("The area of the Circle is: "+area);
}
else if(choice == 2){
System.out.println("Please enter the length of Rectangle");
double length = scan.nextDouble();
System.out.println("Please enter the width of Rectangle");
double width = scan.nextDouble();
double area = getRectangleArea(length, width);
System.out.println("The area of the Rectangle is: "+area);
}
else if(choice == 3){
System.out.println("Please enter the base of Triangle");
double base = scan.nextDouble();
System.out.println("Please enter the height of Triangle");
double height = scan.nextDouble();
double area = getTriangleArea(base, height);
System.out.println("The area of the Rectangle is: "+area);
}
else if(choice == 4){
System.out.println("Exit");
break;
}
else{
System.out.println("Error: The range should be between 1 and 4");
}
}
}
public static double getCircleArea(double radius){
return 2 * Math.PI * radius;
}
public static double getRectangleArea(double length, double width){
return length * width;
}
public static double getTriangleArea(double base, double height){
return base * height * 0.5;
}
}
Output:
Geometry Calculator
1. Calculate the Area of a Circle
2. Calculate the Area of a Rectangle
3. Calculate the Area of a Triangle
4. Quit
Enter your choice (1-4):
1
Please enter the radius of Circle
9
The area of the Circle is: 56.548667764616276
1. Calculate the Area of a Circle
2. Calculate the Area of a Rectangle
3. Calculate the Area of a Triangle
4. Quit
Enter your choice (1-4):
2
Please enter the length of Rectangle
2.2
Please enter the width of Rectangle
3.3
The area of the Rectangle is: 7.26
1. Calculate the Area of a Circle
2. Calculate the Area of a Rectangle
3. Calculate the Area of a Triangle
4. Quit
Enter your choice (1-4):
3
Please enter the base of Triangle
4.4
Please enter the height of Triangle
5.5
The area of the Rectangle is: 12.100000000000001
1. Calculate the Area of a Circle
2. Calculate the Area of a Rectangle
3. Calculate the Area of a Triangle
4. Quit
Enter your choice (1-4):
5
Error: The range should be between 1 and 4
1. Calculate the Area of a Circle
2. Calculate the Area of a Rectangle
3. Calculate the Area of a Triangle
4. Quit
Enter your choice (1-4):
4
Exit
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.