Results 1 to 7 of 7
  1. #1
    bengiles89 is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Unhappy 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.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!

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    Where are you calling setHeight()?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Better yet, consider persisting only totalInches, and having your getters and setters do the appropriate math.

    -Gary-

  5. #5
    bengiles89 is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default

    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. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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. #7
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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-

Similar Threads

  1. Please I need the solution to this
    By debobbt in forum New To Java
    Replies: 4
    Last Post: 12-18-2009, 05:34 AM
  2. Please give me a solution
    By ivvgangadhar in forum AWT / Swing
    Replies: 5
    Last Post: 11-14-2008, 02:43 PM
  3. solution for my project
    By shkelqa in forum AWT / Swing
    Replies: 4
    Last Post: 05-28-2008, 11:31 PM
  4. solution for my project
    By themburu in forum Java Applets
    Replies: 4
    Last Post: 05-21-2008, 02:03 PM
  5. Please need solution
    By prithvi in forum New To Java
    Replies: 4
    Last Post: 04-22-2008, 02:27 PM

Posting Permissions

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