public class Vehicle {
// the Vehicle class has two fields
static int numberOfVehicles = 0; // static field, class variable
private String make;
private int milesPerGallon;
private int milesTravelled;
private final int GALLONS_PER_TANK = 16;
// default constructor
public Vehicle(){
numberOfVehicles++;
}
// Example of Overloading - constructor with the same name but with a
// different signature (different parameters)
public Vehicle(int mpg, String name)
{
make = name;
milesPerGallon = mpg;
numberOfVehicles++;
}
// public Vehicle(String carMake, int mpg)
// {
// make = carMake;
// milesPerGallon = mpg;
// }
public Vehicle(String make, int milesPerGallon)
{
this.make = make;
this.milesPerGallon = milesPerGallon;
}
// method for calculating total distance the car can travel on one tank
public int getMiles()
{
milesTravelled = GALLONS_PER_TANK * milesPerGallon;
return milesTravelled;
}
//mutator/setter method
public void setMake(String m){
make = m;
}
//mutator/setter method
public void setMilesPerGallon(int mpg){
milesPerGallon = mpg;
}
//accessor/getter method
public String getMake(){
return make;
}
//accessor/getter method
public int getMilesPerGallon(){
return milesPerGallon;
}
public static int getNumberOfVehicles( ) // static method - can be called
// even when no instances of the Vehicle class exists.
{
return numberOfVehicles;
}
// provide easily readable output for this object.
public String toString()
{
String output = "";
output = "Car Make: " + getMake() +
"\n" + "mpg: " + getMilesPerGallon();
return output;
}
} // end of class