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 4 of 4

Thread: Java uberNoob, requesting help with simple loop issue

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Java uberNoob, requesting help with simple loop issue

    Hi everyone. I am just starting out with Java, and have encountered this problem with a for loop. I'm not sure that's the ideal loop for this problem, but it's what I have right now.

    Here's the problem:
    Write a class named GasTank containing:
    • An instance variable named amount of type double, initialized to 0.
    • An instance variable named capacity of type double.
    • A constructor that accepts a parameter of type double. The value of the parameter is used to initialize the value of capacity .
    • A method named addGas that accepts a parameter of type double . The value of the amount instance variable is increased by the value of the parameter. However, if the value of amount is increased beyond the value of capacity , amount is set to capacity .
    • A method named useGas that accepts a parameter of type double . The value of the amount instance variable is decreased by the value of the parameter. However, if the value of amount is decreased below 0 , amount is set to 0 .
    • A method named isEmpty that accepts no parameters. isEmpty returns a boolean value: true if the value of amount is less than 0.1 , and false otherwise.
    • A method named isFull that accepts no parameters. isFull returns a boolean value: true if the value of amount is greater than capacity-0.1 , and false otherwise.
    • A method named getGasLevel that accepts no parameters. getGasLevel returns the value of the amount instance variable.
    • A method named fillUp that accepts no parameters. fillUp increases amount to capacity and returns the difference between the value of capacity and the original value of amount (that is, the amount of gas that is needed to fill the tank to capacity).


    Here's what I have so far:

    class GasTank {
    		double amount = 0;
    		double capacity;
     
    	  public GasTank(double x) {
    		capacity = x;
                    }
     
    	  public void addGas(double a){
    		amount += a;
    			if(amount > capacity){
    				amount = capacity;
    			}
    	  }
     
    	  public void useGas(double b) {	
    	     amount -= b;
    		if(amount < 0) {
    			amount = 0;
    		}
    	  }
     
    	  public boolean isEmpty(){
    	     if(amount < 0.1) {
    		return true;
     	     }
    	     else{return false;
    	     }	
    	  }
     
    	  public boolean isFull(){
    	     if(amount > capacity - 0.1) {
    		return true;
     	     }
    	     else {return false;
    	     }	
    	  }
     
     
              public double getGasLevel() {	
    	      return amount;
              }
     
             //doesn’t return difference of capacity and old amount
             public double fillUp() {
    	      int i = amount;
    	      for(i=0; i<capacity; i++) {
    	      }
    		return capacity-amount;
             }
     
    }


    The issue is just with the last method, fillUp(). I'm getting an error message saying that " fillUp doesn’t return difference of capacity and old amount."

    I'm quite sure this is an insanely easy problem, but being so new, my understanding is limited.

    Thanks in advance.
    Last edited by miketeezie; February 21st, 2011 at 09:29 PM. Reason: question answered


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Java uberNoob, requesting help with simple loop issue

    Explain your process for the fillUp() method. There seems to be some logistical confusion here.

    For example
    1. Why are you using a loop if you are not actually doing anything with the loop?
    2. Why are initializing i to equal amount, but then immediately setting i to 0 in your loop?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following User Says Thank You to aussiemcgr For This Useful Post:

    miketeezie (February 21st, 2011)

  4. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Java uberNoob, requesting help with simple loop issue

    So your code:
             public double fillUp() {
    	      int i = amount;
    	      for(i=0; i<capacity; i++) {
    	      }
    		return capacity-amount;
             }

    is doing very little, I think you are overcomplicating the problem a bit.

    Using your method, the returned value would be quite inaccurate(I think, not actually sure what you're trying to do), as you are going up in increments of one for a double value. However, assuming this is what you want, you must first not reset i at the start of the loop, and secondly, as aussie said above, you should probably tell us what's happening in that for loop

    Try this:

             public double fillUp() {
                  double difference = capacity-amount;
    	      for(i=amount; i<capacity; i++) {
                           amount++;
    	      }
    		return difference;
             }

    that would carry the effect of filling up the tank, but it is likely to overfill. A much simpler, and more accurate way of filling up the tank would be:

             public double fillUp() {
                   double difference = capacity-amount;
    	        amount = capacity 
    		return difference;
             }

  5. The Following User Says Thank You to Ciaran54 For This Useful Post:

    miketeezie (February 21st, 2011)

  6. #4
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java uberNoob, requesting help with simple loop issue

    Quote Originally Posted by Ciaran54 View Post
    So your code:

    is doing very little, I think you are overcomplicating the problem a bit.

    Using your method, the returned value would be quite inaccurate(I think, not actually sure what you're trying to do), as you are going up in increments of one for a double value. However, assuming this is what you want, you must first not reset i at the start of the loop, and secondly, as aussie said above, you should probably tell us what's happening in that for loop

    Try this:

             public double fillUp() {
                  double difference = capacity-amount;
    	      for(i=amount; i<capacity; i++) {
                           amount++;
    	      }
    		return difference;
             }

    that would carry the effect of filling up the tank, but it is likely to overfill. A much simpler, and more accurate way of filling up the tank would be:

             public double fillUp() {
                   double difference = capacity-amount;
    	        amount = capacity 
    		return difference;
             }
    Thank you guys so much. It's a little embarrassing - this is my first time to ever try to sit down and write any Java code, so I am definitely almost completely ignorant. I'm still trying to wrap my mind around how to implement loops to accomplish a task.

    I don't know why I tried to implement a loop here, this is so much more obvious.

    The second version of the fillUp() method worked, it just needed a semicolon at the end of the third line.

    Again, thanks so much!
    Last edited by miketeezie; February 21st, 2011 at 09:20 PM.

Similar Threads

  1. Game requesting user input
    By Neo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 3rd, 2011, 10:00 PM
  2. [SOLVED] Simple While loop wont work??
    By chuckie987 in forum Loops & Control Statements
    Replies: 1
    Last Post: January 31st, 2011, 02:58 PM
  3. [SOLVED] ArrayOutofBoundary in Simple While Loop
    By Johnpower in forum Loops & Control Statements
    Replies: 6
    Last Post: June 13th, 2010, 04:31 AM
  4. Weird issue with while loop ending/being skipped
    By ang3c0 in forum Loops & Control Statements
    Replies: 4
    Last Post: December 25th, 2009, 12:09 PM
  5. boolean value in a simple loop (do-while)
    By chronoz13 in forum Loops & Control Statements
    Replies: 5
    Last Post: October 18th, 2009, 12:05 AM