Suppose we want to maintain a list of car and truck objects. For each object, ei
ID: 3682686 • Letter: S
Question
Suppose we want to maintain a list of car and truck objects. For each object, either car or truck, we record the registration number and owner’s name, address, city, state, and zip code. For car, we also record the number of doors. For truck, we also record the number of axles.
Both car and truck have a description method which describes the functions of car and truck accordingly.
Use OOP concepts, inheritance and abstract method/class, to design classes for this problem.
Based on your design, write Java classes.
Write a client program to test these classes. It doesn’t have to be menu-driven.
The program should contain an array of superclass to hold a variety of car and truck objects (about 10 objects, you can hardcode to create objects and place them into the array in random order).
It loops through the array to output the information (including the description) of these objects polymorphically.
Explanation / Answer
Hi I have implemented Vehicle(Abstract Class), Car class, and Truck Class.
I have also created TestVehicle class, where you can create any number of Car and Truck Object. You can set description andprint it. So please this class according to your need..
// Abstract Vehicle Class
public abstract class Vehicle {
private String registrationNumber;
private String ownerName;
private String address;
private String city;
private String state;
private String zip;
protected String des; // to store description
public Vehicle(String registrationNumber, String ownerName, String address,
String city, String state, String zip) {
this.registrationNumber = registrationNumber;
this.ownerName = ownerName;
this.address = address;
this.city = city;
this.state = state;
this.zip = zip;
}
// function to get description
public String description(){
return des;
}
// function to set description
public void setDescription(String des){
this.des = des;
}
@Override
public String toString() {
return "Reg.No.:"+registrationNumber+" Owner Name: "+ownerName+" Address: "+address+
" City: "+city+" State: "+state+" ZIP: "+zip;
}
}
class Car extends Vehicle{
private int noOfDoors;
public Car(String registrationNumber, String ownerName, String address,
String city, String state, String zip, int noOfDoors) {
super(registrationNumber, ownerName, address, city, state, zip);
this.noOfDoors = noOfDoors;
}
@Override
public String toString() {
return super.toString()+" No. of Doors: "+noOfDoors;
}
}
class Truck extends Vehicle{
int noOfAxles;
public Truck(String registrationNumber, String ownerName, String address,
String city, String state, String zip,int nuOfAxles) {
super(registrationNumber, ownerName, address, city, state, zip);
this.noOfAxles = nuOfAxles;
}
@Override
public String toString() {
return super.toString()+" No. of Doors: "+noOfAxles;
}
}
// This is test class
public class TestVehicle {
public static void main(String[] args) {
Car c1 = new Car("JH12", "Alex", "Landon", "Lords", "Aserd", "1234", 2);
c1.setDescription("THis is C1 car, High Mileage");
Car c2 = new Car("BH34", "Weex", "Neaor", "Marlt", "Aserd", "4321", 1);
c2.setDescription("THis is C2");
Car c3 = new Car("LE098", "PK", "Bangalore", "Kormangla", "Karnatka", "5609", 2);
c3.setDescription("THis is C3");
Car c4 = new Car("KR76", "MK", "Melno", "Texas", "SP", "0231", 3);
c4.setDescription("This is C4 Car");
Truck t1 = new Truck("NH332", "Vikas", "bahuBazar", "Ranchi", "jrknd", "8221", 4);
t1.setDescription("This is T1 Truck");
Truck t2 = new Truck("LKJ88", "Aslk", "Btm", "Patna", "Bihar", "3333", 6);
t2.setDescription("This is T2 Truck");
Truck t3 = new Truck("CF556", "Pintu", "LalCuk", "Ranchi", "Naert", "4321", 5);
t3.setDescription("This is T2 Truck");
// creating Vehicle Array
Vehicle [] vehicles = {t1, c1, c4, t3, c2, t2, c3};
for(Vehicle vehicle: vehicles){
System.out.println(vehicle.toString());
System.out.println(vehicle.description());
System.out.println();
}
}
}
/*
Sample run:
Reg.No.:NH332
Owner Name: Vikas
Address: bahuBazar
City: Ranchi
State: jrknd
ZIP: 8221
No. of Doors: 4
This is T1 Truck
Reg.No.:JH12
Owner Name: Alex
Address: Landon
City: Lords
State: Aserd
ZIP: 1234
No. of Doors: 2
THis is C1 car, High Mileage
Reg.No.:KR76
Owner Name: MK
Address: Melno
City: Texas
State: SP
ZIP: 0231
No. of Doors: 3
This is C4 Car
Reg.No.:CF556
Owner Name: Pintu
Address: LalCuk
City: Ranchi
State: Naert
ZIP: 4321
No. of Doors: 5
This is T2 Truck
Reg.No.:BH34
Owner Name: Weex
Address: Neaor
City: Marlt
State: Aserd
ZIP: 4321
No. of Doors: 1
THis is C2
Reg.No.:LKJ88
Owner Name: Aslk
Address: Btm
City: Patna
State: Bihar
ZIP: 3333
No. of Doors: 6
This is T2 Truck
Reg.No.:LE098
Owner Name: PK
Address: Bangalore
City: Kormangla
State: Karnatka
ZIP: 5609
No. of Doors: 2
THis is C3
*/