Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a menu driven program (that means Switch statements) that displays the fol

ID: 3674186 • Letter: W

Question

Write a menu driven program (that means Switch statements) 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.

Use the following formula: Area = p * r2 Use 3.14159 for p 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.

Use the following 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.

Use the following formula: Area = base * height * 0.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. Keep showing him the menu and ask for a valid choice till he enters one.

- Also, do not accept negative values for the circle’s radius, the rectangle’s length or width, or the triangle’s base or height. Keep asking him to enter a valid value for that invalid value.

Explanation / Answer

Find the below program.

package com;

import java.util.Scanner;

public class Calculator {
   static Scanner s=new Scanner(System.in);
   public static void main(String[] args) {
       int operationValue = 0;
       System.out.println("Please enter a value for performing any of the operation given below: 1. Calculate the Area of a Circle 2. Calculate the Area of a Rectangle 3. Calculate the Area of a Triangle 4. Quit ");
       operationValue=s.nextInt();
       while(operationValue<1||operationValue>4)
       {
       System.out.println("Number outside the range of 1 through 4, Please input again");
       System.out.println("Please enter a value for performing any of the operation given below: 1. Calculate the Area of a Circle 2. Calculate the Area of a Rectangle 3. Calculate the Area of a Triangle 4. Quit ");
       operationValue=s.nextInt();
       }
       switch(operationValue){
       case 1:
           System.out.println("Enter radius of the circle:");
           int r=s.nextInt();
           while(r<0)
           {
           System.out.println("Radius should not be negative, Please input again");
           System.out.println("Enter radius of the circle:");
           r=s.nextInt();
           }
           double area=3.14159*r*r;
           System.out.println("Area of the Circle:"+area);
           break;
       case 2:
           System.out.println("Enter length of the rectangle:");
           int length=s.nextInt();
           while(length<0)
           {
           System.out.println("Length should not be negative, Please input again");
           System.out.println("Enter length of the rectangle:");
           length=s.nextInt();
           }
           System.out.println("Enter width of the rectangle:");
           int width=s.nextInt();
           while(width<0)
           {
           System.out.println("Width should not be negative, Please input again");
           System.out.println("Enter width of the rectangle:");
           width=s.nextInt();
           }
           double areaRectangle=length*width;
           System.out.println("Area of the Rectangle:"+areaRectangle);
           break;
       case 3:
           System.out.println("Enter base length of the Triangle:");
           int base=s.nextInt();
           while(base<0)
           {
           System.out.println("Base length should not be negative, Please input again");
           System.out.println("Enter base length of the rectangle:");
           base=s.nextInt();
           }
           System.out.println("Enter height of the Triangle:");
           int height=s.nextInt();
           while(height<0)
           {
           System.out.println("Height should not be negative, Please input again");
           System.out.println("Enter height of the rectangle:");
           height=s.nextInt();
           }
           double areaTriangle=0.5*base*height;
           System.out.println("Area of the Triangle:"+areaTriangle);
           break;
       case 4:
           System.exit(0);
                  
       }
       }
  
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote