Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: CS201 Homework (Need help, not solution)

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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:
    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!


  2. #2
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: CS201 Homework (Need help, not solution)

    OK I can see you put the work in and are genuinely stuck. In programming one of the most important things to learn is how to debug and I will try to point you in the right direction without making it too obvious as it is a quite simple mistake but one you need to learn to spot yourself.

    Now the simplest way to debug is to use some System.out.prints to show the values of variables at different time at strategic points of your code. One of the best places is right before and after a computation. Now your most important calculations all appear in the computeWeight method so try putting a few in there and see what you can come up with.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: CS201 Homework (Need help, not solution)

    If you are using an IDE with a debugger, you can use breakpoints to pause the execution of code a virtually any point. Depending on the debugger, you should be able to step through code one line at a time, jump into methods being called (also known as "stepping into"), jump out of the current method (also known as "step return", all code up to a return statement is executed), continue normal program operation, and if necessary, kill your program process.

    The most effective method of using a debugger for a problem like this is to put a breakpoint anywhere there is a complex calculation or series of different calculations happening (at the start and end of loops are also good places), and then use the step function to execute code one line at a time. Most debuggers also have a feature for showing the variables available at the time, as well as their current value. Some debuggers will even allow you to change these values while running. Monitor these to make sure that the variables you have are the expected value as you step through the code.

    here's a hint to get you pointed in the right direction: look for a method that isn't being executed.

  4. #4
    Junior Member
    Join Date
    Mar 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CS201 Homework (Need help, not solution)

    I'm using DrJava, but I haven't experimented with the debugging features yet. I also have Eclipse, so I'll definitely look into that. Thanks.

    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?


  5. #5
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: CS201 Homework (Need help, not solution)

    Ah I didn't now Java even had IDEs that could do that the only Java IDE I've ever used was BlueJ in my first semester of learning it(it probably can do that too I can't remember though). In the second semester our lecturer wanted us to compile things with the command line so that's what I'm used to now. I don't want to take this thread off topic but is there any IDE you would recommend? Just downloading eclipse now.

  6. #6
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: CS201 Homework (Need help, not solution)

    OK put this code in the start of the computeWeight method and look at the output.

    System.out.print("Feet: " + ft + "\n");
    System.out.print("Inches: " + in + "\n");
    System.out.print("Total Inches: " + totalInches + "\n");

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: CS201 Homework (Need help, not solution)

    Two very good (and completely free) java IDE's: Eclipse and Netbeans. From what i've heard, they're pretty much on equal footing so it's all a personal preference thing (Netbeans does have a very good GUI designer, but I I like designing my GUI's by hand). FYI, I like eclipse because that's what I started on.

    Here's how I would layout the IdealWeight class:

    Instead of having to call re-calculate everytime, it would make sense to declare that method to be private (or protected). Then, whenever you call one of your setter methods like setHeight(), inside of there call the compute() method to update all the data.
    // example
    public void setInches(int inches)
    {
         this.inches = inches;
         this.compute(); // update all of the ideal weights based on the new information
    }
     
    private void comput()
    {
       // ... your code here
    }

Similar Threads

  1. MORE java homework help.
    By TommyFiz in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: October 13th, 2009, 07:15 PM
  2. Homework help
    By cd247 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 11th, 2009, 05:56 PM
  3. need help with homework!
    By programmer12345 in forum Java Theory & Questions
    Replies: 2
    Last Post: September 27th, 2009, 05:34 AM
  4. [SOLVED] What is cast operator and how to use it?
    By napenthia in forum Java Theory & Questions
    Replies: 11
    Last Post: April 27th, 2009, 06:11 AM
  5. Recursive Solution to Knights tour
    By budder8818 in forum Algorithms & Recursion
    Replies: 0
    Last Post: February 4th, 2009, 03:31 PM