Question
How to write this program in user defined method? What would be the syntax? import java.util.Scanner; //Imports the scanner utility from the java.util package public
How to write this program in user defined method? What would be the syntax?
import java.util.Scanner; //Imports the scanner utility from the java.util package
public class bodyFatPercentage { public static void main(String[] args){ //Creates the scanner utility for user input Scanner input = new Scanner(System.in); //Declare variables char gender; double weight; double wrist_measurement; double waist_measurement; double hip_measurement; double forearm_measurement; double bodyFat; double bodyFatPercent; double A1, A2, A3, A4, A5, B; // Prompt user to input either male or female System.out.println("Enter 'm' for male or 'f' for female: "); System.out.print(" Gender: "); gender = input.next().charAt(0); // user input is assignmened to variable gender
// switch and case to compare genders and perform respective calculations switch (gender) { // for female gender case 'f': // prompts the user to input weight in pounds System.out.println("Enter body weight in pounds: "); weight = input.nextDouble();
// Failsafe to ensure the user input is a valid entry if (weight < 0 || weight > 400){ System.out.println("Invalid entry");
return; } // prompts user to input required measurements for female gender System.out.println(" Enter wrist measurement in inches: "); wrist_measurement = input.nextDouble();
System.out.println(" Enter waist measurement in inches: "); waist_measurement = input.nextDouble();
System.out.println(" Enter hip measurement in inches: "); hip_measurement = input.nextDouble();
System.out.println(" Enter forearm measurement in inches: "); forearm_measurement = input.nextDouble(); // Processing phase A1 = (weight * .732) + 8.987; A2 = wrist_measurement / 3.14; A3 = waist_measurement * .157; A4 = hip_measurement * .249; A5 = forearm_measurement * .434; B = A1 + A2 + A2 + A3 + A4 + A5;
bodyFat = weight - B;
bodyFatPercent = (bodyFat * 100) / weight; // Output phase System.out.println(" Calculated A1 is: " + A1); System.out.println(" Calculated A2 is: " + A2); System.out.println(" Calculated A3 is: " + A3); System.out.println(" Calculated A4 is: " + A4); System.out.println(" Calculated A5 is: " + A5); System.out.println(" Calculated B is: " + B);
System.out.println(" Your body fat measurement is: " + bodyFat);
System.out.println(" Your body fat percentage is: " + bodyFatPercent);
// Compares the calculated result for bodyfat percentage to display respective status if (bodyFatPercent < 0) { // if calculated result is a negative number System.out.println(" Invalid entry "); }
else if (bodyFatPercent >= 1 && bodyFatPercent < 20) { // if calculated result is between 1 and 19 System.out.println(" OK "); }
else if (bodyFatPercent >= 20 && bodyFatPercent < 50) { // if calculated result is between 20 and 49 System.out.println(" FINE "); }
else { System.out.println(" NOTHING we can do! "); // if calculated result is 50 or more } break; //ends case 'f'
// for male gender case 'm':
// prompts the user to input weight in pounds System.out.println("Enter your body weight in pounds: "); weight = input.nextDouble();
// Failsafe to ensure the user input is a valid entry if (weight < 0 || weight > 400){ System.out.println(" Invalid entry ");
return; }
// prompts the user to input required measurement to calculate body fat percentage System.out.println(" Enter waist measurement in inches: "); waist_measurement = input.nextDouble(); // Processing phase A1 = (weight * 1.082) + 94.42; A2 = waist_measurement * 4.15; B = A1 - A2; bodyFat = weight - B; bodyFatPercent = (bodyFat * 100) / weight; // Output phase System.out.println(" Calculated A1 is: " + A1); System.out.println(" Calculated A2 is: " + A2); System.out.println(" Calculate B is: " + B); System.out.println(" Your body fat measurement is: " + bodyFat);
System.out.println(" Your body fat percentage is: " + bodyFatPercent);
// Compares the calculated result for bodyfat percentage to display respective status if (bodyFatPercent < 0) { System.out.println(" Invalid entry "); // if calculated result is a negative number }
else if (bodyFatPercent >= 1 && bodyFatPercent < 20) { // if calculated result is between 1 and 19 System.out.println(" OK "); }
else if (bodyFatPercent >= 20 && bodyFatPercent < 50) { // if calculated result is between 20 and 49 System.out.println(" FINE "); }
else { System.out.println(" NOTHING we can do! "); // if calcuated result is 50 or more } break; //ends case 'm'
// if anythingn other than 'm' or 'f' for gender input is used default: System.out.println(" Error: Invalid entry "); break; // ends switch case conditions for male and female gender comparisions } } //ends main } //ends class
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started