Here is the code for the BMI Class
public class BMI { /** * This class calculates Body * Mass Index */ //fields private int weight; private int feet; private int inches; private int totalHeightInInches; //constructors public BMI(){ weight=0; feet=0; inches=0; totalHeightInInches=0; } public BMI(int weightInPounds, int heightInFeet, int additionalInches) { SetWeight(weightInPounds); SetHeightInFeet(heightInFeet); SetRemainingInches(additionalInches); totalHeightInInches=0; } //mutators and accessors public void SetWeight(int w) { weight=w; } public int GetWeight() { return weight; } public void SetHeightInFeet(int ft) { feet=ft; } public int GetHeightInFeet() { return feet; } public void SetRemainingInches(int inch) { inches=inch; } public int GetRemainingInches() { return inches; } //methods private void CalculateTotalHeightInInches() { totalHeightInInches=(feet*12)+inches; } public double CalculateBMI() { CalculateTotalHeightInInches(); return (double)GetWeight() * 703/(double) (totalHeightInInches * totalHeightInInches); } public String BMIEvaluation(){ double b = CalculateBMI(); String eval=""; if (b > 30) eval="Obese"; else if (b >24.9) eval="Overweight"; else if (b > 18.4) eval="Normal"; else eval="Underweight"; return eval; } }
Here is the program class with the main. You could make a display class that is called by the main. That would be a better structure, but I did not specify that as a requirement
import java.util.Scanner; public class Program { /** * @param args */ public static void main(String[] args) { Scanner scan=new Scanner(System.in); System.out.println("Enter your wieght in pounds"); int lbs = scan.nextInt(); System.out.println("enter your height in feet"); int ft = scan.nextInt(); System.out.println("enter any Remaining inches"); int inch = scan.nextInt(); BMI bmi = new BMI(lbs, ft, inch); double b = bmi.CalculateBMI(); System.out.println("Your BM! is " + b); String badNews=bmi.BMIEvaluation(); System.out.println(badNews); } }
Here is the output
Enter your wieght in pounds 180 enter your height in feet 5 enter any Remaining inches 10 Your BM! is 25.824489795918367 Overweight
No comments:
Post a Comment