In: Computer Science
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.
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!