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.

# Thread: Calculating Fuel After Distance

1. ## 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.

```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;
}

public void addPetrol(double litres)	{
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));

}
}```

```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

//testAddPetrol
Car c2 = new Car(30,10);
c2.addPetrol(20);
c2.getPetrol();
c2.addPetrol(20);
c2.getPetrol();

//testDrive
Car c3 = new Car(30, 10);
c3.addPetrol(20);
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);

}
}```

```public class CarTest {

/**
* Test method for {@link weeklytask4.Car#Car(double, double)}.
*/
@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 method for {@link weeklytask4.Car#addPetrol(double)}.
*/
@Test
public final void testAddPetrol() {
Car c = new Car(30,10);
c.addPetrol(20);
assertEquals("After adding 20l of petrol", 20.,c.getPetrol(),0.01);
c.addPetrol(20);
assertEquals("After adding 20l more of petrol", 30.,c.getPetrol(),0.01);
}

/**
* Test method for {@link weeklytask4.Car#drive(double)}.
*/
@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 method for {@link weeklytask4.Car#setEfficiency(double)}.
*/
@Test
public final void testSetEfficiency()
{
Car c = new Car(30,10);
c.setEfficiency(5);
assertEquals("Changing fuel efficiency",5.,c.getEfficiency(),0.01);
}

/**
* Test method for {@link weeklytask4.Car#setPetrol(double)}.
*/
@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 method for {@link weeklytask4.Car#setCapacity(double)}.
*/
@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);
}

}```

2. ## Re: Calculating Fuel After Distance

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.

3. ## 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
testAddPetrol - passed
testDrive - passed
testSetEfficiency - passed
testSetPetrol - failed
testSetCapacity - failed

The two failures are due to typos. Check out your asserts and you should see what's wrong. Otherwise i dont see anything wrong going on. I corrected the typos and everything passed.

4. ## Re: Calculating Fuel After Distance

Great. Thanks very much for the help Chris.