I'm writing an object-oriented version of our previous homework assignment, but I keep getting the wrong int values. This class is fulfilling when I get a program to work, but incredibly frustrating when I can't figure things out.
Here's my IdealWeight class which contains at least one logical error:
public class IdealWeight { int ft, in, totalInches; int maleWeight, maleAllowance, femaleWeight, femaleAllowance; final int INCHES_PER_FT = 12; final double PERCENT_ALLOWANCE = 0.15; public IdealWeight(int feet, int inches) { in = inches; ft = feet; } public void setFeet(int feet) { ft = feet; } public int getFeet() { return ft; } public void setInches(int inches) { in = inches; } public int getInches() { return in; } public void setHeight() { totalInches = ft * INCHES_PER_FT + in; } public void computeWeight() { maleWeight = 106 + 6 * (totalInches - 5 * INCHES_PER_FT); maleAllowance = (int) (PERCENT_ALLOWANCE * maleWeight); femaleWeight = 100 + 5 * (totalInches - 5 * INCHES_PER_FT); femaleAllowance = (int) (PERCENT_ALLOWANCE * femaleWeight); } public int getMaleWeight() { return maleWeight; } public int getMaleAllowance() { return maleAllowance; } public int getFemaleWeight() { return femaleWeight; } public int getFemaleAllowance() { return femaleAllowance; } }
And here is the driver program that my instructor provided:
// Driver program to test the IdealWeight Class import java.util.Scanner; public class IdealWeightTest { //--------------------------------------------------------- // Read in a user's height and compute the ideal weight. //--------------------------------------------------------- public static void main (String[] args) { Scanner scan = new Scanner(System.in); System.out.println ("Please enter your height in feet and inches..."); System.out.print ("Feet: "); int feet = scan.nextInt(); System.out.print ("Inches: "); int inches = scan.nextInt(); IdealWeight wt = new IdealWeight(feet, inches); wt.computeWeight(); int maleWt = wt.getMaleWeight(); System.out.println ("The ideal weight for a " + feet + " foot " + inches + " male is " + maleWt + " pounds."); int allowance = wt.getMaleAllowance(); System.out.println ("A weight in the range " + (maleWt - allowance) + " to " + (maleWt + allowance) + " is okay."); int femaleWt = wt.getFemaleWeight(); System.out.println ("The ideal weight for a " + feet + " foot " + inches + " female is " + femaleWt + " pounds."); allowance = wt.getFemaleAllowance(); System.out.println ("A weight in the range " + (femaleWt - allowance) + " to " + (femaleWt + allowance) + " is okay."); } }
To test the program, I've been using 5 feet 3 inches.
I should get back:
The ideal weight for a 5 foot 3 male is 124 pounds.
A weight in the range 106 to 142 is okay.
The ideal weight for a 5 foot 3 female is 115 pounds.
A weight in the range 98 to 132 is okay.
But instead I get:
The ideal weight for a 5 foot 3 male is -254 pounds.
A weight in the range -216 to -292 is okay.
The ideal weight for a 5 foot 3 female is -200 pounds.
A weight in the range -170 to -230 is okay.
Why are the numbers negative? Thanks in advance!