# CS201 Homework (Need help not solution)

• 03-12-2010, 09:26 PM
bengiles89
CS201 Homework (Need help not solution)
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:
Code:

```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:
Code:

```// 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.

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!
• 03-12-2010, 09:37 PM
StormyWaters
Where are you calling setHeight()?
• 03-12-2010, 09:38 PM
Fubarable
You might want to call setHeight() in your constructor after you've initialized your feet and inches. Do you know why?

Edit: too late! :)
• 03-12-2010, 09:54 PM
gcalvin
Better yet, consider persisting only totalInches, and having your getters and setters do the appropriate math.

-Gary-
• 03-12-2010, 11:24 PM
bengiles89
Maybe I have larger problems than I thought. I only added setHeight() because of this UML provided by my instructor. Could you guys help me decode it and understand how I should alter my class file?

http://i39.tinypic.com/16gh3zq.jpg
• 03-13-2010, 01:34 AM
gcalvin
Well, you didn't implement setHeight() according to the UML. The UML calls for setHeight(int feet, int inches), and you implemented a setHeight() with no parameters that updates the instance variable totalInches based on the ft and in instance variables, and that method never gets called, so totalInches never gets updated. The UML doesn't say anything about totalInches, but totalInches is what you apparently need for your computeWeight() method.

If you're committed to the UML (which has its weirdnesses -- it seems strange to me to be talking about feet and inches as if they are separate entities) then keep the ft and in instance variables, and get rid of totalInches. Instead, write a getTotalInches() method that calculates from ft and in, like your current setHeight() method does, and fix setHeight(int feet, int inches) so that it updates ft and in like it's supposed to do.

-Gary-
• 03-13-2010, 01:39 AM
gcalvin
Also, shouldn't your setter methods be checking for sane values? What if you get setFeet(-2) or setInches(17)? Maybe the UI is supposed to take care of that, but it's a good idea to watch for it in the setters too.

-Gary-