Results 1 to 7 of 7
- 03-12-2010, 08:26 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 6
- 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:
And here is the driver program that my instructor provided: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; } }
To test the program, I've been using 5 feet 3 inches.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.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."); } }
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!
- 03-12-2010, 08:37 PM #2
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Where are you calling setHeight()?
-
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, 08:54 PM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Better yet, consider persisting only totalInches, and having your getters and setters do the appropriate math.
-Gary-
- 03-12-2010, 10:24 PM #5
Member
- Join Date
- Mar 2010
- Posts
- 6
- 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?
- 03-13-2010, 12:34 AM #6
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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, 12:39 AM #7
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Similar Threads
-
Please I need the solution to this
By debobbt in forum New To JavaReplies: 4Last Post: 12-18-2009, 04:34 AM -
Please give me a solution
By ivvgangadhar in forum AWT / SwingReplies: 5Last Post: 11-14-2008, 01:43 PM -
solution for my project
By shkelqa in forum AWT / SwingReplies: 4Last Post: 05-28-2008, 10:31 PM -
solution for my project
By themburu in forum Java AppletsReplies: 4Last Post: 05-21-2008, 01:03 PM -
Please need solution
By prithvi in forum New To JavaReplies: 4Last Post: 04-22-2008, 01:27 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks