Question: Let's change the DisplayVehicle() method to be named Display(). Let's give the Display() method the ability to have a different meaning (behavior) depending on what

Let's change the DisplayVehicle() method to be named Display(). Let's give the Display() method the ability to have a different meaning (behavior) depending on what any derived class of Vehicle wishes to give it.Give the inherited Display() method in Truck and Motorcycle their own meanings. Rewrite VehicleTest.java to use the polymorphic Display() method instead of the: VehicleDisplay(), MotorcycleDisplay() and TruckDisplay() methods

Here is my code from previous code vechicledemo(testcode)

//VehicleDemo.java (Driver class)

class VehicleDemo

{

public static void main(String[] args)

{

//Using constructor with attributes for creating Truck

Truck truck1 = new Truck("Ford","F150",2010,"2WD","regular","Short");

//using default constructor

Truck truck2 = new Truck();

//using setters to set variables

truck2.setMake("Chevrolet");

truck2.setModel("Colorado");

truck2.setYear(2018);

truck2.setCab("Crew Cab");

truck2.setDrive("FW/AWD");

truck2.setBed("Long");

//Motorcycle object

Motorcycle motorcycle1 = new Motorcycle("Honda", "CRB150", 2015, "dual sport", 2);

Motorcycle motorcycle2 = new Motorcycle("Yamaha","YBR150",2001,"street/off-road",2);

motorcycle2.setYear(2010);

//using display method printing the info

truck1.DisplayTruck();

truck2.DisplayTruck();

motorcycle1.DisplayMotorcycle();

motorcycle2.DisplayMotorcycle();

}

}

//OUTPUT

motorcycle code

//Motorcycle.java

public class Motorcycle extends Vehicle{

//Motorcycle have its own attributes

private String classification;

private int no_of_wheels;

public Motorcycle(){}//Default constructor

//Attributes constructor

public Motorcycle(String make, String model, int year,String classification,int wheels) {

super(make, model, year);

this.classification=classification;

this.no_of_wheels=wheels;

}

//setters and getters

public String getClassification() {

return classification;

}

public void setClassification(String classification) {

this.classification = classification;

}

public int getNo_of_wheels() {

return no_of_wheels;

}

public void setNo_of_wheels(int no_of_wheels) {

this.no_of_wheels = no_of_wheels;

}

//display for motorcycle info

public void DisplayMotorcycle(){

super.displayVehicle();

System.out.println("Classification : "+classification+" No Of Wheels : "+no_of_wheels+" ");

}

}

truck code

//Truck.java

public class Truck extends Vehicle

{

//Truck have 2 additional attributes

private String drive,cab,bed;

public Truck(){}//Default constructor

//Attribute constructor

Truck(String make, String model, int year, String drive, String cab, String bed)

{

super(make, model, year);

this.drive=drive;

this.setCab(cab);

this.setBed(bed);

}

//setters and getters

public String getDrive() {

return drive;

}

public void setDrive(String drive) {

this.drive = drive;

}

public String getCab() {

return cab;

}

public void setCab(String cab) {

this.cab = cab;

}

public String getBed() {

return bed;

}

public void setBed(String bed) {

this.bed = bed;

}

//Display truck method

void DisplayTruck()

{

super.displayVehicle();

System.out.println("Drive : " +drive+" Cab : "+cab+" Bed : "+bed);

}

}

vechicle code

//Vehicle.java

class Vehicle

{

private String make;

private String model;

private int year;

//Default constructor

public Vehicle(){

}

//Constructor with attributes

Vehicle(String make, String model, int year)

{

this.make = make;

this.model = model;

this.year = year;

}

//setters and getters

public String getMake() {

return make;

}

public void setMake(String make) {

this.make = make;

}

public String getModel() {

return model;

}

public void setModel(String model) {

this.model = model;

}

public int getYear() {

return year;

}

public void setYear(int year) {

this.year = year;

}

//display method

void displayVehicle()

{

System.out.println("Make: " + make + " Model: " + model + " Year: " +year);

}

}

I'm having trouble starting out with this problem with polymorhism display()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!