# Calculating Fuel After Distance

• March 22nd, 2013, 12:01 AM
floorplay
Calculating Fuel After Distance
I'm trying to calculate the amount of petrol that is left over given a particular distance and fuel efficiency. The 'drive' method is suppose to calculate this but I'm not getting the correct output.

Below is my Car class the main method and the tester class. 'testCar' and 'testAddPetrol' are passing but I can't get 'testDrive' to pass. Any hints as to what I'm doing wrong? Or the way I can calculate the petrol left over in the 'drive' method.

Thanks.

Code :

public class Car {

private double tankSize;		//capacity of tank
private double efficiency;		//litres per 100km
private double gasInTank;		//litres in tank

Car(int initTankSize_, double initEfficiency_)	{
tankSize = initTankSize_;
efficiency = initEfficiency_;
gasInTank = 0;
}

public double getPetrol()	{
return gasInTank;
}

public double getEfficiency()	{
return efficiency;
}

public double getCapacity()	{
return tankSize;
}

gasInTank = gasInTank + litres;
if(gasInTank > tankSize)	{
gasInTank = tankSize;
}
}

//THIS IS WHERE I'M HAVING TROUBLE
public void drive(double km){
double distance = (gasInTank * efficiency);
if (distance > km)	{
distance = km;
}
gasInTank = (gasInTank - (distance/efficiency));

}
}

Code :

public class CarMain {

public static void main(String[] args) {

//TestCar
Car c = new Car(60, 10);
c.getPetrol();  					//returns gasInTank from initial constructor
c.getCapacity();					//returns tankSize from initial constructor
c.getEfficiency();					//returns efficiency from initial constructor

Car c2 = new Car(30,10);
c2.getPetrol();
c2.getPetrol();

//testDrive
Car c3 = new Car(30, 10);
c3.drive(100);		;
double petrolLeft = c3.getPetrol();
System.out.println("This: " +petrolLeft);
c3.drive(10000);
System.out.println("After 10000: " +c3.getPetrol());

//testSetEfficiency
Car c4 = new Car(30,10);

}
}

Code :

public class CarTest {

/**
*/
@Test
public final void testCar() {
Car c = new Car(60,10);
assertEquals("Petrol in constructor",0.0,c.getPetrol(),0.01);
assertEquals("Capacity in constructor",60.0,c.getCapacity(),0.01);
assertEquals("Efficiency in constructor",10.0,c.getEfficiency(),0.01);
}

/**
*/
@Test
Car c = new Car(30,10);
assertEquals("After adding 20l of petrol", 20.,c.getPetrol(),0.01);
assertEquals("After adding 20l more of petrol", 30.,c.getPetrol(),0.01);
}

/**
*/
@Test
public final void testDrive() {
Car c = new Car(30,10);
c.setPetrol(20);
c.drive(100);
assertEquals("After 100 km", 10.,c.getPetrol(),0.01);
c.drive(10000);
assertEquals("After 10000 km", 0.,c.getPetrol(),0.01);
}

/**
*/
@Test
public final void testSetEfficiency()
{
Car c = new Car(30,10);
c.setEfficiency(5);
assertEquals("Changing fuel efficiency",5.,c.getEfficiency(),0.01);
}

/**
*/
@Test
public final void testSetPetrol()
{
Car c = new Car(30,10);
c.setPetrol(5);
assertEquals("Changing fuel petrol",5.,c.getPetrol(),0.01);
c.setPetrol(29);
assertEquals("Changing fuel petrol",29.,c.getPetrol(),0.01);
c.setPetrol(35);
assertEquals("Changing fuel petrol",30.,c.getPetrol(),0.01);
}

/**
*/
@Test
public final void testSetCapacity()
{
Car c = new Car(30,10);
c.setPetrol(25);
c.setCapacity(35);
assertEquals("Changing fuel capacity",25.,c.getPetrol(),0.01);
assertEquals("Changing fuel capacity",35.,c.getCapacity(),0.01);
c.setCapacity(20);
assertEquals("Changing fuel capacity",20.,c.getPetrol(),0.01);
assertEquals("Changing fuel capacity",20.,c.getCapacity(),0.01);
}

}

• March 22nd, 2013, 05:30 AM
Norm
Re: Calculating Fuel After Distance
Quote:

I'm not getting the correct output.
Please post the program's output and add some comments saying what is wrong with the output and show what the output should be.
• March 22nd, 2013, 07:10 AM
Chris.Brown.SPE
Re: Calculating Fuel After Distance
Had to add setters to your car class, did you leave those out of the code you copied or are you really missing them? Just ran your junit test and got the following results.

testCar - passed