Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package edu.cscc;
- import java.util.Scanner;
- // Nicholas Rigsby, 09/22/2020, This program will output a bmi and classification based on entered values
- public class Main {
- private static Scanner input = new Scanner(System.in);
- public static void main(String[] args) {
- double lbs, inches, meters, kgs, bmi;
- String classification;
- System.out.println ("Calculate BMI");
- lbs = inputWeight();
- inches = inputHeight();
- kgs = convertToKilograms (lbs);
- meters = convertToMeters (inches);
- bmi = calcBMI (meters, kgs);
- classification = bmiClassification (bmi);
- System.out.print ("Your BMI is: " +bmi+ "\n");
- System.out.print ("Your BMI classification is: " +classification);
- }
- //Checks for valid weight input
- public static double inputWeight() {
- boolean weightError = true;
- do {
- System.out.print("Enter weight (lbs): ");
- String strLbs = input.nextLine();
- try {
- double lbs = Double.parseDouble(strLbs);
- weightError = false;
- return lbs;
- } catch (NumberFormatException e) {
- System.out.print("invalid input");
- }
- } while (weightError);
- return lbs;
- }
- //Checks for valid height input
- public static double inputHeight(){
- boolean heightError = true;
- do{
- System.out.print("Enter height (in): ");
- String strIn = input.nextLine();
- try {
- double inches = Double.parseDouble(strIn);
- heightError = false;
- return inches;
- }catch (NumberFormatException e){
- System.out.print("invalid input");
- }
- }while(heightError);
- return inches;
- }
- // Converts weight from pounds to kilograms
- public static double convertToKilograms (double lbs) {
- double kgs;
- kgs = lbs / 2.2046;
- return kgs;
- }
- //Converts height from inches to meters
- public static double convertToMeters (double inches) {
- double meters;
- meters = inches / 39.37;
- return meters;
- }
- //Calculates BMI based on weight and height conversions
- public static double calcBMI (double meters, double kgs){
- double bmi;
- bmi = kgs/Math.pow(meters,2);
- return bmi;
- }
- //Calculates BMI classification
- public static String bmiClassification(double bmi) {
- String classification;
- if (bmi<18.5)
- classification = "Underweight";
- else if (bmi<25.0)
- classification = "Normal";
- else if (bmi<30.0)
- classification = "Overweight";
- else
- classification = "Obese";
- return classification;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement