Help with simple program

Ok, I'm very new to the java language and have been creating a simple program to emulate, very simply, a car. On creating an object of the class CarFuel the user specifies through a parameter the fuel consumption of the car in km/litre. There are then methods to insert fuel into the car, display how much fuel is in the car, display how far the car can travel on the amount of fuel in the tank, and a method to drive the car a specified distance, which updates the amount of fuel in the car accordingly. Below is the code I have:

/**

* Write a description of class CarFuel here.

*

* @author Richard J White

* @version 22/10/2004

*/

publicclass CarFuel

{

double consumption = 0;

double distance = 0;

double fuel = 0;

/**

* Constructor for objects of class CarFuel

*/

public CarFuel(int fuelConsumption)

{

consumption = fuelConsumption;

}

/**

* Methods for the class CarFuel

*/

publicvoid insertFuel(double litres)

{

fuel = litres;

distance = fuel * consumption;

}

publicvoid drive(double kilometres)

{

if (distance >= kilometres)

{

System.out.println("You have driven " + kilometres +"km");

double fuelUsed = kilometres / consumption;

fuel = fuel - fuelUsed;

}

else

{

System.out.println("You do not have enough fuel to drive this distance.");

}

}

publicvoid getFuel()

{

double fuelLeft = fuel;

System.out.println("You have " + fuelLeft +" litres in the tank.");

}

publicvoid getDistance()

{

double travelDistance;

if (fuel > 0)

{

travelDistance = consumption * fuel;

System.out.println("You can travel " + travelDistance +"km on the available fuel");

}

else

{

System.out.println("You haven't inserted any fuel.");

}

}

}

Now I wondered if someone could check this code for efficiency and also tell me the solution to one problem. I have used doubles to avoid integer division rounding down but this now leaves me with numbers such as 0.19999999999 when viewing the fuel left after travelling 40km on an engine that consumes 50km/litre. Is there a way of rounding these numbers up to 1 significant figure? And how would this affect the program when I request the fuelLeft after not travelling any distance, wouldn't this then be incorrectly 0.5 bigger than the fuel amount?

[4165 byte] By [Phoresis] at [2007-9-30 20:34:32]
# 1

Hi Richard,

I didn't compile or run your code, but I have a just a few thoughts:

(1) I think there's far too much printing of messages going on here. I think designing a good object means having a private state that clients of your class can't see and public methods for manipulating them.

(2) Follow good design practices and make those consumption, distance, and fuel variables private. As written, they're package visible.

(3) The JavaBean standard would have you write getter and setter methods for accessing data like this:

// Private data member

private double fuel;

// Public method for reading value

public double getFuel() { return this.fuel; }

// Public method for writing value

public void setFuel(double newFuel) { this.fuel = newFuel; }

If you don't want someone to be able to read or write a variable, don't implement that method.

(4) If I were writing about the behavior of a car, I might list activities like this:

* I can refuel when my gas tank is empty,

* When I drive along I use up the fuel at a rate that's determined by the efficiency of my car, driving conditions, etc.

* I might like to know how much further I can go on the fuel that remains, based on an assumed consumption rate.

* When I try to drive further than my fuel supply allows I run out of gas.

Based on this, I might write methods that would express these ideas:

public void addFuel(double newFuel);

public void travel(double distance) throws OutOfGasException;

public double getMaxTripDistance();

I might write an OutOfGasException class that extends java.lang.Exception that will tell me when I run out of gas.

Let your methods simply change the state of your car. Have the main method that tests it do all that message printing.

%

duffymo at 2007-7-7 1:24:01 > top of Java-index,Other Topics,Patterns & OO Design...
# 2
I forgot to add: look at java.text.NumberFormat for helping with that decimal formatting problem.%
duffymo at 2007-7-7 1:24:01 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

Plus, you have three variables representing only two datapoints:

You have fuel, consumption, and range (which you call "distance"). You spend a lot of time keeping range in sync with the other two. Just drop the last variable, and rewrite drive() as public void drive(double kilometres) {

double fuelUsed = kilometres / consumption;

if (fuelUsed > fuel) {

threw new OutOfGasException();

}

System.out.println("You have driven " + kilometres +"km");

fuel -= fuelUsed;

}

Also, don't worry much about computational efficiency. Write in an expressive fashion without wasting cycles, and in most cases you'll do fine. Premature optimization, wrote Dr Knuth, is the root of all evil.

Beyond that, everything Duffymo said is correct.

Michael_Lorton at 2007-7-7 1:24:01 > top of Java-index,Other Topics,Patterns & OO Design...