Question

In: Computer Science

in java Write a Java Program that displays a menu with five different options: 1. Lab...

in java

Write a Java Program that displays a menu with five different options:

1. Lab Test Average Calculator

2. Dice Roll

3. Circle Area Calculator

4. Compute Distance

5. Quit The program will display a menu with each of the options above, and then ask the user to enter their choice. There is also a fifth option to quit, in which case, the program will simply display a goodbye message. Based on the user’s choice, one of the options will be shown. Make sure to include proper output in all cases (such as instructions, questions to the user, or directions telling the user to input something specific). Lab Test Average Calculator: Prompts the user to enter the grades for three Lab Tests. The program then calculates the average of all 3 lab tests and displays the result. Note: For example, if the user inputs: 80, 60, 95 the result should be 78.33 and not 78.0. Be sure to use the appropriate data types for this option. Dice Roll: The program simulates a dice roll by generating a random integer between 1 and 6. The user is then prompted to guess whether the dice value is even or odd. If the user guess matches the evenness/oddness of the dice, a victory message is displayed, otherwise, a loss message is displayed. Circle Area Calculator: Prompts the user to enter the radius, then calculates the circle’s area using the formula (3.14*radius2 πr ). 2 Compute Distance: The user is prompted to enter the x and y coordinates for two points (x1 , y1 and x2 , y2 ). The program then computes and displays the distance between the two user-inputted points. The formula for computing the distance is √(x ) (y ) . 2 − x 1 2 + 2 − y 1 2 Note that you can use Math.pow(a, 0.5) to compute √a . Make sure your code is well commented with well defined steps. You cannot use any sort of Java API methods other than the Scanner for this program.

Solutions

Expert Solution

JAVA PROGRAM

/////////////////////////MenuProgram.java//////////////////////////////

import java.text.DecimalFormat;
import java.util.Scanner;
/**
*MenuProgram class will perform different functionalities
*based on user input choice.
*/
public class MenuProgram {
   //decimalFormat to format decimal number to 2 decimal places
   private DecimalFormat df = new DecimalFormat("#.00");
  
   //main program starts here
   public static void main(String[] args){
       Scanner sc = new Scanner(System.in); //instantiate scanner
       //create a MenuProgram instance
       MenuProgram mp = new MenuProgram();
      
       //this will be used to check when the inifinite loop should end
       boolean isFinish = false;
       do{
           mp.printMenu();//print menu
           //and read user choice
           int userChoice = Integer.parseInt(sc.nextLine());
          
           switch(userChoice){
               case 1: //call labTestAvgCalculator method
                       mp.labTestAvgCalculator(sc);
                       break;
               case 2: //call diceRoll method
                       mp.diceRoll(sc);
                       break;
               case 3: //call circleAreaCalculator method
                       mp.circleAreaCalculator(sc);
                       break;
               case 4: //call computeDistance method
                       mp.computeDistance(sc);
                       break;
               case 5: //good bye and set isFinish to true
                       System.out.println("Good Bye!");
                       isFinish = true;
                       break;
               default: System.out.println("Invalid Choice. Try Again..");
           }
          
           //if isFinish True, then breaks out of inifinite do-while loop
           if(isFinish){
               break;
           }
       }while(true);
   }
  
   /**
   * print the menu
   */
   private void printMenu(){
       System.out.println();
      
       System.out.println("1. Lab Test Average Calculator");
       System.out.println("2. Dice Roll");
       System.out.println("3. Circle Area Calculator");
       System.out.println("4. Compute Distance");
       System.out.println("5. Quit");
      
       System.out.println("\nPlease enter your choice:");
   }
  
   /**
   * prompt user for the 3 grades o lab tests
   * and calculate average
   * @param sc
   */
   private void labTestAvgCalculator(Scanner sc){
       double[] grades = new double[3];
       double sumOfGrades = 0;
       for(int i = 0 ; i < 3 ; i++){
           System.out.println("Please enter grade for lab test "+(i+1)+" : ");
           //read the data and convert to double
           grades[i] = Double.parseDouble(sc.nextLine());
           sumOfGrades = sumOfGrades + grades[i];//calculate running sum
       }
       //calculate avergae
       double average = sumOfGrades/grades.length;
       System.out.println("LAb test average is: "+ df.format(average));
   }
  
   /**
   * Generate a random integer (dice value) between 1 and 6
   * Checks if it is odd/even
   * Prompt for use guess of even/odd
   * Display victory/loss based on correctness of user
   * @param sc
   */
   private void diceRoll(Scanner sc){
       //generate random integer between 1 and 6
       int randomInt = 1 + (int)Math.random()*6;
       //If randomInt%2 == 0 then it is even else odd
       boolean isEven = (randomInt%2 == 0);
       System.out.println("Guess the dice value (Type E for Even O for Odd): ");
       String userInput = sc.nextLine();//read user input
       if(userInput.equalsIgnoreCase("E")){//if user guess is for even
           if(isEven)
               System.out.println("VICTORY!");
           else
               System.out.println("LOSS!");
       }else if(userInput.equalsIgnoreCase("O")){//else if guess is odd
           if(isEven)
               System.out.println("LOSS!");
           else
               System.out.println("VICTORY!");
       }else{//invalid
           System.out.println("Invalid Entry!");
       }
      
   }
  
   /**
   * calculate area of circle given radius
   * @param sc
   */
   private void circleAreaCalculator(Scanner sc){
       System.out.println("Please enter the radius:");
       double radius = Double.parseDouble(sc.nextLine());//read radius as double
       final double PI = 3.14;//value of pi
       double area = PI*radius*radius;//calculate area
       //format to 2 decimal places and print area
       System.out.println("Area of the circle is: "+ df.format(area));
   }
  
   /**
   * computer distance between 2 user input points
   * @param sc
   */
   private void computeDistance(Scanner sc){
       System.out.println("Enter x-coordinate for Point1: ");
       int x1 = Integer.parseInt(sc.nextLine());
       System.out.println("Enter y-coordinate for Point1: ");
       int y1 = Integer.parseInt(sc.nextLine());
       System.out.println("Enter x-coordinate for Point2: ");
       int x2 = Integer.parseInt(sc.nextLine());
       System.out.println("Enter y-coordinate for Point2: ");
       int y2 = Integer.parseInt(sc.nextLine());
      
       int d1 = x2-x1;
       int d2 = y2-y1;
       double distance = Math.pow((d1*d1+d2*d2), 0.5);
      
       System.out.println("Distance between point1 and point 2 is: "+ df.format(distance));
      
   }
  
  

}

===========================================

OUTPUT

=============================================


1. Lab Test Average Calculator
2. Dice Roll
3. Circle Area Calculator
4. Compute Distance
5. Quit

Please enter your choice:
1
Please enter grade for lab test 1 :
9.87
Please enter grade for lab test 2 :
9.21
Please enter grade for lab test 3 :
8.91
LAb test average is: 9.33

1. Lab Test Average Calculator
2. Dice Roll
3. Circle Area Calculator
4. Compute Distance
5. Quit

Please enter your choice:
2
Guess the dice value (Type E for Even O for Odd):
E
LOSS!

1. Lab Test Average Calculator
2. Dice Roll
3. Circle Area Calculator
4. Compute Distance
5. Quit

Please enter your choice:
3
Please enter the radius:
5.56
Area of the circle is: 97.07

1. Lab Test Average Calculator
2. Dice Roll
3. Circle Area Calculator
4. Compute Distance
5. Quit

Please enter your choice:
4
Enter x-coordinate for Point1:
10
Enter y-coordinate for Point1:
15
Enter x-coordinate for Point2:
25
Enter y-coordinate for Point2:
37
Distance between point1 and point 2 is: 26.63

1. Lab Test Average Calculator
2. Dice Roll
3. Circle Area Calculator
4. Compute Distance
5. Quit

Please enter your choice:
5
Good Bye!


Related Solutions

Write a menu program to have the above options for the polynomials. Your menu program should...
Write a menu program to have the above options for the polynomials. Your menu program should not use global data; data should be allowed to be read in and stored dynamically. Test your output with the data below. Poly #1: {{2, 1/1}, {1, 3/4}, {0, 5/12}} Poly #2: {{4, 1/1}, {2, -3/7}, {1, 4/9}, {0, 2/11}} provide a C code (only C please) that gives the output below: ************************************ *         Menu HW #4 * * POLYNOMIAL OPERATIONS * * 1....
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...
C++ Write a menu based program for the pet rescue. There should be 2 menu options...
C++ Write a menu based program for the pet rescue. There should be 2 menu options -Add a pet -View pets -If the user chooses option 1, you will open a data file for writing without erasing the current contents, and write a new pet record to it. The file can be formatted any way that you choose but should include the pet's name, species, breed, and color. You my also include any additional information you think is appropriate. -If...
Part 1:Write a program in Java that declares an array of 5 elements and displays the...
Part 1:Write a program in Java that declares an array of 5 elements and displays the contents of the array. Your program should attempt to access the 6th element in the array (which does not exist) and using try. catch your program should prevent the run-time error and display your error message to the user. The sample output including the error message is provided below. Part (1) Printing an element out of bounds 5 7 11 3 0 You went...
Create a menu in Java. A menu is a presentation of options for you to select....
Create a menu in Java. A menu is a presentation of options for you to select. You can do this in the main() method. 1. Create a String variable named menuChoice. 2. Display 3 options for your program. I recommend using 3 System.out.println statements and a 4th System.out.print to show "Enter your Selection:". This needs to be inside the do -- while loop. A. Buy Stock. B. Sell Stock X. Exit Enter your Selection: 3. Prompt for the value menuChoice....
1.Write a Java program that inputs a binary number and displays the same number in decimal....
1.Write a Java program that inputs a binary number and displays the same number in decimal. 2.Write Java program that inputs a decimal number and displays the same number in binary.
URGENT!! DO THIS CODE IN JAVA Write a complete Java FX program that displays a text...
URGENT!! DO THIS CODE IN JAVA Write a complete Java FX program that displays a text label and a button.When the program begins, the label displays a 0. Then each time the button is clicked, the number increases its value by 1; that is each time the user clicks the button the label displays 1,2,3,4............and so on.
Class, Let's create a menu in Java. A menu is a presentation of options for you...
Class, Let's create a menu in Java. A menu is a presentation of options for you to select. You can do this in the main() method. 1. Create a String variable named menuChoice. 2. Display 3 options for your program. I recommend using 3 System.out.println statements and a 4th System.out.print to show "Enter your Selection:". This needs to be inside the do -- while loop. A. Deposit Cash. B. Withdraw Cash X. Exit Enter your Selection: 3. Prompt for the...
PROGRAM MUST BE WRITTEN IN JAVAFX Develop a program flowchart and then write a menu-driven Java...
PROGRAM MUST BE WRITTEN IN JAVAFX Develop a program flowchart and then write a menu-driven Java program that will solve the following problem. The program uses one and two-dimensional arrays to accomplish the tasks specified below. The menu is shown below. Please build a control panel as follows: (Note: the first letter is shown as bold for emphasis and you do not have to make them bold in your program.) Help SetParams FillArray DisplayResults Quit Upon program execution, the screen...
Write a JAVA program that displays a table of the Celsius temperatures and their Fahrenheit equivalents....
Write a JAVA program that displays a table of the Celsius temperatures and their Fahrenheit equivalents.  The formula for converting a temperature from Celsius to Fahrenheit is F = 9/ 5 C + 32 where F → Fahrenheit temperature C → Celsius temperature.  Allow the user to enter the range of temperatures in Celsius to be converted into Fahrenheit.  Your program must use a loop to display the values of the temperature conversions (see sample output). Sample...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT