Everything should reside in a single java file called Menu.java You have the fre
ID: 3746399 • Letter: E
Question
Everything should reside in a single java file called Menu.java
You have the freedom to put everything in one class. Also everything can be in the main function.
Display the menu options
1) Hypotenuse
2) Gravity
3) Quit
The program should run continuously until the user selects option 3. (Validate input using do/while)
Upon selecting option 1, you should call method Hypotenuse that does the following:
Ask the user to input the length for the shortest sides of a right triangle. Calculate and display the length of the longest side (Hypotenuse).
Formula: If (a) and (b) represent the shortest sides and (c) represents the longest side then the formula is
Validation:The inputs must be validated using do/while loops. The inputs should be between 2 and 20 (inclusive)
Hint: Use the pow and sqrt methods in the Math class
Square root and pow examples
double a=9,b,c;
b=Math.sqrt(a); //variables must be declared of type double
c= Math.pow(a,b) //variables must be of type double
Upon selecting option 2, you should call method gravity that does the following:
The moon’s gravity is about 17% of Earth’s. Write a program that accepts your weight here on earth and then calculates and displays your weight on the moon.
Validation: the input should be validated using a while loop. The input should be between 100 and 300 (inclusive)
What to turn in:
Menu.java file
(NOTE: I do not want any more print screens. Just the .java file)
Example of validation:
public static void main(String[] args){
Scanner reader = new Scanner(System.in);
int score;
boolean valid;
do{
valid = true;
System.out.println("What is your score 0 and 100 : ");
score = reader.nextInt();
if (score < 0 || score > 100){
System.out.println("Try again: must be between 0 and 100");
valid = false;
}
}while (!valid);
}
//Here is a basic template that you can use for this assignment.
public class Menu{
static Scanner reader = new Scanner(System.in);
public static void main(String[] args){
int selection = 0;
do{
//call menu function
switch (selection) {
case 1:
//call hyptenuse function
case 2:
//call gravity function
}
}while(????);
}
public static int menu(){
do {
//validate user input
}while(?????);
return input;
}
public static void hypotenuse(){
do{
//validate user input for both variables
}while(??????);
//calculate and display hypotenuse
}
public static void gravity(){
do{
//validate input
}while(??????);
//Calculate and display gravity
}
}
Explanation / Answer
import java.util.Scanner; public class Menu{ static Scanner reader = new Scanner(System.in); public static void main(String[] args){ int selection = 0; int end = 0; do{ selection = menu(); //call menu function switch (selection) { case 1: hypotenuse(); break; case 2: gravity(); break; case 3: end = 1; } }while(end != 1); } public static int menu(){ int choice; do { System.out.println("1) Hypotenuse " + " " + "2) Gravity " + " " + "3) Quit"); System.out.print("Enter your choice: "); choice = reader.nextInt(); }while(choice != 1 && choice != 2 && choice != 3); return choice; } public static void hypotenuse(){ int a,b; double c; boolean valid = true; do{ System.out.print("First Side: "); a = reader.nextInt(); System.out.println("Second Side: "); b = reader.nextInt(); if(a < 2 || a > 20 || b < 2 || b > 20) { valid = false; } else { valid = true; c = Math.pow(a, 2) + Math.pow(b, 2); c = Math.sqrt(c); System.out.println("Hypotnuse is: " + c); } }while(!valid); //calculate and display hypotenuse } public static void gravity(){ double a; double c; boolean valid = true; do{ System.out.print("Weight: "); a = reader.nextDouble(); System.out.println(a); if(a < 100 || a > 300) { valid = false; } else { valid = true; c = .17 * a; System.out.println("Weight on moon is: " + c); } }while(!valid); } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.