Write small program that has four classes, Vehicle, Car, Truck, motorcycle. Vehi
ID: 3776764 • Letter: W
Question
Write small program that has four classes, Vehicle, Car, Truck, motorcycle. Vehicle hold the Vin number (String) Weight. The remaining classes extend Vehicle. Car holds number of doors, number of passengers, Truck holds cargo weight, and motorcycle holds wheel size. In the main use polymorphism to fill an array of vehicles with each of the noted vehicles then display the Vin numbers of each. Hard code the data input and use a simple loop with System.out.println for your output. Has to be in Java Language
Explanation / Answer
/**
Program with four classes Vehicle, Car, Truck and Motorcycle.
Using polymorphism to print the properties.
*/
class Vehicle {
public void vinNumber(String vinNumber){
System.out.println("vehicle class, vin number is: " +vinNumber);
}
}
class Car extends Vehicle{
public void vinNumber(String vinNumber,int door,int passenger){
System.out.println("vehicle class, vin number is: " +vinNumber);
System.out.println("Car class , doors: " +door);
System.out.println("Car class, passengers: " +passenger);
}
}
class Truck extends Vehicle{
public void vinNumber(String vinNumber,int cargoWeight){
System.out.println("Truck class, vin number is: " +vinNumber);
System.out.println("Truck class , cargo weight: " +cargoWeight);
}
}
class Motorcycle extends Vehicle{
public void vinNumber(String vinNumber,int wheelSize){
System.out.println("Motorcycle class, vin number is: " +vinNumber);
System.out.println("Motorcycle class , wheel size: " +wheelSize);
}
}
public class VehicleHolder{
public static void main(String arg[]) {
Vehicle v = new Vehicle();
Car c = new Car();
Truck t = new Truck();
Motorcycle m = new Motorcycle();
v.vinNumber("MH-12345");
c.vinNumber("DEL-2345",4,4);
t.vinNumber("DEL-2345",58);
m.vinNumber("DEL-2345",6);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.