Creating
Classes: Constructors, Methods, Instance Fields
The goal for this lab is to gain more experience creating
classes, specifying their interfaces, and implementing those interfaces. We will also see how BlueJ can help us
interactively "unit test" our classes.
Go menu select Connect to Server.... Type or choose afp://fileserver1 and enter your
password if prompted. Select
the Personal volume.
fuelLevel
and fuelEfficiency. They should both be private and of type
double. Precede each with a comment stating what it stores. You don't need to initialize
instance variables since we do that in the class' constructors. For example,private double fuelLevel; private double fuelEfficiency;fuelEfficiency to that value,
and sets the fuelLevel
to 0. Remember no return type
is needed for constructors.
They return a new object instance of the class./**
* Constructor initializing the car
fuel efficiency
* @param efficiency the car's fuel
efficiency
**/
public Automobile (double
efficiency)
{
fuelEfficiency=efficiency;
//This
car starts with no fuel
fuelLevel=0;
}
/**
* Constructor initializing fuel
efficiency and fuel level to specified values
* @param efficiency the car's fuel
efficiency
*
@param amount the initial amount of fuel
**/
public Automobile (double
efficiency, double amount)
{
fuelEfficiency
= efficiency;
//This
car starts with specified amount of fuel
fuelLevel
= amount;
}
drive
that take one parameter, a double, returns void, and drives the car that
many miles. Assume the car
has enough fuel to drive the specified distance./**
Adds the specified amount of fuel to
the car.
@param distance (in miles) to drive
the car
**/
public void drive (double distance)
{
fuelLevel =
fuelLevel – distance/fuelEfficiency;
}
drive method. Create a new object by control
clicking on Automobile, choosing one of the constructors, and supplying
the needed parameters. Inspect
the fuel level of your object by double clicking it. Execute the drive method by control clicking
the red object, choosing drive,
and entering the distance.
Inspect your car (the red object) again to see that the correct
amount of fuel was used.
addFuel
that takes one parameter, a double, and returns void. It should add the specified amount
of gas to the car. For
example,/**
Adds the specified amount of fuel to
the car.
@param fuelAmount amount of fuel
to add
**/
public void addFuel (double fuelAmount)
{
fuelLevel =
fuelLevel + fuelAmount;
}
addFuel method. Create a new object by control
clicking on Automobile, choosing one of the constructors, and supplying
the needed parameters. Inspect
the fuel level of your object by double clicking it. Execute the addFuel method by control
clicking the red object, choosing addFuel,
and entering the fuel amount.
Inspect your car (the red object) again to see that the fuel added
correctly.
mileage. Modify your constructors to
initialize mileage to
0. If desired add a new
constructor that lets the mileage be specified. Modify drive
to add the distance to the mileage.
Test your changes.
/**
Gets the fuel efficiency of the
car.
@return the fuel efficiency (in
miles/gallon)
**/
public double getFuelEfficiency()
{
return fuelEfficiency;
}
/**
Sets the fuel efficiency of the
car to the given value.
@param the fuel efficiency (in
miles/gallon)
**/
public void setFuelEfficiency(double efficiency)
{
fuelEfficiency
= efficiency;
}
addFuel and drive
to use getFuelLevel and setFuelLevel instead of accessing
the instance variable directly.
Test your modifications.