Question

Having trouble with the do while/while loop and the switch statement. I got some of the switch statement but cant get the program to repeat itself like it should.What i have so far for my code is below. Any help is appreciated... i am not sure what I am doing wrong or what i am missing. I am completely lost on the while loop and where and how to use it in this scenario.

For this assignment, you will create a Java program that will evaluate a persons health. The program will ask the user for their waist measurement (in inches), height measurement (in feet and inches), gender and number of minutes of exercise they get in a typical week. After evaluating, the program will print the users waist measurement (in inches), height measurement (in inches), gender, waist to height ratio (as a percentage, rounded to 2 decimal places), a ratio message, the total number of minutes of exercise they get in a typical week and an exercise message onto the screen. Items will be displayed, as shown below (remember to use -, where appropriate. Example output is shown below in red (note the output shown below is an example, your values will be different). Before asking for the users waist measurement and, again, before printing the users measurements, you will print 2 lines containing 50 asterisks per line (you must use a nested for loop to complete this task). After printing one users waist to height ratio, the program will ask if they would like to calculate another waist to height ratio (this will continue, as long as the user indicates that they would like to perform another calculation). The ratio message that will be displayed is dependent upon the indicated gender and waist to height ratio, as shown below Gender Female Waist to Height Ratio Under 42% 42% to 48% 48.01% to 57% Over 57% Under 43% 43% to 52% 52.01% to 62% Over 62% Ratio Message You are considered underwei You are at a healthy weight! You are considered overweight. You are considered obese You are considered underweight You are at a healthy weight! You are considered overweight You are considered obese Male The exercise message that will be displayed is dependent upon the number of minutes of exercise that the user gets in a typical week. If the user gets under 150 minutes, print the message You are not getting a sufficient amount of exercise in a typical week. Otherwise, print the message You are getting adequate exercise in a typical week This program must make use of a nested if/else statement, a while or do...while loop and a switch This program will make use of 7 methods. The first method will be used to print the lines of asterisks onto the screen. The second method will be used to ask for the waist measurement (in inches). The third method will be used to ask for the height measurement (in feet and inches) and convert this height to total inches (remember, a method can return only a single vale) The fourth method will be used to ask for the users gender. The fifth method will be used to ask for the typical number of minutes of exercise the user gets each day of the week; this method will perform a cumulative algorithm to calculate the total number of minutes of exercise that the user gets in a typical week. The sixth method

import java.util.Scanner;

public class sampleforchegg {

public static void main(String[] args) {


//while loop goes here

Scanner console= new Scanner (System.in);


printAsterisks(); //printing 2 lines of 50 asterisk
int waist = inputWaist(); //taking input of waist
int height = inputHeight(); //taking input of height
String gender = inputGender(); //taking input of gender
String message = "";
int minutes = getMinutes(console);
printAsterisks();
int ratio = waist_to_height_ratio(waist,height); //Calculating the ratio
String s= "y";
  

if(gender.equalsIgnoreCase("f")) //get the message using the waist to height ration and gender
{ //open if/else
if(ratio < 42)
message = "You are considered underweight.";
else if(ratio >= 42 && ratio <= 48)
message = "You are at a healthy weight!";
else if(ratio >= 48.01 && ratio <= 57)
message = "You are considered overweight.";
else if(ratio > 57)
message = "You are considered obese.";
} //close if/else

  
else
{ //open if/else
if(ratio < 43)
message = "You are considered underweight.";
else if(ratio >= 43 && ratio <= 52)
message = "You are at a healthy weight!";
else if(ratio >= 52.01 && ratio <= 62)
message = "You are considered overweight.";
else if(ratio > 62)
message = "You are considered obese.";
} //close if/else

  
  
  

display(waist, height, gender, ratio, message, minutes); //print the information


} //closing main

//#1 METHOD- method that prints two lines of asterisks
public static void printAsterisks()
{ //open forloop
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 50; j++)
{
System.out.print("*");
}
System.out.println();
}
} //closing forloop


//#2 METHOD- function for taking input of the waist in inches
public static int inputWaist()
{
Scanner scan = new Scanner(System.in); //Creating an object of scanner class
System.out.print("Please enter your waist measurement, in inches: "); //Printing the appropriate message to the user
int waist = scan.nextInt(); //Taking the input
System.out.println();
return waist; //Returning the value of waist
}

//#3 METHOD- Function for taking the input of height in feet and inches
public static int inputHeight()
{
Scanner scan = new Scanner(System.in); // Creating an object of Scanner class.
System.out.print("Please enter your height measurement, in feet and inches (Separate feet and inches by space): ");
int height_feet =scan.nextInt();
System.out.println();
int height_inches = scan.nextInt();
return height_feet*12 + height_inches; //converting the feet to inches by multiplying 12
}

//#4 METHOD- Fuction for taking the input of the users gender
public static String inputGender()
{
Scanner scan = new Scanner(System.in); // Creating an object of Scanner class.
System.out.print("Please enter your gender, F for female or M for male: ");
String gender = scan.next();
System.out.println();
return gender;
}

//#5 METHOD= fuction for taking total minutes of exercise in 7 days
public static int getMinutes (Scanner console)
{
int sum=0, minutes;
for (int i=1; i<=7; i++)
{ System.out.print("Please enter the typical number of minutes of exercise you get on day" + i + " of each week: ");
sum = sum + console.nextInt();
}
minutes = sum;
return minutes;
}

//#6 METHOD- for caculating the ratio of wait/height
public static int waist_to_height_ratio(int waist, int height)
{
double percentage;
percentage = (waist*1.0)/(height*1.0) * 100.0; // Converting the integer values of double and calculating the percentage
//ROunding the double value to the nearest integer and converting long integer returned be Math.round()
int k= (int)Math.round(percentage);
return k; //returning the percentage
}

// #7 METHOD- for printing out users results
public static void display(int waist, int height, String gender, int ratio, String message, int minutes)
{
System.out.println("Waist = " + waist + " inches");
System.out.println("Height = " + height + " inches");
System.out.println("Gender = " + gender);
System.out.println("Waist to Height Ratio = " + ratio + "%");
System.out.println(message);
System.out.println("Number of Minutes of Exercise in a Typical Week = " + minutes);


System.out.println("Would you like to perform health calculation of another user(enter Y/N)");
Scanner scan = new Scanner(System.in); // Creating an object of Scanner class.
String s=scan.next();

switch(s)
{
case "y": System.out.println("");
break;
default: System.out.println(" Thank you for using health calculator! Good Bye!");
break;
}
}
} //Closes class

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi please find the modified program below:

import java.util.Scanner;

public class sampleforHomeworkLib {

public static void main(String[] args) {


//while loop goes here

Scanner console= new Scanner (System.in);

String s= "y";
do
{
printAsterisks(); //printing 2 lines of 50 asterisk
int waist = inputWaist(); //taking input of waist
int height = inputHeight(); //taking input of height
String gender = inputGender(); //taking input of gender
String message = "";
int minutes = getMinutes(console);
printAsterisks();
int ratio = waist_to_height_ratio(waist,height); //Calculating the ratio

  

if(gender.equalsIgnoreCase("f")) //get the message using the waist to height ration and gender
{ //open if/else
if(ratio < 42)
message = "You are considered underweight.";
else if(ratio >= 42 && ratio <= 48)
message = "You are at a healthy weight!";
else if(ratio >= 48.01 && ratio <= 57)
message = "You are considered overweight.";
else if(ratio > 57)
message = "You are considered obese.";
} //close if/else

  
else
{ //open if/else
if(ratio < 43)
message = "You are considered underweight.";
else if(ratio >= 43 && ratio <= 52)
message = "You are at a healthy weight!";
else if(ratio >= 52.01 && ratio <= 62)
message = "You are considered overweight.";
else if(ratio > 62)
message = "You are considered obese.";
} //close if/else

  
  
  

display(waist, height, gender, ratio, message, minutes); //print the information
System.out.println("Would you like to perform calculations for another persion (enter y or n)");
s=console.next();

}while (s.equalsIgnoreCase("y"));
System.out.println(" Thank you for using health calculator! Good Bye!");
} //closing main

//#1 METHOD- method that prints two lines of asterisks
public static void printAsterisks()
{ //open forloop
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 50; j++)
{
System.out.print("*");
}
System.out.println();
}
} //closing forloop


//#2 METHOD- function for taking input of the waist in inches
public static int inputWaist()
{
Scanner scan = new Scanner(System.in); //Creating an object of scanner class
System.out.print("Please enter your waist measurement, in inches: "); //Printing the appropriate message to the user
int waist = scan.nextInt(); //Taking the input
System.out.println();
return waist; //Returning the value of waist
}

//#3 METHOD- Function for taking the input of height in feet and inches
public static int inputHeight()
{
Scanner scan = new Scanner(System.in); // Creating an object of Scanner class.
System.out.print("Please enter your height measurement, in feet and inches (Separate feet and inches by space): ");
int height_feet =scan.nextInt();
System.out.println();
int height_inches = scan.nextInt();
return height_feet*12 + height_inches; //converting the feet to inches by multiplying 12
}

//#4 METHOD- Fuction for taking the input of the users gender
public static String inputGender()
{
Scanner scan = new Scanner(System.in); // Creating an object of Scanner class.
System.out.print("Please enter your gender, F for female or M for male: ");
String gender = scan.next();
System.out.println();
return gender;
}

//#5 METHOD= fuction for taking total minutes of exercise in 7 days
public static int getMinutes (Scanner console)
{
int sum=0, minutes;
for (int i=1; i<=7; i++)
{ System.out.print("Please enter the typical number of minutes of exercise you get on day" + i + " of each week: ");
sum = sum + console.nextInt();
}
minutes = sum;
return minutes;
}

//#6 METHOD- for caculating the ratio of wait/height
public static int waist_to_height_ratio(int waist, int height)
{
double percentage;
percentage = (waist*1.0)/(height*1.0) * 100.0; // Converting the integer values of double and calculating the percentage
//ROunding the double value to the nearest integer and converting long integer returned be Math.round()
int k= (int)Math.round(percentage);
return k; //returning the percentage
}

// #7 METHOD- for printing out users results
public static void display(int waist, int height, String gender, int ratio, String message, int minutes)
{
System.out.println("Waist = " + waist + " inches");
System.out.println("Height = " + height + " inches");
System.out.println("Gender = " + gender);
System.out.println("Waist to Height Ratio = " + ratio + "%");
System.out.println(message);
System.out.println("Number of Minutes of Exercise in a Typical Week = " + minutes);


//System.out.println("Would you like to perform health calculation of another user(enter Y/N)");
//Scanner scan = new Scanner(System.in); // Creating an object of Scanner class.
//String s=scan.next();

/*switch(s)
{
case "y": System.out.println("");
break;
default: System.out.println(" Thank you for using health calculator! Good Bye!");
break;
}*/
}
} //Closes class

Output:

Add a comment
Know the answer?
Add Answer to:
Having trouble with the do while/while loop and the switch statement. I got some of the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (How do I remove the STATIC ArrayList from the public class Accounts, and move it to...

    (How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?) import java.util.ArrayList; import java.util.Scanner; public class Accounts { static ArrayList<String> accounts = new ArrayList<>(); static Scanner scanner = new Scanner(System.in);    public static void main(String[] args) { Scanner scanner = new Scanner(System.in);    int option = 0; do { System.out.println("0->quit\n1->add\n2->overwirte\n3->remove\n4->display"); System.out.println("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } else if (option == 1) { add(); } else...

  • Complete the do-while loop to output 0 to countLimit. Assume the user will only input a...

    Complete the do-while loop to output 0 to countLimit. Assume the user will only input a positive number. import java.util.Scanner; public class CountToLimit { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); int countLimit = 0; int printVal = 0; // Get user input countLimit = scnr.nextInt(); printVal = 0; do { System.out.print(printVal + " "); printVal = printVal + 1; } while ( /* Your solution goes here */ ); System.out.println(""); return; } }

  • (a)How many times does the code snippet given below display "Hello"? int x = 1; while...

    (a)How many times does the code snippet given below display "Hello"? int x = 1; while (x != 15) {    System.out.println ("Hello");    x++; } (b)What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 5) {    sum = sum + i;    i++; } System.out.println("The value of sum is " + sum); Quie 2 What is the output of the following snipped code? public class Test {...

  • I need to create a code for this prompt: In this project we will build a...

    I need to create a code for this prompt: In this project we will build a generic UserInput class for getting keyboard input from the user. Implementation: The class UserInput is a 'Methods only' class, and all the methods should be declared static. Look at the TestScanner.java program at the bottom of this page that inputs a string, int and double. It shows you how to use Scanner class to get input from the keyboard. Write FOUR simple methods, one...

  • I have spent so much time on this and I am having trouble getting all the...

    I have spent so much time on this and I am having trouble getting all the methods to work together correctly to run the code right, and I am not sure what is wrong with it. /* You will recreate one or two versions of the logic game shown in the Algorithms movie and you will ask which one the user would like to play. Upon completion of the game, display who won and ask if they would like to...

  • I have already finished most of this assignment. I just need some help with the canMove...

    I have already finished most of this assignment. I just need some help with the canMove and main methods; pseudocode is also acceptable. Also, the programming language is java. Crickets and Grasshoppers is a simple two player game played on a strip of n spaces. Each space can be empty or have a piece. The first player plays as the cricket pieces and the other plays as grasshoppers and the turns alternate. We’ll represent crickets as C and grasshoppers as...

  • IT Java code In Lab 8, we are going to re-write Lab 3 and add code...

    IT Java code In Lab 8, we are going to re-write Lab 3 and add code to validate user input. The Body Mass Index (BMI) is a calculation used to categorize whether a person’s weight is at a healthy level for a given height. The formula is as follows:                 bmi = kilograms / (meters2)                 where kilograms = person’s weight in kilograms, meters = person’s height in meters BMI is then categorized as follows: Classification BMI Range Underweight Less...

  • Let's fix the displayMenu() method! First, edit the method to do the following: We want to...

    Let's fix the displayMenu() method! First, edit the method to do the following: We want to also allow the user to quit. Include a "Quit" option in the print statements! Have it get the user input from within the method itself and return an int based on the user's choice. (Make sure to also edit the method header and how it is called back in the main() method!) What is the method's return type now?                  ...

  • I have this code but when there's 0 pennies, it doesn't output "and 0 pennies" at...

    I have this code but when there's 0 pennies, it doesn't output "and 0 pennies" at all, it is just left blank. package edu.wit.cs.comp1050; /** * Solution to the third programming assignment * When it runs it outputs the amount in quarters, dimes, nickels and pennies * * @author Rose Levine * */ import java.util.Scanner; public class PA1c {       /**    * Error message to display for negative amount    */    public static final String ERR_MSG =...

  • Hi I need help with a java program that I need to create a Airline Reservation...

    Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT