Modify Vehicle.java so that it includes an instance variable called milesPerGal. Both Bus and Truck have a method called calculateMPG. The formula for a bus's milesPerGal is 10000/numOfPassenger/horsePower, but for a truck it is 5000/towingCapacity/horsePower. Create a program called VehicleApp.java that displays all information about a specific bus and truck of your choice.
_______________________
Vehicle.java:
public class Vehicle {
public String make;
public String year;
public int horsepower;
public Vehicle() {
make="Default";
year="1111";
horsepower=0;
}
public int getHorsepower() {
return horsepower;
}
public String getMake() {
return make;
}
public String getYear() {
return year;
}
public void setHorsepower(int hp) {
horsepower = hp;
}
public void setMake(String m) {
make = m;
}
public void setYear(String y) {
year = y;
}
public void Display()
{
System.out.println("Make: "+getMake());
System.out.println("Year: "+getYear());
System.out.println("HorsePower: "+getHorsepower());
}
public static void main(String[] args) {
Vehicle a = new Vehicle();
Vehicle b = new Vehicle();
Vehicle c = new Vehicle();
a.setMake("Ford");
a.setYear("1999");
a.setHorsepower(200);
b.setMake("GM");
b.setYear("1998");
b.setHorsepower(300);
c.setMake("Honda");
c.setYear("2004");
c.setHorsepower(400);
System.out.println("Car 1:");
a.Display();
System.out.println("nCar 2:");
b.Display();
System.out.println("nCar 3:");
c.Display();
}
}
__________________________
Bus.java:
public class Bus extends Vehicle{
public int numOfPassengers;
public Bus() {
this.numOfPassengers = 0;
}
public int getNumOfPassengers() {
return numOfPassengers;
}
public void setNumOfPassengers(int numOfPassengers) {
this.numOfPassengers = numOfPassengers;
}
@Override
public void Display()
{
System.out.println("Make: "+getMake());
System.out.println("Year: "+getYear());
System.out.println("HorsePower: "+getHorsepower());
System.out.println("Number of Passengers: "+getNumOfPassengers());
}
public static void main(String[] args) {
Bus a = new Bus();
Bus b = new Bus();
Bus c = new Bus();
a.setMake("Toyota");
a.setYear("1999");
a.setHorsepower(200);
a.setNumOfPassengers(20);
b.setMake("Honda");
b.setYear("1998");
b.setHorsepower(300);
b.setNumOfPassengers(30);
c.setMake("GM");
c.setYear("2007");
c.setHorsepower(500);
c.setNumOfPassengers(40);
System.out.println("Bus 1:");
a.Display();
System.out.println("nBus 2:");
b.Display();
System.out.println("nBus 3:");
c.Display();
}
}
_______________________
Truck.java:
public class Truck extends Vehicle{
public double towingCapacity;
public double getTowingCapacity() {
return towingCapacity;
}
public void setTowingCapacity(double towingCapacity) {
this.towingCapacity = towingCapacity;
}
@Override
public void Display()
{
System.out.println("Make: "+getMake());
System.out.println("Year: "+getYear());
System.out.println("HorsePower: "+getHorsepower());
System.out.println("Towing Capacity: "+getTowingCapacity());
}
public static void main(String[] args) {
Truck a = new Truck();
Truck b = new Truck();
Truck c = new Truck();
a.setMake("Honda");
a.setYear("1999");
a.setHorsepower(200);
a.setTowingCapacity(2.0);
b.setMake("GM");
b.setYear("1998");
b.setHorsepower(300);
b.setTowingCapacity(1.0);
c.setMake("Ford");
c.setYear("2007");
c.setHorsepower(500);
c.setTowingCapacity(1.5);
System.out.println("Truck 1:");
a.Display();
System.out.println("nTruck 2:");
b.Display();
System.out.println("nTruck 3:");
c.Display();
}
}