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: Calculating Fuel After Distance

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default 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.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  4. #4
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calculating Fuel After Distance

    Great. Thanks very much for the help Chris.

Similar Threads

  1. Distance not being calculated properly
    By floorplay in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 18th, 2013, 08:54 AM
  2. Distance function help
    By abf617 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 2nd, 2012, 06:32 PM
  3. Getting Distance between two addresses
    By Shockwave786 in forum Java Theory & Questions
    Replies: 3
    Last Post: July 23rd, 2012, 10:19 AM
  4. i am having a problem with the distance formula,help needed!
    By Bentino in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 22nd, 2012, 07:07 PM
  5. Distance between 2 points
    By captain in forum Java Theory & Questions
    Replies: 3
    Last Post: February 22nd, 2012, 12:53 AM