Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Medical costs and health insurance premiums continue to increase. In an effort t

ID: 3909196 • Letter: M

Question

Medical costs and health insurance premiums continue to increase. In an effort to encourage individuals to embrace healthier lifestyles, the Wolfpack Health Plan is offering discounts to members who do not smoke and/or are not overweight. Health plan members who are obese must pay a surcharge. The tables below provide the monthly health insurance premiums for the four different plans as well as the discounts/surcharge. The discounts and surcharge apply only to the Employee for the first two plans and to the Employee and Spouse for the last two plans. The last table below gives the weight status for employees (and spouses) based on their body mass index (BMI).

Medical costs and health insurance premiums continue to increase. In an effort to encourage individuals to embrace healthier lifestyles, the Wolfpack Health Plan is offering discounts to members who do not smoke and/or are not overweight. Health plan members who are obese must pay a surcharge. The tables below provide the monthly health insurance premiums for the four different plans as well as the discounts/surcharge. The discounts and surcharge apply only to the Employee for the first two plans and to the Employee and Spouse for the last two plans. The last table below gives the weight status for employees (and spouses) based on their body mass index (BMI).


BMI = (weight (lb) / (height (in))2) x 703

For this project, you will write a program named HealthPlan. It must display a header that lists the name of the application – you may name it as you like – and provides instructions about using the program. For example,

Welcome to the Wolfpack Health Plan! When prompted, please enter the health plan in which you would like to enroll: E (E-mployee), C (Employee/C-hildren), S (Employee/S-pouse), F (Employee/F-amily). You will be asked whether or not you smoke and for your weight and height. If you choose plan S or F, you will be asked whether your spouse smokes and for his/her weight and height. Your monthly health plan premium will then be displayed.

The user should be prompted for the following input values:

Health Plan (E, C, S, F) - Both upper- and lowercase characters should be accepted.

Employee Smokes (y/n) - Input that starts with y or Y should be considered as yes, any other input should be considered as no.

Employee Weight (lb) - Input as an integer.

Employee Height (in) - Input as an integer.

If the Health Plan is S-pouse or F-amily, the user should also be prompted for the following input values:

Spouse Smokes (y/n) - Input that starts with y or Y should be considered as yes, any other input should be considered as no.

Spouse Weight (lb) - Input as an integer.

Spouse Height (in) - Input as an integer.

The program should then output the monthly health plan premium formatted as a monetary amount, for example, $710.00.

Here is an example output:

If the user enters an invalid value for the health plan or a height or weight that is negative, the program should print an error message as soon as the error occurs and quit.
HINT: The statement System.exit(1); will cause the program to quit with a status of 1 indicating an error condition.
NOTE: You do not need to handle the situation where the user enters something other than an integer for the height or weight. We will learn to handle this later in the semester.

Here are some examples of handling invalid input:

The program must contain and use the completed version of the methods below. You are free to (and encouraged to) define and use additional methods, if you like – be sure to provide Javadoc for them. Replace the comments below with appropriate Javadoc.

Please note that your program should NEVER call the getPremium() method with illegal arguments because you should do thorough error checking BEFORE calling the method! This requirement is simply to give you practice in checking method parameters and throwing exceptions. It is good practice to include a helpful/descriptive message when you throw an exception. For this project, it is required that you include the message “Invalid input for health care premium” with each IllegalArgumentException you throw.

So far for this program, my code is as follows:

import java.util.*;

public class HealthPlan {

public static final int EMPLOYEE_RATE = 105;

public static final int EMPLOYEE_CHILDREN_RATE = 290;

public static final int EMPLOYEE_SPOUSE_RATE = 670;

public static final int EMPLOYEE_FAMILY_RATE = 710;

public static final int NONSMOKER_DISCOUNT = -30;

public static final int NOT_OVERWEIGHT_DISCOUNT = -15;

public static final int OBESE_SURCHARGE = 20;

public static final double NOT_OVERWEIGHT_BMI = 25.0;

public static final double OBESE_BMI = 30.0;

public static void main(String[] args) {

System.out.print(" Welcome to our new Health Plan! ");

System.out.print("When prompted, enter the health plan in which you would like to enroll: ");

System.out.print("E (E-mployee), C (Employee/C-hildren), S (Employee/S-pouse), F (Employee/F-amily). ");

System.out.print("You will be asked whether or not you smoke and for your weight and height. ");

System.out.print("If you choose plan S or F, you will be asked whether your spouse smokes and for ");

System.out.print("his/her weight and height. Your monthly health plan premium will then be displayed. ");

Scanner console = new Scanner(System.in);

System.out.println("Heath Plan (E-mployee, C-hildren, S-pouse, F-amily): ");

char plan = console.next().charAt(0);

System.out.print("Weight (lb): ");

int weight = console.nextInt();

System.out.print("Height (in): ");

int height = console.nextInt();

System.out.print("Do you smoke?(y/n): ");

String isSmoker = console.nextLine();

boolean smoker = false;

if (isSmoker.equalsIgnoreCase("y")) {

smoker = true;

}

if (Character.toLowerCase(plan) == 'f' || Character.toLowerCase(plan) == 's') {

System.out.print("Spouse Weight (lb): ");

int spouseWeight = console.nextInt();

System.out.print("Spouse Height (in): ");

int spouseHeight = console.nextInt();

System.out.print(calculateBMI(weight, height));

System.out.print("Spouse Smokes (y/n): ");

String isSpouseSmoker = console.nextLine();

boolean spouseSmoker = false;

if (isSpouseSmoker.equalsIgnoreCase("y")) {

spouseSmoker = true;

}

System.out.println("Monthly Health Plan Premium: " + getPremium(plan, smoker, weight, height, spouseSmoker, spouseWeight, spouseHeight));

} else if (Character.toLowerCase(plan) == 'e' || Character.toLowerCase(plan) == 'c') {

boolean spouseSmoker = true;

//using this height and weight spouse will always be overweight and not charged

int spouseWeight = 150;

int spouseHeight = 62;

System.out.print(calculateBMI(weight, height));

System.out.println("Monthly Health Plan Premium: " + getPremium(plan, smoker, weight, height, spouseSmoker, spouseWeight, spouseHeight));

}

}

/**

* Calculates and returns BMI value using the formula given above

* @param weight of individual entered

* @param height of individual entered

* @return bmi

*/

public static double calculateBMI(int weight, int height) {

double bmi = ((weight / Math.pow(height, 2)) * 703);

return bmi;

}

/**

* Calculates the cost of health plan premium

* @param plan

* @param smoker

* @param weight of individual entered

* @param height of individual entered

* @param spouseSmoker

* @param spouseWeight of spouse entered

* @param spouseHeight of spouse entered

* @return cost monthly health care premium

* @throw IllegalArgumentException if the plan is not 'e','E','c','C','s','S','f', or 'F'

*/

public static int getPremium(char plan, boolean smoker, int weight, int height, boolean spouseSmoker, int spouseWeight, int spouseHeight) {

int cost = 0; //initialized variable

if (Character.toLowerCase(plan) == 'e') {

cost = cost + EMPLOYEE_RATE;

} else if (Character.toLowerCase(plan) == 'c') {

cost = cost + EMPLOYEE_CHILDREN_RATE;

} else if (Character.toLowerCase(plan) == 's') {

cost = cost + EMPLOYEE_SPOUSE_RATE;

if (spouseSmoker == false) {

cost = cost + NONSMOKER_DISCOUNT;

}

double bmi = calculateBMI(spouseWeight, spouseHeight);

if (bmi < NOT_OVERWEIGHT_BMI) {

//not overweight

cost = cost + NOT_OVERWEIGHT_DISCOUNT;

} else if (bmi >= OBESE_BMI) {

//obese

cost = cost + OBESE_SURCHARGE;

}

} else if (Character.toLowerCase(plan) == 'f') {

cost = cost + EMPLOYEE_FAMILY_RATE;

if (spouseSmoker == false) {

cost = cost + NONSMOKER_DISCOUNT;

}

double bmi = calculateBMI(spouseWeight, spouseHeight);

if (bmi < NOT_OVERWEIGHT_BMI) {

cost = cost + NOT_OVERWEIGHT_DISCOUNT;

} else if (bmi >= OBESE_BMI) {

cost = cost + OBESE_SURCHARGE;

}

} else {

throw new IllegalArgumentException("Invalid input for health care premium");

}

double bmi = calculateBMI(weight, height);

if (bmi < NOT_OVERWEIGHT_BMI) {

cost = cost + NOT_OVERWEIGHT_DISCOUNT;

} else if (bmi >= OBESE_BMI) {

cost = cost + OBESE_SURCHARGE;  

}

if (smoker == false) {

cost = cost + NONSMOKER_DISCOUNT;

}

return cost;

}

}

However, I am having some logic errors as the program for some reason is not accepting 'y' or 'n' for the smoking response, and terminates after that. Can someone help me finish this program?

Health Plan Monthly Premium Employee $ 105 Employee and Children $ 290 Employee and Spouse $ 670 Employee and Family $ 710

Explanation / Answer

Your code was causing input issues due to the nextInt() method of Scanner class. The problem is when you call nextInt() method, the new line character is left in the buffer, which will be captured by the following nextLine() call, so your code was skipping the string input following nextInt() call. In such cases, use the following syntax to capture the data

Int value=Integer.parseInt(console.nextLine());

where console is the Scanner object.

I have also implemented the validation option for height and weight inputs (>0)

// HealthPlan.java

import java.util.*;

public class HealthPlan {

                public static final int EMPLOYEE_RATE = 105;

                public static final int EMPLOYEE_CHILDREN_RATE = 290;

                public static final int EMPLOYEE_SPOUSE_RATE = 670;

                public static final int EMPLOYEE_FAMILY_RATE = 710;

                public static final int NONSMOKER_DISCOUNT = -30;

                public static final int NOT_OVERWEIGHT_DISCOUNT = -15;

                public static final int OBESE_SURCHARGE = 20;

                public static final double NOT_OVERWEIGHT_BMI = 25.0;

                public static final double OBESE_BMI = 30.0;

                public static void main(String[] args) {

                                System.out.print(" Welcome to our new Health Plan! ");

                                System.out

                                                                .print("When prompted, enter the health plan in which you would like to enroll: ");

                                System.out

                                                                .print("E (E-mployee), C (Employee/C-hildren), S (Employee/S-pouse), F (Employee/F-amily). ");

                                System.out

                                                                .print("You will be asked whether or not you smoke and for your weight and height. ");

                                System.out

                                                                .print("If you choose plan S or F, you will be asked whether your spouse smokes and for ");

                                System.out

                                                                .print("his/her weight and height. Your monthly health plan premium will then be displayed. ");

                                Scanner console = new Scanner(System.in);

                                System.out

                                                                .println("Heath Plan (E-mployee, C-hildren, S-pouse, F-amily): ");

                                char plan = console.nextLine().charAt(0);

                                System.out.print("Do you smoke?(y/n): ");

                                String isSmoker = console.nextLine();

                                int weight =0;

                                /**

                                * looping until user enters a valid weight

                                */

                                do{

                                                System.out.print("Weight (lb): ");

                                                /**

                                                * the nextInt() method leaves next line character in the buffer, which

                                                * is why the following nextLine() call skips the data input from the

                                                * user. In such cases use the following technique to capture the data

                                                */

                                                weight = Integer.parseInt(console.nextLine());

                                                if(weight<=0){

                                                                System.out.println("Invalid weight");

                                                }

                                }while(weight<=0);

                               

                                int height=0;

                                /**

                                * looping until user enters a valid height

                                */

                                do{

                                                System.out.print("Height (lb): ");                                            

                                                height = Integer.parseInt(console.nextLine());

                                                if(height<=0){

                                                                System.out.println("Invalid height");

                                                }

                                }while(height<=0);

                                boolean smoker = false;

                                if (isSmoker.equalsIgnoreCase("y")) {

                                                smoker = true;

                                }

                                if (Character.toLowerCase(plan) == 'f'

                                                                || Character.toLowerCase(plan) == 's') {

                                                System.out.print("Spouse Smokes (y/n): ");

                                                String isSpouseSmoker = console.nextLine();

                                                int spouseWeight;

                                                /**

                                                * looping until user enters a valid spouse weight

                                                */

                                                do{

                                                                System.out.print("Spouse Weight (lb): ");                                           

                                                                spouseWeight = Integer.parseInt(console.nextLine());

                                                                if(spouseWeight<=0){

                                                                                System.out.println("Invalid weight");

                                                                }

                                                }while(spouseWeight<=0);

                                                int spouseHeight = 0;

                                                /**

                                                * looping until user enters a valid spouse height

                                                */

                                                do{

                                                                System.out.print("Spouse Height (lb): ");                                            

                                                                spouseHeight = Integer.parseInt(console.nextLine());

                                                                if(spouseHeight<=0){

                                                                                System.out.println("Invalid height");

                                                                }

                                                }while(spouseHeight<=0);

                                                boolean spouseSmoker = false;

                                                if (isSpouseSmoker.equalsIgnoreCase("y")) {

                                                                spouseSmoker = true;

                                                }

                                                System.out.println("Monthly Health Plan Premium: $"

                                                                                + getPremium(plan, smoker, weight, height, spouseSmoker,

                                                                                                                spouseWeight, spouseHeight));

                                } else if (Character.toLowerCase(plan) == 'e'

                                                                || Character.toLowerCase(plan) == 'c') {

                                                boolean spouseSmoker = true;

                                                // using this height and weight spouse will always be overweight and

                                                // not charged

                                                int spouseWeight = 150;

                                                int spouseHeight = 62;

                                                System.out.println("Monthly Health Plan Premium: $"

                                                                                + getPremium(plan, smoker, weight, height, spouseSmoker,

                                                                                                                spouseWeight, spouseHeight));

                                }

                }

               

                /**

                *

                * Calculates and returns BMI value using the formula given above

                *

                * @param weight

                *            of individual entered

                *

                * @param height

                *            of individual entered

                *

                * @return bmi

                */

                public static double calculateBMI(int weight, int height) {

                                double bmi = ((weight / Math.pow(height, 2)) * 703);

                                return bmi;

                }

                /**

                *

                * Calculates the cost of health plan premium

                *

                * @param plan

                *

                * @param smoker

                *

                * @param weight

                *            of individual entered

                *

                * @param height

                *            of individual entered

                *

                * @param spouseSmoker

                *

                * @param spouseWeight

                *            of spouse entered

                *

                * @param spouseHeight

                *            of spouse entered

                *

                * @return cost monthly health care premium

                *

                * @throw IllegalArgumentException if the plan is not

                *        'e','E','c','C','s','S','f', or 'F'

                */

                public static int getPremium(char plan, boolean smoker, int weight,

                                                int height, boolean spouseSmoker, int spouseWeight, int spouseHeight) {

                                int cost = 0; // initialized variable

                                if (Character.toLowerCase(plan) == 'e') {

                                                cost = cost + EMPLOYEE_RATE;

                                } else if (Character.toLowerCase(plan) == 'c') {

                                                cost = cost + EMPLOYEE_CHILDREN_RATE;

                                } else if (Character.toLowerCase(plan) == 's') {

                                                cost = cost + EMPLOYEE_SPOUSE_RATE;

                                                if (spouseSmoker == false) {

                                                                cost = cost + NONSMOKER_DISCOUNT;

                                                }

                                                double bmi = calculateBMI(spouseWeight, spouseHeight);

                                                if (bmi < NOT_OVERWEIGHT_BMI) {

                                                                // not overweight

                                                                cost = cost + NOT_OVERWEIGHT_DISCOUNT;

                                                } else if (bmi >= OBESE_BMI) {

                                                                // obese

                                                                cost = cost + OBESE_SURCHARGE;

                                                }

                                } else if (Character.toLowerCase(plan) == 'f') {

                                                cost = cost + EMPLOYEE_FAMILY_RATE;

                                                if (spouseSmoker == false) {

                                                                cost = cost + NONSMOKER_DISCOUNT;

                                                }

                                                double bmi = calculateBMI(spouseWeight, spouseHeight);

                                                if (bmi < NOT_OVERWEIGHT_BMI) {

                                                                cost = cost + NOT_OVERWEIGHT_DISCOUNT;

                                                } else if (bmi >= OBESE_BMI) {

                                                                cost = cost + OBESE_SURCHARGE;

                                                }

                                } else {

                                                throw new IllegalArgumentException(

                                                                                "Invalid input for health care premium");

                                }

                                double bmi = calculateBMI(weight, height);

                                if (bmi < NOT_OVERWEIGHT_BMI) {

                                                cost = cost + NOT_OVERWEIGHT_DISCOUNT;

                                } else if (bmi >= OBESE_BMI) {

                                                cost = cost + OBESE_SURCHARGE;

                                }

                                if (smoker == false) {

                                                cost = cost + NONSMOKER_DISCOUNT;

                                }

                                return cost;

                }

}

/*OUTPUT*/

                Welcome to our new Health Plan!

When prompted, enter the health plan in which you would like to enroll:

E (E-mployee), C (Employee/C-hildren), S (Employee/S-pouse), F (Employee/F-amily).

You will be asked whether or not you smoke and for your weight and height.

If you choose plan S or F, you will be asked whether your spouse smokes and for

his/her weight and height. Your monthly health plan premium will then be displayed.

Heath Plan (E-mployee, C-hildren, S-pouse, F-amily):

f

Do you smoke?(y/n): n

Weight (lb): 150

Height (lb): 65

Spouse Smokes (y/n): y

Spouse Weight (lb): -120

Invalid weight

Spouse Weight (lb): 250

Spouse Height (lb): 0

Invalid height

Spouse Height (lb): 71

Monthly Health Plan Premium: $685