Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hey All, i am having a bit of logical trouble with some custom code. I can\'t se

ID: 3637492 • Letter: H

Question

Hey All, i am having a bit of logical trouble with some custom code. I can't seem to figure out why the gallons in my code begin decrementing after every mile past 5. Sorry it's a bit tough to explain, if you are able i would copy and paste the code so you see the problem. The vehicle gets 5 miles per gallon and has a fuel tank capacity of 5 so it should only be able to travel 25 miles and then have to fill up, but for some reason after 10 miles it asks me to fill up and says its on empty. Here are the 3 classes, 1 is the demo to run the other 2 classes.

public class FuelGauge {

private int fuel = 0;

//reports amount of fuel in tank
public int getFuel(){
//report amount of fuel in tank
return fuel;

}

//method to add gas, increment by 1
public void addGas(){
if (getFuel() >= 0 && getFuel() <= 5)
fuel = getFuel()+ 1;
else
System.out.println("You can not get gas!");
}

//Method to calculate gas usage, decrement by 1
public void useGas(){
if (getFuel() > 0)
fuel = getFuel() - 1;
else
System.out.println("You are out of gas!");

}

//Method to return fuel amount
public String toString(){
String fuelAmount = "Fuel Amount: " + fuel;
return fuelAmount;

}

}



import java.util.Scanner;

public class Odometer {

//Set variables
private int milesTraveled = 0;
private int mileage = 0;
Scanner keyboard = new Scanner(System.in);

//create a FuelGauge Object labeled fg
private FuelGauge fg = new FuelGauge();

//Class Constructor
public Odometer(FuelGauge myFG) {

}

//Drive class that simulates the car driving
public void drive()
{
System.out.println(fg.toString());
System.out.println("You need gas. ");
System.out.println("Would you like gas, enter 1 for yes or 2 for No: ");
double str = keyboard.nextDouble();

//Adds fuel to the car if the user wants to
if (str == 1)
{
while (fg.getFuel() < 5)
fg.addGas();
System.out.println("You have: " + fg.getFuel() + " gallons of gas.");
}
else
System.out.println("Thanks for playing!");


//Use while statement to loop through mileage and gallons
while (fg.getFuel() > 0)
{
this.mileage = mileage + 1;
this.milesTraveled = milesTraveled + 1;
System.out.println("Mileage: " + mileage);

System.out.println(fg.toString());

//Divide miles traveled by 5 to show the 1st gallon has been used
if (milesTraveled/5 == 1)
{
fg.useGas();

}

//Divide miles traveled by 5 to show the 2nd gallon has been used
else if (milesTraveled/5 == 2 )
{
fg.useGas();
}

//Divide miles traveled by 5 to show the 3rd gallon has been used
else if (milesTraveled/5 == 3 )
{
fg.useGas();
}

//Divide miles traveled by 5 to show the 4th gallon has been used
else if (milesTraveled/5 == 4 )
{
fg.useGas();
}

//Divide miles traveled by 5 to show the 5th gallon has been used
else if (milesTraveled/5 == 5)
{
fg.useGas();
milesTraveled = 0;
}

//Resets Odometer Mileage once it passes 999,999 miles
if (mileage == 999999)
{
System.out.println("Reseting your Odometer Mileage to 0.");
mileage = 0;
}

//checks to see if fuel amount is 0
if (fg.getFuel() == 0)
{
System.out.println("You need gas. ");
System.out.println("Would you like gas, enter 1 for yes or 2 for No: ");
double str2 = keyboard.nextDouble();

//Asks the user to get gas inside of while loop
if (str2 == 1)
{
while (fg.getFuel() < 5)
fg.addGas();
System.out.println("You have: " + fg.getFuel());
}
else
System.out.println("Thanks for playing!");
}
}
}
}




public class CarDemo {

public static void main(String[] args) {
FuelGauge myFG = new FuelGauge();
Odometer odometer = new Odometer(myFG);

//starts up myFG and simulates driving
if (myFG.getFuel() >= 0)
odometer.drive();

}
}

Explanation / Answer

its an easy fix your code //Set variables private int milesTraveled = 0; private int mileage = 0; Scanner keyboard = new Scanner(System.in); the private int mileage needs to be a float when you divide by an int it will round up every time so when you do //Divide miles traveled by 5 to show the 2nd gallon has been used else if (milesTraveled/5 == 2 ) { fg.useGas(); } the milesTraveled/5 will round up to two, just change that int milesTraveled to a float milesTraveled and it will work