# Thread: CS201 Homework (Need help not solution)

1. Member
Join Date
Mar 2010
Posts
7
Rep Power
0

## 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:
Java 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:
Java 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.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!

2. Senior Member
Join Date
Feb 2009
Posts
312
Rep Power
10
Where are you calling setHeight()?

3. You might want to call setHeight() in your constructor after you've initialized your feet and inches. Do you know why?

Edit: too late! :)

4. Senior Member
Join Date
Mar 2010
Posts
952
Rep Power
8
Better yet, consider persisting only totalInches, and having your getters and setters do the appropriate math.

-Gary-

5. Member
Join Date
Mar 2010
Posts
7
Rep Power
0
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?

6. Senior Member
Join Date
Mar 2010
Posts
952
Rep Power
8
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-

7. Senior Member
Join Date
Mar 2010
Posts
952
Rep Power
8
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-

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•