Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Healthy.java coding here: // Class Healthy definition public class Healthy { // Instance variables to store data private String name; private char gender; private double

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Healthy.java coding here:

// Class Healthy definition public class Healthy { // Instance variables to store data private String name; private char gender; private double weight; private double height; private int age; private int activityLevel; // Default constructor public Healthy() { name = ""; gender = ' '; weight = height = age = 0; }// End of default constructor // Setter methods // Method to set name public void setName(String name) { this.name = name; }// End of method // Method to set gender public void setGender(char gender) { this.gender = gender; }// End of method // Method to set weight public void setWeight(double weight) { this.weight = weight; }// End of method // Method to set height public void setHeight(double height) { this.height = height; }// End of method // Method to set age public void setAge(int age) { this.age = age; }// End of method // Method to set activity level public void setActivityLevel(int activityLevel) { this.activityLevel = activityLevel; }// End of method // Getter methods // Method to return name public String getName() { return name; }// End of method // Method to return gender public char getGender() { return gender; }// End of method // Method to return weight public double getWeight() { return weight; }// End of method // Method to return height public double getHeight() { return height; }// End of method // Method to return age public int getAge() { return age; }// End of method // Method to return activity level public int getActivityLevel() { return activityLevel; }// End of method // Method to return gender message public String getGenderMessage() { String genderMessage = ""; // Checks if the gender is 'M' or 'm' return message for male if(gender == 'M' || gender == 'm') genderMessage = " These are for a male."; // Otherwise gender is 'F' or 'f' return message for female else genderMessage = " These are for a female."; // Returns gender message return genderMessage; }// End of method // Overrides toString() method to display information public String toString() { // Concatenates message with getter method to get the instance variable data String information = getName() + "'s information"; information += " Weight: " + getWeight() + " puunds Height: " + getHeight() + " inches Age: " + getAge() + getGenderMessage(); return information; }// End of method // Method to convert pound to KG and returns result double convertPoundsToKg(double pound) { return (pound * 0.4535923); }// End of method // Method to convert inches to centimeter and returns result double convertInchesToCentimeters(double inches) { return (inches * 2.540000); }// End of method // Method to calculate and return BMR double calculateBMR() { double result = 0; // Checks if the gender is male if(getGender() == 'M' || getGender() == 'm') result = 66 + (13.7 * convertPoundsToKg(getWeight()) + (5 * convertInchesToCentimeters(getHeight())) - (6.8 * age)); // Otherwise, gender is female else result = 655 + (9.6 * convertPoundsToKg(getWeight()) + (1.8 * convertInchesToCentimeters(getHeight())) - (4.7 * age)); return result; }// End of method // Method to calculate and return BMI double calculateBMI() { return ((getWeight()) / Math.pow(getHeight(), 2) * 703); }// End of method // Method to calculate and return TDEE double calculateTDEE() { // Checks if the activity level to calculate TDEE if(activityLevel == 1) return (calculateBMR() * 1.2); else if(activityLevel == 2) return (calculateBMR() * 1.375); else if(activityLevel == 3) return (calculateBMR() * 1.55); else if(activityLevel == 4) return (calculateBMR() * 1.725); else return (calculateBMR() * 1.9); }// End of method // Method to return BMI classification message String BMIClassification() { // Checks the BMI and returns appropriate message if(calculateBMI()

HowHealthy.java coding here:

import java.util.Scanner; // Class HowHealthy definition public class HowHealthy { // Scanner object declared static Scanner sc; // Method to accept data void acceptData(Healthy person) { // Local variable to store data entered by the user String name; char gender; double weight; double height; int age; int activityLevel; // Scanner class object created sc = new Scanner(System.in); // Loops till valid name entered by the user do { // Accepts name from the user System.out.print(" Person's name: "); name = sc.nextLine(); // Checks if the length of the name is not zero if(name.length() != 0) { // Calls the method to set the name person.setName(name); // Come out of the loop break; }// End of if condition // Otherwise, invalid name. Displays error message else System.out.println("Person name cannot be null."); }while(true);// End of do - while loop // Loops till valid gender entered by the user do { // Accepts gender from the user System.out.print(name + ", are you male or female (M/F): "); gender = sc.next().charAt(0); // Checks if the gender is either 'M' or 'm' or 'F' or 'f' if(gender == 'M' || gender == 'm' || gender == 'F' || gender == 'f') { // Calls the method to set the gender person.setGender(gender); break; }// End of if condition // Otherwise, invalid gender. Displays error message else System.out.println("Gender can be either (M/F)"); }while(true);// End of do - while loop // Loops till valid weight entered by the user do { // Accepts weight from the user System.out.print(name + "'s weight (pounds): "); weight = sc.nextDouble(); // Checks if the weight is greater than or equals to 100 if(weight >= 100) { // Calls the method to set the weight person.setWeight(weight); // Come out of the loop break; }// End of if condition // Otherwise, invalid weight. Displays error message else System.out.println("Invalid weight - must be at least 100 pounds"); }while(true);// End of do - while loop // Loops till valid height entered by the user do { // Accepts height from the user System.out.print(name + "'s height (inches): "); height = sc.nextDouble(); // Checks if the height is between 60 and 84 inclusive if(height >= 60 && height = 18) { // Calls the method to set the age person.setAge(age); // Come out of the loop break; }// End of if condition // Otherwise, invalid age. Displays error message else System.out.println("Invalid age - must be at least 18 years"); }while(true);// End of do - while loop // Loops till valid activity level entered by the user do { // Displays activity level menu System.out.print(" Activity Level: Use these categories: "); System.out.print(" \t\t 1 - Sedentary (little or no exercise, desk job) "); System.out.print(" \t\t 2 - Lightly active (light exercise/sports 1 - 3 days/wk) "); System.out.print(" \t\t 3 - Moderately active (moderate exercise/sports 3 - 5 days/wk) "); System.out.print(" \t\t 4 - Very active (hard exercise/sports 4 - 7 days/wk) "); System.out.print(" \t\t 5 - Extra active (hard daily exercise/sports & physical job or 2x day training i.e marathon, contest etc.) "); // Accepts activity level from the user System.out.print(" How active are you? "); activityLevel = sc.nextInt(); // Checks if the activity level is between 1 and 5 inclusive if(activityLevel >= 1 && activityLevel Homework 8-"How Healthy Are You, Again?" Problem This is an extension to Homework 7 that calculates health information. In addition this will allow the user to re-enter the correct input when erroneous input is detected. Also, this program will allow the user to enter information for multiple persons. Remember that HW07 had the program terminate if the user entered an invalid input such as a weight less than 100 pounds, an age less than 18, etc. For Homework 8, if the user enters invalid data to a prompt, print an appropriate error message and prompt the user again to re-enter the information. See the Sample Execution below. After completing the calculation for one person, prompt if the user wishes to continue. The user must be prompted to enter the word "Yes" or the word "No." In theory, the user must enter only one of these two words, or receive an error message to re-enter one of the two values. A common programming practice for Yes/No responses is to "short-cut" the response. After the user enters the response of "Yes" or "No" (string), use only the first character to make the decision. If the user enters "Yes," the decision will be based on the letter Y'; similarly with "No." To avoid issues with upper and lower case, change the character to upper case (or lower case if so desired; just be consistent) This brings up one other question: what if the user does not enter the letter ? or the letter The user could be prompted again, but you are to take an easier approach. If the user enters the letter?Y', the program will do another calculation. Any other input will halt the program with the message "Have a good day." Programming focus This program will require the use of loops Description: See Homework 7 for all of the formulas and rules for valid input. There is no need to change any of the formulas as Homework 8 focuses on the validation of the input

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions