Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

D 9 9 import java.util. InputMismatchException; import java.util.Scanner; 9 public class BMI {//class //set values for kilo per pound and meters per inch final

image text in transcribedimage text in transcribedimage text in transcribed

D 9 9 import java.util. InputMismatchException; import java.util.Scanner; 9 public class BMI {//class //set values for kilo per pound and meters per inch final double KILOGRAMS_PER_POUND= 0.45359237; final double METERS_PER_INCH = 0.0254; //initialize fields private double weight; private double height; private double bmi; //create new object of class Scanner Scanner input = new Scanner(System.in); public BMI (double w, double h, double p, int b){ //begin constructor weight = w; //set value of weight height = h; //set value of height bmi= b; //set value of bmi }//end 3 argument constructor public double getWeight() {//begin method get weight return weight; }//end method getweight public void setWeight (double w) {//begin method set weight if (w > 0) {//if value is greater than 0 weight = w;//set value to weight }else {//end method set weight System.out.println(" Please enter a positive number. "); promptWeight(); //print error message and allow the user to retry entry } }//end method set weight public double getHeight() {//begin method getheight return height; }//end method getHeight public void setHeight (double h) {//begin method setheight if (h> 0) {//if value is over > height = h; //set value to height }else { System.out.println(" Please enter a positive number. "); promptHeight(); //print error message and allow the user to retry entry } }//end method setheight public double getBmi() {//begin method getBMI return bmi; }//end method getHeight public void setBmi (double b) {//begin method setBmi bmi = b; }//end method setBmi bmi = b; }//end method setBmi public void promptWeight() {//begin method promptWeight boolean continueLoop = true; //set boolean for looping do { //begin do...while statement try {//begin try statement System.out.print("Please enter your weight in pounds: "); setWeight (input.nextDouble()); //validate amount entered continueLoop = false;//stop the loop } //end try statement catch (InputMismatch Exception ime) { //begin catch statement System.out.println(" Please enter a valid amount. "); input.nextLine(); //print error message and restart the loop }//end catch statement } while (continueLoop); //end do...while statement } //end method promptWeight public void promptHeight() {//begin method promptHeight boolean continueLoop = true; //set boolean for looping do { //begin do...while statement try {//begin try statement System.out.print("Please enter your height in inches: "); setHeight (input.nextDouble());//validate amount entered continueLoop = false;//stop the loop } //end try statement catch (InputMismatch Exception ime) { //begin catch statement System.out.println(" Please enter a valid amount. "); input.nextLine(); //print error message and restart the loop }//end catch statement } while (continueLoop); //end do...while statement } //end method promptHeight public void calculate BMI () {//begin method calculate BMI double weightInKilograms = getWeight() * KILOGRAMS_PER_POUND; double heightInMeters = getHeight()* METERS_PER_INCH; setBmi (weightInKilograms /(heightInMeters * heightInMeters));//calculate BMI System.out.println("BMI is + bmi); if (getBmi() < 18.5) System.out.println("You are Underweight"); else if (getBmi() < 25) System.out.println("You are Normal"); else if (getBmi() < 30) System.out.println("You are overweight"); else System.out.println("You are Obese");//print BMI } //end method public void loop() { //begin method loop //as long as current Year is less than year continue loop and increment current Year calculatoRMT/\//calculato interact try {//begin try statement System.out.print("Please enter your height in inches: "); setHeight (input.nextDouble());//validate amount entered continueLoop = false;//stop the loop } //end try statement catch (InputMismatchException ime) { //begin catch statement System.out.println(" Please enter a valid amount. "); input.nextLine(); //print error message and restart the loop } //end catch statement } while (continueLoop); //end do...while statement } //end method promptHeight public void calculateBMI() {//begin method calculate BMI double weightInkilograms = getWeight() * KILOGRAMS_PER_POUND; double heightInMeters = getHeight()* METERS_PER_INCH; setBmi (weightInKilograms /(heightInMeters * heightInMeters));//calculate BMI System.out.println("BMI is " + bmi); if (getBmi() < 18.5) System.out.println("You are Underweight"); else if (getBmi() < 25) System.out.println("You are Normal"); else if (getBmi() < 30) System.out.println("You are overweight"); else System.out.println("You are Obese");//print BMI } //end method public void loop() { //begin method loop //as long as current Year is less than year continue loop and increment currentYear calculateBMI(); //calculate interest System.out.print(toString()); //print the method toString } //end method loop //to string @Override public String toString() { return "your weight is + getWeight() +", your height is + getHeight() + your BMI is }//end to string public static void main(String[] args) {//main method begina BMI bmi new BMI(0, 0, 0, 0); bmi.promptweight(); bmi.promptHeight(); bmi.loop();//invoke method loop System.out.printf(" %s%20s ", "Height", "Weight"); //print column headers }//end method main + getBmi(); }//end class

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Java Programming, Comprehensive Version

Authors: Y. Daniel Liang

10th Edition

133761312, 978-0133761313

Students also viewed these Programming questions

Question

What is the tax treatment for a guaranteed payment?

Answered: 1 week ago