Question

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 than 18.5

Normal

18.5 or more, but less than 25.0

Overweight

25.0 or more, but less than 30.0

Obese

30.0 or more

To convert inches to meters:

                meters = inches / 39.37

To convert pounds (lbs) to kilograms:

                kilograms = lbs / 2.2046

Assignment

Ask the user for their weight in pounds and height in inches. Compute their BMI and BMI classification and output the results.

The program must implement and use the following methods:

convertToKilograms – convert pounds to kilograms

convertToMeters – convert inches to meters

calcBMI – take weight in kilograms and height in meters and return the BMI

bmiClassification – take the value for the BMI and return a String with the BMI classification

Use the following code as a starting point for your assignment:

package home;

import java.util.Scanner;

// TODO Student name, date, purpose
public class Main {

    private static Scanner input = new Scanner(System.in);

    private static final String INPUT_ERROR = "Input is not valid, try again";



    public static void main(String[] args) {
        double lbs, inches, meters, kgs, bmi;
        String classification;

System.out.println("Calculate BMI");
lbs = inputWeight();
inches = inputHeight();



       // TODO add code here
   
}

   // TODO add your methods here (make them static)
}

Validations

You will include code that will validate all user input. If the user inputs an invalid value, the program will respond with an error message and will ask the user to input the data again (use a do / while loop). Invalid values include:

  • Weight or height are less than or equal to zero
  • Weight or height contain non-numeric characters

When you get user input, read in the values as Strings. Convert the weight and height String values using the Double.parseDouble() method. Be sure to handle all exceptions. See:

https://docs.oracle.com/javase/10/docs/api/java/lang/Double.html#parseDouble(java.lang.String)

for details.

You will create and use two new methods:

inputWeight() – return a double with the user’s valid weight in pounds

inputHeight() – return a double with the user’s valid height in inches

Example Output

Calculate BMI

Enter weight (lbs): -17

Input is not valid, try again

Enter weight (lbs): 0

Input is not valid, try again

Enter weight (lbs): abcd

Input is not valid, try again

Enter weight (lbs): 200

Enter height (inches): -12

Input is not valid, try again

Enter height (inches): 0

Input is not valid, try again

Enter height (inches): 1abc

Input is not valid, try again

Enter height (inches): 72

Your BMI is 27.124767811523153

Your BMI classification is Overweight

coding:

package HOME;

import java.util.Scanner;

public class Main {

   import java.util.Scanner;
    public class Main {

        private static Scanner input = new Scanner(System.in);
        private static final String INPUT_ERROR = "Input is not valid, try again";

        public static void main(String[] args) {
            double lbs, inches, meters, kgs, bmi;
            String classification;
            System.out.println("Calculate BMI");
            do {
                lbs = inputWeight();
            } while (lbs <= 0.0);
            do {
                inches = inputHeight();
            } while (inches <= 0.0);
            kgs = convertToKilograms(lbs);
            meters = convertToMeters(inches);
            bmi = calcBMI(kgs, meters);
            System.out.println("Your BMI is " + bmi);
            System.out.println(bmiClassification(bmi));
        }
        private static double inputWeight() {
            System.out.print("Enter weight (lbs): ");
            String pounds = input.nextLine();
            try {
                double value = Double.parseDouble(pounds);
                if (value <= 0.0) {
                    value = 0.0;
                    System.out.println(INPUT_ERROR);
                }
                return value;
            } catch (NullPointerException ex) {
                System.out.println(INPUT_ERROR);
                return 0.0;
            } catch (NumberFormatException ex) {
                System.out.println(INPUT_ERROR);
                return 0.0;
            } catch (Exception ex) {
                System.out.println(INPUT_ERROR);
                return 0.0;
            }
        }
        private static double inputHeight() {
            System.out.print("Enter height (inches):");
            String height = input.nextLine();
            try {
                double value = Double.parseDouble(height);
                if (value <= 0.0) {
                    value = 0.0;
                    System.out.println(INPUT_ERROR);
                }
                return value;
            } catch (NullPointerException ex) {
                System.out.println(INPUT_ERROR);
                return 0.0;
            } catch (NumberFormatException ex) {
                System.out.println(INPUT_ERROR);
                return 0.0;
            } catch (Exception ex) {
                System.out.println(INPUT_ERROR);
                return 0.0;
            }
        }
        private static double convertToKilograms(double lbs) {
            return (lbs / 2.2046);
        }
        private static double convertToMeters(double inches) {
            return (inches / 39.37);
        }
        private static double calcBMI(double weight, double height) {
            return (weight / (height * height));
        }
        private static String bmiClassification(double bmi) {
            if (bmi < 18.5) {
                return "Your BMI classification is Underweight";
            } else if (bmi >= 18.5 && bmi < 25) {
                return "Your BMI classification is Normal";
            } else if (bmi >= 25 && bmi < 30) {
                return "Your BMI classification is Overweight";
            } else if (bmi > 30) {
                return "Your BMI classification is Obese";
            } else {
                return "";
            }
        }
    }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the answer below, have corrected some exception part

Main.java
import java.util.Scanner;

public class Main {
  private static Scanner input = new Scanner(System.in);
  private static final String INPUT_ERROR = "Input is not valid, try again";

  public static void main(String[] args) {
    double lbs, inches, meters, kgs, bmi;
    String classification;
    System.out.println("Calculate BMI");
    do {
      lbs = inputWeight();
    } while (lbs <= 0.0);
    do {
      inches = inputHeight();
    } while (inches <= 0.0);
    kgs = convertToKilograms(lbs);
    meters = convertToMeters(inches);
    bmi = calcBMI(kgs, meters);
    System.out.println("Your BMI is " + bmi);
    System.out.println(bmiClassification(bmi));
  }

  private static double inputWeight() {
    System.out.print("Enter weight (lbs): ");
    String pounds = input.nextLine();
    try {
      double value = Double.parseDouble(pounds);
      if (value <= 0.0) {
        value = 0.0;
        System.out.println(INPUT_ERROR);
      }
      return value;
    } catch (Exception ex) {
      System.out.println(INPUT_ERROR);
      return 0.0;
    }
  }

  private static double inputHeight() {
    System.out.print("Enter height (inches):");
    String height = input.nextLine();
    try {
      double value = Double.parseDouble(height);
      if (value <= 0.0) {
        value = 0.0;
        System.out.println(INPUT_ERROR);
      }
      return value;
    } catch (Exception ex) {
      System.out.println(INPUT_ERROR);
      return 0.0;
    }
  }

  private static double convertToKilograms(double lbs) {
    return (lbs / 2.2046);
  }

  private static double convertToMeters(double inches) {
    return (inches / 39.37);
  }

  private static double calcBMI(double weight, double height) {
    return (weight / (height * height));
  }

  private static String bmiClassification(double bmi) {
    if (bmi < 18.5) {
      return "Your BMI classification is Underweight";
    } else if (bmi >= 18.5 && bmi < 25) {
      return "Your BMI classification is Normal";
    } else if (bmi >= 25 && bmi < 30) {
      return "Your BMI classification is Overweight";
    } else if (bmi > 30) {
      return "Your BMI classification is Obese";
    } else {
      return "";
    }
  }
}

Output:

Please let us know in the comments if you face any problems.

Add a comment
Know the answer?
Add Answer to:
IT Java code In Lab 8, we are going to re-write Lab 3 and add code...
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
  • javafx 1. Implement a BMI (Body Mass Index) server that will accept height and weight from...

    javafx 1. Implement a BMI (Body Mass Index) server that will accept height and weight from the client, calculate the BMI and return a string with the BMI and a status (l.e. normal, over weight, etc.). Build a GUI using JavaFX. You are given a working BMI class that will do the BMI calculations, etc. A single BMI client connection is sufficient. 2. Implement a JavaFX BMI client that will allow the user to enter height (in inches), weight (in...

  • Please solve the following, please type out the code, and do not hand write because its...

    Please solve the following, please type out the code, and do not hand write because its hard to read. I would greatly appreciate it. (JAVA) Problem 1 In a class called H1P1, write a method called bmiOne that takes as arguments a mass in kilograms (a double) and a height in meters (also a double), and returns the body mass index (or BMI) for the given data. If we have mass -m kg and height-h meters, then BMI = Write...

  • c++ pls 10D yUur Tugt Sna you will have to re-take it at another time PROBLEM...

    c++ pls 10D yUur Tugt Sna you will have to re-take it at another time PROBLEM 1 Body Mass Index (BMI) is a measure of body fat that is useful in screening for health issues. To calculate a BMi, you need the height in inches and the weight in pounds. You square the height, then divide the weight in pounds by the squared-height. BMI is defined in terms of meters and kilograms, so to convert from pounds and inches to...

  • In c++ Please. Now take your Project 4 and modify it.  You’re going to add another class...

    In c++ Please. Now take your Project 4 and modify it.  You’re going to add another class for getting the users name. Inherit it. Be sure to have a print function in the base class that you can use and redefine in the derived class. You’re going to also split the project into three files.  One (.h) file for the class definitions, both of them.  The class implementation file which has the member function definitions. And the main project file where your main...

  • 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?                  ...

  • CONVERT THIS JAVA CODE WITH THE JAVA DOC GUIDELINES WHERE APPROPRIATE. DO NOT CHANGE THE CODE....

    CONVERT THIS JAVA CODE WITH THE JAVA DOC GUIDELINES WHERE APPROPRIATE. DO NOT CHANGE THE CODE. SAME CODE SHOULD BE USED FOR THE DOCUMENTATION PURPOSES. ONLY INSERT THE JAVA DOC. //Vehicle.java public class Vehicle {    private String manufacturer;    private int seat;    private String drivetrain;    private float enginesize;    private float weight; //create getters for the attributes    public String getmanufacturer() {        return manufacturer;    }    public String getdrivetrain() {        return drivetrain;...

  • [JAVA] Suppose we need to write code that receives String input from a user, and we...

    [JAVA] Suppose we need to write code that receives String input from a user, and we expect the String input to contain a double value (inside quotes). We want to convert the String to a double using the static method Double.parseDouble(String s) from the Double class. The parseDouble method throws a NumberFormatException if the String s is not a parsable double. Write a method printSum that takes two String parameters (that hopefully have parsable doubles in them), and either prints...

  • Welcome to the Triangles program Enter length 1: 3 Enter length 2: 5 Enter length 3:...

    Welcome to the Triangles program Enter length 1: 3 Enter length 2: 5 Enter length 3: 5 Enter the base: 5 Enter the height: 8 --------------------- The triangle is Isosceles since it has two equal length sides. Isosceles triangle also has two equal angles The area is: 20 The permimeter is: 13 Continue? (y/n): y Enter length 1: 10 Enter length 2: 10 Enter length 3: 10 Enter the base: 10 Enter the height: 7 --------------------- The triangle is Equilateral...

  • Java program Build the interface: A BMI calculator First: add a Layered Pane; drag it from...

    Java program Build the interface: A BMI calculator First: add a Layered Pane; drag it from the right window to your blank frame. Then enlarge the pane to make it cover your entire frame. Use the text fields and labels to build the following application. Example: String inch = jTextField1.getText(); You need to convert value in the text field from String to integer when you do the calculation. How to Calculate BMI 1 feet = 12 inches BMI= weight in...

  • 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...

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