In: Computer Science
Ch 4 Program 3 – Geometry Calculator
Please adhere to the Standards for Programming Assignments and the Java Coding Guideline.
:
GeometryCalculator.java (15 pts)
Write a program that displays the following menu:
Prompt the user to enter a menu choice.
Then based on the user’s choice use a switch statement to do the following:
submit GeometryCalculator.java on Canvas.
Input validation:
Hint:
Do not try to write the entire program in one go. It is much easier to write a small piece and test it, then write another small piece and test it. For example, start by writing just a skeleton with input and output. Then add the code to do the normal computation, without any data validation. Then add each additional data validation rule, one at a time. You should test your program with simple test inputs to check that you handle each case. Good luck!
Test data: Your included output must show your program running with the following sequence.
Test Menu choice 1. Radius = 3.7 then
Menu choice 2. length = 5.5, width = 3.
Menu choice 4. Radius = 3.5, height = 8.
Menu choice 9. Should generate an error message.
Menu choice 3. base = -6.2, height = 4.Should generate an error message
Menu choice 5. Quit the program.
Example 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. Calculate the volume of a cylinder
5. Quit
Enter your choice (1-5): 1
Enter the circle's radius: 3.7
The area is 43.0084
Thanks for using the Geometry Calculator!
***Geometry Calculator***
1. Calculate the area of a Circle
2. Calculate the area of a Rectangle
3. Calculate the area of a Triangle
4. Calculate the volume of a cylinder
5. Quit
Enter your choice (1-5): 2
Enter the rectangle's length: 5.5
Enter the rectangle's width: 3
The area is 16.5
Thanks for using the Geometry Calculator!
---------------------------------------------------------------------
***Geometry Calculator***
1. Calculate the area of a Circle
2. Calculate the area of a Rectangle
3. Calculate the area of a Triangle
4. Calculate the volume of a cylinder
5. Quit
Enter your choice (1-5): 4
Enter the cylinder's radius: 3.5
Enter the cylinder’s height: 8
The volume is 307.8760
Thanks for using the Geometry Calculator!
------------------------------------------------------------------------
***Geometry Calculator***
1. Calculate the area of a Circle
2. Calculate the area of a Rectangle
3. Calculate the area of a Triangle
4. Calculate the volume of a cylinder
5. Quit
Enter your choice (1-5): 9
You may only enter 1, 2, 3, 4 or 5.
Thanks for using the Geometry Calculator!
-------------------------------------------------------------------------
***Geometry Calculator***
1. Calculate the area of a Circle
2. Calculate the area of a Rectangle
3. Calculate the area of a Triangle
4. Calculate the volume of a cylinder
5. Quit
Enter your choice (1-5): 3
Enter the length of the base: -6.2
Enter the triangle's height: 4
Only positive values are accepted for the base.
Thanks for using the Geometry Calculator!
-------------------------------------------------------------------------
***Geometry Calculator***
1. Calculate the area of a Circle
2. Calculate the area of a Rectangle
3. Calculate the area of a Triangle
4. Calculate the volume of a cylinder
5. Quit
Enter your choice (1-5): 5 Quitting program now.
Thanks for using the Geometry Calculator!
import java.util.*;
import java.lang.*;
public class Main
{
public static void areaCircle(double r)
{
double a = Math.PI * r * r; //Caclulating the Area of Circle
System.out.printf("Area of Circle is:%.3f",a); //Printing the Area
of Circle
}
public static void areaRectangle(double l,double w){
double a = l * w; //Caclulating the Area of Rectangle
System.out.printf("Area of Rectangle is:%.3f",a); //Printing the
Area of Rectangle
}
public static void areaTriangle(double b,double h){
double a = 0.5 * b * h; //Caclulating the Area of Triangle
System.out.printf("Area of Triangle is:%.3f",a); //Printing the
Area of Triangle
}
public static void volumeCylinder(double r,double h){
double v = Math.PI * r * r * h; //Caclulating the Volume of
Cylinder
System.out.printf("Volume of Cylinder is:%.3f",v); //Printing the
Volume of Cylinder
}
public static void main(String[] args) {
int ch;
Scanner sc = new
Scanner(System.in);
System.out.println("***Geometry
Calculator***"); //Printing the Menu
System.out.println("1.Calculate the
area of a Circle");
System.out.println("2.Calculate the
area of a Rectangle");
System.out.println("3.Calculate the
area of a Triangle");
System.out.println("4.Calculate the
volume of a Cylinder");
System.out.println("5.Quit");
System.out.println("Enter your
choice(1-5): ");
ch = sc.nextInt(); //Taking Choice
from user
switch (ch)
{
case 1: System.out.println("Enter
the Radius of a Circle: ");
double r = sc.nextDouble(); //Taking Radius from user
if(r<0)
System.out.println("Invalid Radius");
else
areaCircle(r);
break;
case 2:System.out.println("Enter
the Length: ");
double l = sc.nextDouble();
//Taking Length from user
System.out.println("Enter the
Width: ");
double w = sc.nextDouble();
//Taking Width from user
if(l<0 || w<0)
System.out.println("Invalid Length
or Width");
else
areaRectangle(l,w);
break;
case 3:System.out.println("Enter
the Base: "); //Taking Base from user
double b = sc.nextDouble();
System.out.println("Enter the
Height: "); //Taking Height from user
double h = sc.nextDouble();
if(b<0 || h<0)
System.out.println("Invalid Base or
Height");
else
areaTriangle(b,h);
break;
case 4:System.out.println("Enter
the Radius: "); //Taking Radius from user
double r1 = sc.nextDouble();
System.out.println("Enter the
Height: "); //Taking Height from user
double h1 = sc.nextDouble();
if(r1<0 || h1<0)
System.out.println("Invalid Radius
or Height");
else
volumeCylinder(r1,h1);
break;
case 5:System.exit(0);
break;
default:System.out.println("Invalid
Choice"); //Default Statement
}
System.out.println("\nThanks for
using Geometry Calculator!");
}
}
Output:-