// A simple program that uses overloaded constructors to work out the distance a vehicle can go.
class Cons {
//initialise variables.
double mpg, fuel_cap, boot_space, fuel_result, distance;
String vehicle_name;
// create first constructor without bootspace
Cons(double mpg, double fuel_cap, String vehicle_name) {
this.distance = distance;
this.mpg = mpg;
this.fuel_cap = fuel_cap;
this.vehicle_name = vehicle_name;
distance = this.fuel_cap * this.mpg;
}
// creat second construtor with boot space
Cons(double mpg, double fuel_cap, double boot_space, String vehicle_name) {
this.distance = distance;
this.mpg = mpg;
this.fuel_cap = fuel_cap;
this.boot_space = boot_space;
this.vehicle_name = vehicle_name;
distance = this.fuel_cap * this.mpg;
}
}
class MainTest2 {
public static void main(String args[]) {
// create objects, note the 3rd object specifies boot space.
Cons mpgcalc1 = new Cons(30, 60, "Avensis");
Cons mpgcalc2 = new Cons(23, 23, "Rav 4");
Cons mpgcalc3 = new Cons(12, 54, 40, "Bat Mobile");
// Print all the values of the objects.
System.out.println("The " + mpgcalc1.vehicle_name + " has a fuel capacity of "
+ mpgcalc1.fuel_cap + "gals");
System.out.println("On a full tank it can go " + mpgcalc1.distance + " miles");
System.out.println();
System.out.println("The " + mpgcalc2.vehicle_name + " has a fuel capacity of "
+ mpgcalc2.fuel_cap + "gals");
System.out.println("On a full tank it can go " + mpgcalc2.distance + " miles");
System.out.println();
System.out.println("The " + mpgcalc3.vehicle_name + " has a fuel capacity of "
+ mpgcalc3.fuel_cap + "gals");
System.out.println("on a full tank it can go " + mpgcalc3.distance + " miles");
System.out.println("It also has a boot space of " + mpgcalc3.boot_space);
System.out.println();
}
}