Friday, January 8, 2010

I need help with creating a Car object in java?

# Implement a Car class with the following properties.





1. A Car keeps track of the starting odometer reading, ending odometer reading, and the number of gallons used between readings.


2. The initial odometer reading is specified in the constructor.


3. A method calculateMPG calculates and returns the miles per gallon for the car.


4. A method fillUp simulates filling up the tank at a gas station: odometerReading is the current odometer reading and gallons is the number of gallons that filled the tank. Save these values in instance variables.


5. With this information, miles per gallon can be calculated. calculateMPG will calculate the miles per gallon since the last time resetMPG was called.





I have all the way up to 3, but I don't know how to do #4.





Our teacher started the method for us by writing:





void fillUp(int odometerReading, double gallons) {





}





but what actually goes in between i don't knowI need help with creating a Car object in java?
Call your odometer 'odometer', and set up another variable 'gallons_used', and one miles_travelled


Your method should go like this:


void fillUp(int odometerReading, double gallons) {


miles_travelled = odometerReading - odometer;


gallons_used = gallons;


odometer = odometerReading;


}


Public double calculate MPG(){


return miles_travelled/gallons_used;


}


public void resetMPG(){


gallons_used = 0;


miles_travelled =0;


}


hope this helpsI need help with creating a Car object in java?
Get solved with more java related queries here by visiting the following websites.

No comments:

Post a Comment