Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help create this program Make a class called Vehicle which behaves as a s

ID: 3864973 • Letter: P

Question

Please help create this program Make a class called Vehicle which behaves as a superclass for vehicle types. The Vehicle class must have private variables for • the number of wheels and • the average number of miles per gallon (MPG). For variable – create the proper get and set methods. The Vehicle class should 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 must conatin 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

/*COMMENTS
* for the convinience of posting the answer i have made it in a single file structure
* you are free to add packages and seperate the classes to new files like Vehicle.java but do the necessary imports
* recommendation use eclipse ide.
* ALL THE BEST ,change messages according to your convinience
*/

// program starts from here


import javax.swing.JOptionPane;


public class UseVehicles {

   public static void main(String[] args) {

       //Integer.parseInt method is used as JOptionPane.showInputDialog returns string
       //but we require integer

       // for vehicle class
       Vehicle vehicle = new Vehicle();
       int numWheels;
      
       numWheels = Integer.parseInt(JOptionPane.showInputDialog(null,
               "for vehicle class enter number of wheels"));
       vehicle.setNumWheels(numWheels);

       int mpg;
       mpg = Integer.parseInt(JOptionPane.showInputDialog(null,
               "for vehicle class enter average number of miles per gallon"));
       vehicle.setMpg(mpg);

       JOptionPane.showMessageDialog(null,"number of wheels :"+ vehicle.getNumWheels()+
               " average number of miles per gallon :"+ vehicle.getMpg());


       //passenger car class
       PassengerCar pCar = new PassengerCar();

       int numWheelsCar;
      
      
       numWheelsCar = Integer.parseInt(JOptionPane.showInputDialog(null,
               "for PassengerCar class enter number of wheels"));
       pCar.setNumWheels(numWheelsCar);

       int mpgCar;
       mpgCar = Integer.parseInt(JOptionPane.showInputDialog(null,
               "for PassengerCar class enter average number of miles per gallon"));
       pCar.setMpg(mpgCar);

       int fuelTankCapacityCar =Integer.parseInt(JOptionPane.showInputDialog(null,
               "for PassengerCar class enter fuel tank capacity"));
       pCar.setFuelTankCapacity(fuelTankCapacityCar);

       JOptionPane.showMessageDialog(null,"number of wheels :"+ pCar.getNumWheels()+
               " average number of miles per gallon :"+ pCar.getMpg()
               +" fuel tank capacity: "+pCar.getFuelTankCapacity()
               +" maximum distance driven: "+pCar.MaximumDistanceDriven(pCar.getMpg(), pCar.getFuelTankCapacity()));


       //for Truck class
       Truck truck = new Truck();
       int numWheelsTruck;
      
      
       numWheelsTruck = Integer.parseInt(JOptionPane.showInputDialog(null,
               "for Truck class enter number of wheels"));
       truck.setNumWheels(numWheelsTruck);

       int mpgTruck;
       mpgTruck = Integer.parseInt(JOptionPane.showInputDialog(null,
               "for Truck class enter average number of miles per gallon"));
       truck.setMpg(mpgTruck);

       int cargoCapacityTruck =Integer.parseInt(JOptionPane.showInputDialog(null,
               "for Truck class enter fuel tank capacity"));
       truck.setCargoLoad(cargoCapacityTruck);

       JOptionPane.showMessageDialog(null,"number of wheels :"+ truck.getNumWheels()+
               " average number of miles per gallon :"+ truck.getMpg()
               +" cargo capacity: "+truck.getCargoLoad());

   }

}


class Vehicle {
  
   private int numWheels; // number of wheels
   private int mpg; // average miles per gallon
  
  
   public int getNumWheels() {
       return numWheels;
   }
   public void setNumWheels(int numWheels) {
       this.numWheels = numWheels;
   }
   public int getMpg() {
       return mpg;
   }
   public void setMpg(int mpg) {
       this.mpg = mpg;
   }
  
   public Vehicle(int numWheels,int mpg)
   {
       this.numWheels = numWheels;
       this.mpg = mpg;
      
   }
   public Vehicle() {
      
   }
  
  
  
}


class Truck extends Vehicle {

   private int cargoLoad;
  
  
  
   public int getCargoLoad() {
       return cargoLoad;
   }

   public void setCargoLoad(int cargoLoad) {
       this.cargoLoad = cargoLoad;
   }

  
  
   public Truck(int numWheels,int cargoLoad) {
       super(numWheels, cargoLoad);
      
   }
  
   @Override
   public void setNumWheels(int numWheels)
   {
       super.setNumWheels(8); // overriding so that no one can alter the numWheels for Truck
   }

   public Truck() {
       super.setNumWheels(8); // assigning 8 as default for numWheels of Truck
   }
  
  
  
}
class PassengerCar extends Vehicle{

   private int fuelTankCapacity;
  

   public int getFuelTankCapacity() {
       return fuelTankCapacity;
   }

   public void setFuelTankCapacity(int fuelTankCapacity) {
       this.fuelTankCapacity = fuelTankCapacity;
   }

  
  
   PassengerCar(int numWheels,int fuelTankCapacity) {
       super(numWheels, fuelTankCapacity);
      
   }
  
   @Override
   public void setNumWheels(int numWheels)
   {
       super.setNumWheels(4); // overriding so that no one can alter the numWheels for Car
   }
      
  
   public PassengerCar() {
       super.setNumWheels(4); // assigning 4 as default for numWheels of car
   }

   public double MaximumDistanceDriven(int mpg,int fuelTankCapacity)
   {
      
       return mpg*fuelTankCapacity;
   }
  
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote