Question

In: Computer Science

Ch 4 Program 3  – Geometry Calculator Please adhere to the Standards for Programming Assignments and the...

Ch 4 Program 3  – Geometry Calculator

Please adhere to the Standards for Programming Assignments and the Java Coding Guideline.

:

  • List of source code with comments to document
  • Test output listed as comments at the end of your program

GeometryCalculator.java (15 pts)

Write a program that displays the following menu:

  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

Prompt the user to enter a menu choice.

Then based on the user’s choice use a switch statement to do the following:

  • If the user enters 1, then you should prompt for the radius of the circle and display its area. Use MATH.PI for the value of PI.
  • If the user enters 2, then prompt for the length and width of the rectangle, and then display the rectangle’s area.
  • If the user enters 3, then prompt for the base and height of the triangle, and then display its area.
  • If the user enters 4, then prompt for the radius and height of the cylinder, and then display its volume.
  • If the user enters 5, the program should end.
  • Note that the program also ends after displaying the area or the volume of figures.

submit GeometryCalculator.java on Canvas.

Input validation:

  • Display an error message if the user enters a menu number outside the range of 1 through 4 and ends the program.
  • Only accept positive values for the circle’s radius, the rectangle’s length or width, or the triangle’s base or height. In such a case, display an error message and end the program.

                  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!

Solutions

Expert Solution

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:-


Related Solutions

Assignment #3 – Geometry Calculator Assignment Objectives Task #1 void Methods 1. Name the program file...
Assignment #3 – Geometry Calculator Assignment Objectives Task #1 void Methods 1. Name the program file Geometry.java. This program will compile, but, when you run it, it doesn’t appear to do anything except wait. That is because it is waiting for user input, but the user doesn’t have the menu to choose from yet. We will need to create it. 2. Below the main method, but in the Geometry class,create a static method called printMenu that has no parameter list...
Ch 4 Program 5: RockPaperScissors.java                                    &
Ch 4 Program 5: RockPaperScissors.java                                                  Please adhere to the Standards for Programming Assignments and the Java Coding Guidelines. In the game Rock Paper Scissors, two players simultaneously choose one of three options: rock, paper, or scissors. If both players choose the same option, then the result is a tie. However, if they choose differently, the winner is determined as follows Rock beats scissors because a rock can break a part of scissors Scissors beats paper because scissors can cut paper...
---------------------------------------------------------------------------------------------------------------- C++ //code a program for the following the given Instruction. Geometry Calculator 1. Calculate the...
---------------------------------------------------------------------------------------------------------------- C++ //code a program for the following the given Instruction. 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): If the user enters 1, the program should ask for the radius of the circle and then display its area. Use 3.14159 for pi. #03   If the user enters 2, the program should ask for the length and width of...
C++ Write a program that displays the follow menu: Geometry Calculator    1. Calculate the Area...
C++ Write a program that displays the follow menu: 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): If the user enters 1, the program should ask for the radius of the circle then display it's area using the following formula: area = PIr2 Use 3,14159 for PI and the radius of the circle for r. If the...
Time Calculator – Intro To Programming - JAVA Write a program that asks the user to...
Time Calculator – Intro To Programming - JAVA Write a program that asks the user to enter a number of seconds. • There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds. • There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater than or equal to...
Ch 7-3 Exercises & Problems Hide or show questions eBook Calculator Print Item Effect of Errors...
Ch 7-3 Exercises & Problems Hide or show questions eBook Calculator Print Item Effect of Errors in Physical Inventory Fonda Motorcycle Shop sells motorcycles, ATVs, and other related supplies and accessories. During the taking of its physical inventory on December 31, 20Y8, Fonda Motorcycle Shop incorrectly counted its inventory as $294,380 instead of the correct amount of $282,600. Enter all amounts as positive numbers. a.  State the effect of the error on the December 31, 20Y8, balance sheet of Fonda Motorcycle...
3. Refer to Ch 4, pg 132- 4 in BM. We are going to calculate the...
3. Refer to Ch 4, pg 132- 4 in BM. We are going to calculate the velocity of the Earth today (let’s say 7 Feb 2020) using the vis viva equation. Take a deep breath... (a) Calculate the "mean anomaly" of the Earth M. This is the angle from perihelion to the position of Earth as if the Earth were in a circular orbit. You have to look up the perihelion of the Earth. See pg 34 in BM. This...
The thermal decomposition of dimethyl ether CH 3 2 O g CH 4 g H 2...
The thermal decomposition of dimethyl ether CH 3 2 O g CH 4 g H 2 g CO g is to be carried out in an isothermal 2.00-liter laboratory reactor at 600°C. The reactor is charged with pure dimethyl ether at a pressure of 350 torr. After about two hours, the reactor pressure is 875 torr. (a) Has the reaction proceeded to completion at the end of the two-hour period? If not, what percentage of the dimethyl ether has decomposed?...
Ch 8-3 Exercises & Problems Hide or show questions eBook Calculator Print Item Internal Controls Ramona's...
Ch 8-3 Exercises & Problems Hide or show questions eBook Calculator Print Item Internal Controls Ramona's Clothing is a retail store specializing in women's clothing. The store has established a liberal return policy for the holiday season in order to encourage gift purchases. Any item purchased during November and December may be returned through January 31, with a receipt, for cash or exchange. If the customer does not have a receipt, cash will still be refunded for any item under $75. If...
Biological Anthropology: Ch. 3- Genetics: Reproducing Life & Producing Variation Ch. 4-Genes Their Evolution: Population Genetics...
Biological Anthropology: Ch. 3- Genetics: Reproducing Life & Producing Variation Ch. 4-Genes Their Evolution: Population Genetics (Please TYpe) Chapter 4 1. Microevolution, macroevolution 2. Reproductive isolation 3. Hardy-Weinberg Law 4. Four forces of evolution 5. Mutation types 6. Three patterns of natural selection 7. Admixture, founder’s effect Chapter 5 1. Race: historical and modern concepts, issues with the concept, etc. 2. Blumenbach, Boas, Lewontin 3. Four levels of human adaptation 4. Terms related to adaptation (stress, homeostasis, plasticity, functional adaptation)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT