Part 3 Add a method named Display to each of the four classes to print out the i
ID: 3770780 • Letter: P
Question
Part 3 Add a method named Display to each of the four classes to print out the individual vehicle information. For the Vehicle class, the Display method should print the following (one element per line):
Make: vehicle_make
Year: vehicle_year
Model: vehicle_model
Miles: vehicle_mileage
Price: vechicle_price
Each vehicle-type class (Car, Truck, SUV) should print out the information specific to its own type in addition to the vehicle information (Hint: use the superclass' Display method). Here is an example of the output for a Car:
Inventory unit: Car
Make: Audi
Year: 2009
Model: A8
Miles: 40000
Price: 27300.00
Number of doors: 4
Explanation / Answer
package assignment;
public class VehicleDisplayTest {
public static void main(String[] args) {
Car car = new Car("Audi",2009,"A8", 4000, 273.00,4);
Truck truck = new Truck("Tata",2001,"BMX", 10000, 1073.00,6);
SUV suv = new SUV("Jaguar",2005,"FLX", 8000, 2739.00,150);
car.display();
truck.display();
suv.display();
}
}
class SUV extends Vehicle {
//This is example propery, change as per your need
private double speed;
public SUV(String make, int year, String model, double miles, double price,double speed) {
super(make, year, model, miles, price);
// TODO Auto-generated constructor stub
this.speed = speed;
}
public void display() {
System.out.println("Inventory unit: SUV");
super.display();
System.out.println("Speed(Kmph): "+speed);
}
public double getSpeed() {
return speed;
}
public void setSpeed(double speed) {
this.speed = speed;
}
}
class Truck extends Vehicle {
//Add specific fileds here
private int noOfWheels;
public Truck(String make, int year, String model, double miles, double price, int noOfWheels) {
super(make, year, model, miles, price);
// TODO Auto-generated constructor stub
this.noOfWheels = noOfWheels;
}
public void display() {
System.out.println("Inventory unit: Truck");
super.display();
System.out.println("Number of wheels: "+noOfWheels);
}
public int getNoOfWheels() {
return noOfWheels;
}
public void setNoOfWheels(int noOfWheels) {
this.noOfWheels = noOfWheels;
}
}
class Car extends Vehicle {
private int noOfDoors;
public Car(String make, int year, String model, double miles, double price,int noOfDoors) {
super(make, year, model, miles, price);
// TODO Auto-generated constructor stub
this.noOfDoors = noOfDoors;
}
public void display() {
System.out.println("Inventory unit: Car");
super.display();
System.out.println("Number of Doors: "+noOfDoors);
}
public int getNoOfDoors() {
return noOfDoors;
}
public void setNoOfDoors(int noOfDoors) {
this.noOfDoors = noOfDoors;
}
}
class Vehicle {
private String make;
private int year;
private String model;
private double miles;
private double price;
public Vehicle(String make, int year,String model, double miles,double price) {
this.make = make;
this.year = year;
this.model = model;
this.miles = miles;
this.price = price;
}
public void display() {
System.out.println("Make: "+ make
+"Year: " +year+
"Model: " +model+
" Miles: " +miles+
" Price: "+price);
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public double getMiles() {
return miles;
}
public void setMiles(double miles) {
this.miles = miles;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
--output---
Inventory unit: Car
Make: AudiYear: 2009Model: A8 Miles: 4000.0 Price: 273.0
Number of Doors: 4
Inventory unit: Truck
Make: TataYear: 2001Model: BMX Miles: 10000.0 Price: 1073.0
Number of wheels: 6
Inventory unit: SUV
Make: JaguarYear: 2005Model: FLX Miles: 8000.0 Price: 2739.0
Speed(Kmph): 150.0
----output----