5 Replies - 1039 Views - Last Post: 03 May 2013 - 11:07 PM Rate Topic: -----

#1 newGains  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 07-April 13

stuck in infinite loop

Posted 03 May 2013 - 01:04 PM

here is the instructions for this assignment:
For this assignment, you will design a set of classes that work together to simulate a car's fuel gauge and odometer. The classes you will design are the following:

The FuelGauge Class: This class will simulate a fuel gauge. Its responsibilities are as follows:

• To know the car’s current amount of fuel, in gallons.
• To report the car’s current amount of fuel, in gallons.
• To be able to increment the amount of fuel by 1 gallon. This simulates putting fuel in the car. ( The car can hold a maximum of 15 gallons.)
• To be able to decrement the amount of fuel by 1 gallon, if the amount of fuel is greater than 0 gallons. This simulates burning fuel as the car runs.

The Odometer Class: This class will simulate the car’s odometer. Its responsibilities are as follows:

• To know the car’s current mileage.
• To report the car’s current mileage.
• To be able to increment the current mileage by 1 mile. The maximum mileage the odometer can store is 999,999 miles. When this amount is exceeded, the odometer resets the current mileage to 0.
• To be able to work with a FuelGauge object. It should decrease the FuelGauge object’s current amount of fuel by 1 gallon for every 24 miles traveled. ( The car’s fuel economy is 24 miles per gallon.)

Demonstrate the classes by creating instances of each. Simulate filling the car up with fuel, and then run a loop that increments the odometer until the car runs out of fuel. During each loop iteration, print the car’s current mileage and amount of fuel.


PROBLEM: I am stuck in an infinite loop and my gallons don't seem to be decrementing.
I am not sure how to set up the last point in the Odometer class:
• To be able to work with a FuelGauge object. It should decrease the FuelGauge object’s current amount of fuel by 1 gallon for every 24 miles traveled. ( The car’s fuel economy is 24 miles per gallon.)

FuelGauge Class:
public class FuelGauge
{
	final int MAX_GALLONS = 15;
	private int currentFuel;

	public FuelGauge()
	{
		currentFuel = 0;
	}
	public FuelGauge(int cf)
	{
		currentFuel = cf;
	}
	public void setCurrentFuel(int cf)
	{
		currentFuel = cf;
	}
	public int getCurrentFuel()
	{
		return currentFuel;
	}
	public void addFuel()
	{
		if(currentFuel < MAX_GALLONS)
		{
			currentFuel = currentFuel + 1;
		}
		else
			System.out.println("FUEL TANK IS FULL");
	}
	public void useFuel()
	{
		if(currentFuel > 0)
		{
			currentFuel = currentFuel - 1;
		}
		else
			System.out.println("FUEL TANK IS EMPTY!");
	}
}


Odometer Class:
public class Odometer
{
	final int MAX_MILEAGE = 999999;
	final int MPG = 24;
	private int currentMileage;
	//private int setPoint;
	private FuelGauge fg = new FuelGauge();

	public Odometer()
	{
		currentMileage = 0;
	}
	public Odometer(int curMile, FuelGauge fGauge)
	{
		currentMileage = curMile;
	}
	public void setCurrentMileage(int curMile)
	{
		currentMileage = curMile;
	}
	public int getCurrentMileage()
	{
		return currentMileage;
	}
	public void addMileage()
	{
		if(currentMileage < MAX_MILEAGE)
			currentMileage = currentMileage + 1;
		else
			currentMileage = 0;
	}
}


Demo:
public class CarInstrumentSimulator
{
	public static void main(String [] args)
	{
		FuelGauge fg1 = new FuelGauge();
		Odometer od1 = new Odometer(999900, fg1);

		for(int i = 0; i<fg1.MAX_GALLONS;i++)
			fg1.addFuel();

		while(fg1.getCurrentFuel()>0)
		{
			od1.addMileage();
			System.out.println("Mileage: " + od1.getCurrentMileage());

			System.out.println("Fuel Level: " + fg1.getCurrentFuel() + " Gallons");
		}
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: stuck in infinite loop

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,686
  • Joined: 19-March 11

Re: stuck in infinite loop

Posted 03 May 2013 - 01:10 PM

		while(fg1.getCurrentFuel()>0)
		{
			od1.addMileage();
			System.out.println("Mileage: " + od1.getCurrentMileage());

			System.out.println("Fuel Level: " + fg1.getCurrentFuel() + " Gallons");
		}
	}



Looks to me like this is the problem point. The way to diagnose this is to figure out why fg1.getCurrentFuel() always returns a value >0. Looks to me like nothing that happens in the loop is likely to change the current fuel level - if it does, that's a whole different issue, because checking the level and getting the current mileage shouldn't change the amount of fuel in the tank. So I'm guessing it just keeps checking the same value until you kill the process.
Was This Post Helpful? 0
  • +
  • -

#3 newGains  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 07-April 13

Re: stuck in infinite loop

Posted 03 May 2013 - 01:25 PM

View Postjon.kiparsky, on 03 May 2013 - 01:10 PM, said:

		while(fg1.getCurrentFuel()>0)
		{
			od1.addMileage();
			System.out.println("Mileage: " + od1.getCurrentMileage());

			System.out.println("Fuel Level: " + fg1.getCurrentFuel() + " Gallons");
		}
	}



Looks to me like this is the problem point. The way to diagnose this is to figure out why fg1.getCurrentFuel() always returns a value >0. Looks to me like nothing that happens in the loop is likely to change the current fuel level - if it does, that's a whole different issue, because checking the level and getting the current mileage shouldn't change the amount of fuel in the tank. So I'm guessing it just keeps checking the same value until you kill the process.


ok so i am pretty sure it is my addMileage() that is causing the problem and i know what i want to do i just can't seem to figure out how to do it. i want for every 24 miles the odometer is incremented to then call the useFuel() which will decrement the fuel gauge by 1 gallon. how could i set a variable to keep track of how many miles is added to the current miles already on the odometer. if the odometer always started at 0 i could just simply divide by 24 and decrement X amount of times but that wouldn't work for if i already have XXXXXX amount of miles on the odometer already.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,686
  • Joined: 19-March 11

Re: stuck in infinite loop

Posted 03 May 2013 - 01:45 PM

Don't take offense, but I'm at work, don't have time to figure out what you just said, or what you meant.

Go back to that loop and figure out what method you should be calling that changes the current fuel level. Call that method in the loop. If such a method doesn't exist, make one.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: stuck in infinite loop

Posted 03 May 2013 - 06:58 PM

View PostnewGains, on 03 May 2013 - 04:25 PM, said:

ok so i am pretty sure it is my addMileage() that is causing the problem and i know what i want to do

Don't want to be rude but why don't you just fix it ?
Was This Post Helpful? 1
  • +
  • -

#6 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: stuck in infinite loop

Posted 03 May 2013 - 11:07 PM

Quote

ok so i am pretty sure it is my addMileage() that is causing the problem and i know what i want to do i just can't seem to figure out how to do it. i want for every 24 miles the odometer is incremented to then call the useFuel() which will decrement the fuel gauge by 1 gallon. how could i set a variable to keep track of how many miles is added to the current miles already on the odometer. if the odometer always started at 0 i could just simply divide by 24 and decrement X amount of times but that wouldn't work for if i already have XXXXXX amount of miles on the odometer already.

There are multiple problems in your code, but I'll address them one at a time. Firstly, in order to decrement the fuel gauge, you'll need access to the FuelGauge object. Ok, well you create a new FuelGauge object in your Odometer class, so that's a start. It's wrong (see below), but it's a start. Now your addMileage method can work if you apply the above math. But you're confused on the math. How can you know when you start your journey? The answer is that you can't. You're not supposed to. You are supposed to decrement the gas mileage every time your odometer drives 24 miles. Or in other words, every time currentMileage is a multiple of 24. (Hint: Research the modulus operator, %)

This is now below: Ok, I said you were wrong because you created a new FuelGauge object in your Odometer class. But why did I say that? For one, you are unable to access the FuelGauge from outside of the Odometer. I'm pretty sure other classes (or parts of the car) need to access the FuelGauge. You are already on the right track by creating a new object in the main method. But the FuelGauge object in the main method and the FuelGauge object in the Odometer class are two different objects. Adding fuel to one won't affect the other, and vice versa every which way. You want the FuelGauge in your main method to be exactly the same as the one you reference in your Odometer class. To do this, you'll need to pass it as a parameter, whether to the method or the class constructor.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1