Create a class named Vehicle that acts as a super class for vehicle types. The V
ID: 3660881 • Letter: C
Question
Create a class named Vehicle that acts as a super class for vehicle types. The Vehicle class contains private variables for the number of wheels and the average number of miles per gallon. The Vehicle class also has a constructor with integer arguments for the number of wheels and average miles per gallon and a toString() method that returns a String containing these values. Create two subclasses, Car and MotorCycle, that extend the Vehicle class. Each subclass contains a constructor that accepts the mile-per-gallon value as an argument and forces the number of wheels to the appropriate valueExplanation / Answer
//Vehicle.java public class Vehicle { private int NumWheels; private double AvgMilesPerGallon; public Vehicle(int NumWheels, double AvgMilesPerGallon) { this.NumWheels = NumWheels; this.AvgMilesPerGallon = AvgMilesPerGallon; } @Override public String toString() { return "Number of Wheels = " + NumWheels+" Average Miles per Gallon = "+AvgMilesPerGallon; } } //Car.Java public class Car extends Vehicle { Car(double dMilesPerGallon) { super(4, dMilesPerGallon); } } public class Car extends Vehicle { Car(double dMilesPerGallon) { super(4, dMilesPerGallon); } } //MotorCycle.java public class MotorCycle extends Vehicle { MotorCycle(double dMilesPerGallon) { super(2, dMilesPerGallon); } } public class MotorCycle extends Vehicle { MotorCycle(double dMilesPerGallon) { super(2, dMilesPerGallon); } } //UseVehicle.java public class UseVehicle { public static void main(String[] args) { Vehicle V = new Vehicle(6, 12.0); System.out.println(" Vehicle :: " + V); Vehicle C = new Car(20); System.out.println(" Car :: " + C); Vehicle M = new MotorCycle(50); System.out.println(" Motor Cycle :: " + M); } } public class UseVehicle { public static void main(String[] args) { Vehicle V = new Vehicle(6, 12.0); System.out.println(" Vehicle :: " + V); Vehicle C = new Car(20); System.out.println(" Car :: " + C); Vehicle M = new MotorCycle(50); System.out.println(" Motor Cycle :: " + M); } }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.