Create a class named Vehicle that acts as a superclass for vehicle types. The Ve
ID: 3862801 • Letter: C
Question
Create a class named Vehicle that acts as a superclass for vehicle types. The Vehicle class contains private variables for the number of wheels and the average number of miles per gallon (MPG). For each of these variables – create the proper get and set methods. The Vehicle class also contains a constructor with integer parameters for the number of wheels and average MPG. Create two subclasses, PassengerCar, and Truck that extend the Vehicle class. The PassengerCar class will have an additional field fuelTankCapacity and the method MaximumDistanceDriven, which calculate the maximum ride distance using the proper MPG and fuel tank capacity values. The Truck class has the additional cargoLoad field. Each subclass contains a constructor that accepts the MPG value and forces the number of wheels to the appropriate values – 4 for a PassengerCar and 8 for a Truck. The PassengerCar constructor also accepts fuel tank capacity value, and the Truck constructor accepts the cargo load value. All classes contain appropriate get and set methods for each newly added field.
Write a UseVehicles class to instantiate three objects making use of proper overloaded class constructors: one object of the Vehicle class, one object of the PassengerCar class and one object of the Truck class. When doing so, accept the user input and provide proper values for each the fields of each object (use Scanner of JOptionPane classes to implement a user’s input).
Display the content of every data field in the newly created object: the number of wheels and average number of miles per gallon values for the Vehicle object; the number of wheels, miles per gallon, fuel tank capacity and maximum distance driven for a passenger car; number of wheels, average number of miles per gallon and cargo capacity for a Truck object.
Explanation / Answer
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Sam
*/
public class UseVehicle {
public static void main(String[] args) throws IOException {
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter wheels and mpg for vehicle: ");
Vehicle1 v = new Vehicle1(Integer.parseInt(br.readLine()), Integer.parseInt(br.readLine()));
System.out.println("Enter fuel tank capacity and amg for passenger car: ");
PassengerCar p = new PassengerCar(Integer.parseInt(br.readLine()), Integer.parseInt(br.readLine()));
System.out.println("Enter cargo capsity and mgp for truck: ");
Truck t = new Truck(Integer.parseInt(br.readLine()), Integer.parseInt(br.readLine()));
System.out.println(v.toString());
System.out.println(p.toString());
System.out.println(t.toString());
}
}
class PassengerCar extends Vehicle1 {
int fuelTankCapacity;
int maximumRideDistance;
@Override
public String toString() {
return "PassengerCar{ "+super.toString() + "fuelTankCapacity=" + fuelTankCapacity + ", maximumRideDistance=" + maximumRideDistance + '}';
}
public PassengerCar(int fuelTankCapacity, int mpg) {
super(4, mpg);
this.fuelTankCapacity = fuelTankCapacity;
maximumRideDistance = mpg * fuelTankCapacity;
}
public int getFuelTankCapacity() {
return fuelTankCapacity;
}
public int getMaximumRideDistance() {
return maximumRideDistance;
}
public void setFuelTankCapacity(int fuelTankCapacity) {
this.fuelTankCapacity = fuelTankCapacity;
maximumRideDistance = super.getMpg() * fuelTankCapacity;
}
}
class Truck extends Vehicle1 {
int cargoLoad;
@Override
public String toString() {
return "Truck{"+ super.toString() + "cargoLoad=" + cargoLoad + '}';
}
public Truck(int cargoLoad, int mpg) {
super(8, mpg);
this.cargoLoad = cargoLoad;
}
public int getCargoLoad() {
return cargoLoad;
}
public void setCargoLoad(int cargoLoad) {
this.cargoLoad = cargoLoad;
}
}
class Vehicle1{
private int numberOfWheels;
private int mpg;
@Override
public String toString() {
return "Vehicle1{" + "numberOfWheels=" + numberOfWheels + ", mpg=" + mpg + '}';
}
public Vehicle1(int numberOfWheels, int mpg) {
this.numberOfWheels = numberOfWheels;
this.mpg = mpg;
}
public int getNumberOfWheels() {
return numberOfWheels;
}
public void setNumberOfWheels(int numberOfWheels) {
this.numberOfWheels = numberOfWheels;
}
public int getMpg() {
return mpg;
}
public void setMpg(int mpg) {
this.mpg = mpg;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.