Basic Java, OOP -
i'm stuck @ moment, program works should except returning value of 0.0 bmi output, i'm sure i'm overlooking, cannot seem figure out. i've posted same code on question different issue that's solved. below class code... pointers appreciated! have not gotten calcompu
method yet, wanting resultbmi
work first...
import java.util.scanner; public class myperson { double weight; double height; int age; string gender; double calcompu; final double kg_weight_conv = 2.2; final double mt_height_conv = 0.0254; public myperson () { weight = 0; height = 0; age = 0; gender = " "; } public void setweight (double weight) { this.weight = weight; } public void setheight (double height) { this.height = height; } public void setage (int age) { this.age = age; } public void setgender (string gender) { this.gender = gender; } public double getweight () { return weight / kg_weight_conv; } public double getheight () { return height * mt_height_conv; } public int getage () { return age; } public string getgender () { return gender; } //instance method calculate bmi public double bodymassindex() { double resultbmi = weight / math.pow(height, 2); return resultbmi; } public double caloriecomp(double weight, double height, int age) { double heightincentimeters = height * 100; system.out.println("select activity level: "); system.out.println(" 1 - sedentary"); system.out.println(" 2 - moderately active (light exercise 1-3 days week) "); system.out.println(" 3 - active (moderate exercise 3-5 days week) "); system.out.println(" 4 - active (heavy exercise 6-7 days week) "); system.out.printf("enter choice above: "); int afactor; scanner keyboard = new scanner( system.in ); afactor = keyboard.nextint(); switch (afactor) { case 1: afactor = (int) 1.2; //sedentary people break; case 2: afactor = (int) 1.375; //moderate active people break; case 3: afactor = (int) 1.55; //active people break; case 4: afactor = (int) 1.725; //very active people break; default : system.out.println("invalid choice entered!"); } if ("m".equals(gender)) { //return men double basalmetabolicrate = 13.397 * weight + 4.799 * heightincentimeters - 5.677 * age + 88.362; double psyactfact = basalmetabolicrate * afactor; calcompu = (int) math.ceil(psyactfact); return calcompu; } else { //return women double basalmetabolicrate = 9.247 * weight + 3.098 * heightincentimeters - 4.330 * age + 447.593; double psyactfact = basalmetabolicrate * afactor; calcompu = (int) math.ceil(psyactfact); return calcompu; } } }
main class
import java.util.scanner; public class myhealthcalculator { public static void main(string[] args) { displaypurpose(); //create person object myperson person = new myperson(); scanner keyboard = new scanner( system.in ); system.out.print("please enter weight in pounds: "); person.weight = keyboard.nextdouble(); system.out.print("please enter height in inches: "); person.height = keyboard.nextdouble(); system.out.print("please enter age in years: "); person.age = keyboard.nextint(); system.out.print("please enter gender (m/f): "); person.gender = keyboard.next(); //person.bodymassindex(0, 0); // person.caloriecomp(0, 0, 0); double resultbmi = person.bodymassindex(); displayresults(person, resultbmi, person.caloriecomp(0, 0, 0)); } public static void displaypurpose() { system.out.println("this program implements health assistance " + "calculator"); system.out.println("\ngiven weight, height, , age, compute:"); system.out.println(" bmi - body mass index"); system.out.println(" calories needed per day maintain weight"); system.out.println(); } public static void displayresults(myperson person, double resultbmi, double calcompu) { scanner keyboard2 = new scanner( system.in ); final double low_n_bmi = 18.5; final double hi_n_bmi = 24.9; system.out.println("\nresults:"); system.out.printf("bmi in range of " + low_n_bmi + " " + hi_n_bmi + " considered normal" ); //getters here, gender, age, weight kgs , height meters. //system.out.printf("\n\ncurrent weight in kgs is: " + "%.2f", person.getweight()); //system.out.printf("\ncurrent height in meteres: " + "%.3f", person.getheight()); if ("m".equals(person.gender)) system.out.printf("\nyou " + "male,"); else system.out.printf("\nyou " + "female,"); system.out.printf(" age " + person.age); system.out.printf(", weight of " + "%.2f", person.getweight()); system.out.printf(" kg, height of " + "%.3f", person.getheight()); system.out.printf(" m."); system.out.printf("\nyour bmi " + "%.1f", resultbmi); if (resultbmi < low_n_bmi) system.out.println(", below normal."); else if (resultbmi > hi_n_bmi) system.out.println(", above normal."); else system.out.println(", normal."); system.out.printf("\n maintain current weight, should consume ", calcompu, "calories per day."); if (resultbmi < low_n_bmi) { system.out.println("would try , reach normal range? "); string questionone = keyboard2.next(); if ("y".equals(questionone)) { system.out.println("how many pounds gain per week? "); double questiontwo = keyboard2.nextdouble(); double gainrate = questiontwo * 3500; system.out.println(" gain " + questiontwo + " pound(s) per " + "week, should consume " + gainrate + "calories per day."); } else { } } else if (resultbmi > hi_n_bmi) { system.out.println("would try , reach normal range? "); double questionthree = keyboard2.nextdouble(); double loserate = questionthree / 3500; system.out.println(" lose " + questionthree + " pound(s) per " + "week, should consume " + loserate + "calories per day."); } else { } } }
example input / output
run: program implements health assistance calculator given weight, height, , age, compute: bmi - body mass index calories needed per day maintain weight please enter weight in pounds: 180 please enter height in inches: 68 please enter age in years: 24 please enter gender (m/f): m select activity level: 1 - sedentary 2 - moderately active (light exercise 1-3 days week) 3 - active (moderate exercise 3-5 days week) 4 - active (heavy exercise 6-7 days week) enter choice above: 2 results: bmi in range of 18.5 24.9 considered normal male, age 24, weight of 81.82 kg, height of 1.727 m. bmi 0.0, below normal. maintain current weight, should consume try , reach normal range? y how many pounds gain per week? 5 gain 5.0 pound(s) per week, should consume 17500.0calories per day. build successful (total time: 22 seconds)
the problem mismatch between weight , height units. weight in pounds, , height in inches. plugging in 145 pounds , 69 inches in bmi formula, 145 / 69^2
or ~ 0.03
bmi. you're seeing 0.0 in output, because placeholder "%.1"
limits output 1 digit after decimal point.
modify bmi formula u.s. units.
Comments
Post a Comment