Answer to Question #258019 in Java | JSP | JSF for pinno

Question #258019

Write a program that displays the following 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 and then display its area. formula: area π r 2 Use 3.14159 for π and the radius of the circle for r.

If the user enters 2, the program should ask for the length and width of the rectangle and then display the rectangle’s area. formula: area = length * width

 If the user enters 3 the program should ask for the length of the triangle’s base and its height, and then display its area. formula: area = base * height * .5

If the user enters 4, the program should end.

Input Validation: Display an error message if the user enters a number outside the range of 1 through 4 when selecting an item from the menu. Do not accept negative values for the circle’s radius, the rectangle’s length or width, or the triangle’s base or height.  



1
Expert's answer
2021-10-28T06:58:59-0400
import java.util.Random;
import java.util.Scanner;


public class App {


	public static void main(String[] args) {


		Scanner keyBoard = new Scanner(System.in);
		int choice = 0;


		while (choice != 4) {


			System.out.println("Geometry Calculator");
			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. Quit");
			System.out.print("Enter your choice (1-4): ");
			choice = keyBoard.nextInt();


			switch (choice) {
			case 1:// If the user enters 1, the program should ask for the radius of the circle and
					// then display its area. formula: area PI r 2 Use 3.14159 for PI and the radius
					// of the circle for r.
			{
				double radius = -1;
				while (radius <= 0) {
					System.out.print("Enter the radius of the circle >0: ");
					radius = keyBoard.nextDouble();
				}
				double area = 3.14159 * radius * radius;
				System.out.printf("\nThe area of the circle: %.2f\n\n", area);
			}
				break;
			case 2:// If the user enters 2, the program should ask for the length and width of the
					// rectangle and then display the rectangle’s area. formula: area = length *
					// width
			{


				double length = -1;
				while (length <= 0) {
					System.out.print("Enter the length of the rectangle >0: ");
					length = keyBoard.nextDouble();
				}
				double width = -1;
				while (width <= 0) {
					System.out.print("Enter the width of the rectangle >0: ");
					width = keyBoard.nextDouble();
				}
				double area = length * width;
				System.out.printf("\nThe area of the rectangle: %.2f\n\n", area);


			}
				break;
			case 3:// If the user enters 3 the program should ask for the length of the triangle’s
					// base and its height, and then display its area. formula: area = base * height
					// * .5
			{
				double base = -1;
				while (base <= 0) {
					System.out.print("Enter the base of the triangle >0: ");
					base = keyBoard.nextDouble();
				}
				double height = -1;
				while (height <= 0) {
					System.out.print("Enter the height of the triangle >0: ");
					height = keyBoard.nextDouble();
				}
				double area = 0.5 * base * height;
				System.out.printf("\nThe area of the triangle: %.2f\n\n", area);
			}
				break;
			case 4:// If the user enters 4, the program should end.
				break;
			default:
				System.out.println("Wrong menu item.");
				break;
			}


		}


		keyBoard.close();


	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS