Create a base class called Vehicle that has the manufacturer\'s name (type strin
ID: 3834603 • Letter: C
Question
Create a base class called Vehicle that has the manufacturer's name (type string), number of cylinders in the engine (type int), and owner (type Person given below). Then create a class called Truck that is derived from Vehicle and has additional properties, the load capacity in tons (type double since it may contain a fractional part) and towing capacity in pounds (type int). Be sure your classes have a reasonable complement of constructors and accessor methods and a copy constructor. Write a driver program that tests all your methods. The definition of the class Person follows. Implement it.
class Person
{
public:
Person();
Person(string theName);
Person(const Person& theObject);
string getName() const;
private:
string name;
};
Explanation / Answer
//person class
public class Person {
String name;
public Person(){
name=null;
}
public Person(String nm){
name=nm;
}
public Person(Person per){
name=per.name;
}
}
//Vehicle class
public class Vehicle {
String manuf_name;
int no_of_cyl;
Person p;
public Vehicle(){
manuf_name=null;
no_of_cyl=0;
p=new Person();
}
public Vehicle(String mf_nm,int no_cyl,String per_name){
manuf_name=mf_nm;
no_of_cyl=no_cyl;
p=new Person(per_name);
}
public Vehicle(String mf_nm,int no_cyl,Person pers){
manuf_name=mf_nm;
no_of_cyl=no_cyl;
p=new Person(pers);
}
public void setManufName(String mn){
manuf_name=mn;
}
public String getManufName(){
return manuf_name;
}
public void setNoOfCyl(int cyl){
no_of_cyl=cyl;
}
public int getNoOfCyl(){
return no_of_cyl;
}
public void setPersonName(String nme){
p.name=nme;
}
public String getPersonName(){
return p.name;
}
}
// Truck class
public class Truck extends Vehicle{
double load_capacity;
int towing_capacity;
public Truck(){
super();
load_capacity=0.0;
towing_capacity=0;
}
public Truck(String mn,int cyl,String pnm,double ld,int tw){
super(mn,cyl,pnm);
load_capacity=ld;
towing_capacity=tw;
}
public Truck(String mn,int cyl,Person per_nm,double ld,int tw){
super(mn,cyl,per_nm);
load_capacity=ld;
towing_capacity=tw;
}
public void setLoadCapacity(double lc){
load_capacity=lc;
}
public double getLoadCapacity(){
return load_capacity;
}
public void setTowingCapacity(int tc){
towing_capacity=tc;
}
public int getTowingCapacity(){
return towing_capacity;
}
public static void main(String args[]){
Truck tk=new Truck();
System.out.println("Vehicle detail after default constructor call****************");
System.out.println("Manufacturer name is : "+tk.getManufName());
System.out.println("No of cylinders is/are : "+tk.getNoOfCyl());
System.out.println("Owner is : "+tk.getPersonName());
System.out.println("Load capacity is : "+tk.getLoadCapacity());
System.out.println("Towing capacity is : "+tk.getTowingCapacity());
Truck tk1=new Truck("Tata",4,"Vinod Kumar Singh",120000.50,5000);
System.out.println(" Vehicle detail after parameterized constructor call****************");
System.out.println("Manufacturer name is : "+tk1.getManufName());
System.out.println("No of cylinders is/are : "+tk1.getNoOfCyl());
System.out.println("Owner is : "+tk1.getPersonName());
System.out.println("Load capacity is : "+tk1.getLoadCapacity());
System.out.println("Towing capacity is : "+tk1.getTowingCapacity());
Person per=new Person("Sachin Singh");
Truck tk2=new Truck("Ashok Leyland",8,per,150000.00,8000);
System.out.println(" Vehicle detail after copy constructor call****************");
System.out.println("Manufacturer name is : "+tk2.getManufName());
System.out.println("No of cylinders is/are : "+tk2.getNoOfCyl());
System.out.println("Owner is : "+tk2.getPersonName());
System.out.println("Load capacity is : "+tk2.getLoadCapacity());
System.out.println("Towing capacity is : "+tk2.getTowingCapacity());
tk2.setManufName("Marcopolo");
tk2.setNoOfCyl(12);
tk2.setPersonName("Sunil Singh");
tk2.setLoadCapacity(200000.50);
tk2.setTowingCapacity(10000);
System.out.println(" Vehicle detail after setting values****************");
System.out.println("Manufacturer name is : "+tk2.getManufName());
System.out.println("No of cylinders is/are : "+tk2.getNoOfCyl());
System.out.println("Owner is : "+tk2.getPersonName());
System.out.println("Load capacity is : "+tk2.getLoadCapacity());
System.out.println("Towing capacity is : "+tk2.getTowingCapacity());
}
}