I need help with this Java project Create a base class called Vehicle that has t
ID: 3840922 • Letter: I
Question
I need help with this Java project
Create a base class called Vehicle that has the manufacturer's name (String), number of cylinders in the engine (int), horsepower (int) and owner (type Person from previous assignment).
The Vehicle class should have 2 constructors:
Parameterized constructor (manufacturerName, numberCylinder, horsepower, owner)
Copy constructor
Now, create a class Truck that is derived from Vehicle and has additional instance variables: load capacity in tons (double) and towing capacity in tons (also type double).
The Truck class should have 2 constructors:
Parameterized constructor (manufacturerName, numberCylinder, horsepower, owner, loadCapacity, towingCapacity)
Copy constructor
Give your classes a reasonable complement of accessor and mutator methods, an equals( ) method and a toString( ) method. Write a driver (no pun intended) program to test both classes.
Below is an inheritance diagram in UML (Unified Modeling Language) of the three classes:
Vehicle Class E Fields horsepower manufacturerName number Cylinders owner E Methods equals getHorsepower getManufacturerName get owner set Horsepower set ManufacturerName set owner toString ehicle 1 overload) Truck Class vehicle E Fields oad Capacity towing capacity E Methods equals getLoadCapadty getTo ng Capacity set LoadCapadty set Towing Capacity toString Truck 1 overload)Explanation / Answer
class Person
{
private String firstName;
private String lastName;
public Person(String firstName,String lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
public String toString()
{
return firstName+" "+lastName;
}
}
class Vehicle
{
private double horsepower;
private String manufacturerName;
private int numberCylinders;
private Person owner;
public boolean equals(Vehicle v)
{
if(this.horsepower == v.horsepower && this.manufacturerName == v.manufacturerName && this.numberCylinders == v.numberCylinders && this.owner == v.owner)
return true;
else
return false;
}
//set and get methods
public double getHorsepower()
{
return horsepower;
}
public int getNumberCylinder()
{
return numberCylinders;
}
public String getManufacturerName()
{
return manufacturerName;
}
public Person getOwner()
{
return owner;
}
public void setHorsepower(double horsepower)
{
this.horsepower = horsepower;
}
public void setManufacturerName(String manufacturerName)
{
this.manufacturerName = manufacturerName;
}
public void setOwner(Person owner)
{
this.owner = owner;
}
public String toString()
{
return "Horsepower : "+horsepower+" Manufacturer name : "+manufacturerName+" Number of cylinders : "+numberCylinders+ " Owner : "+owner;
}
//parameterized constructor
public Vehicle(String manufacturerName, int numberCylinders, double horsepower, Person owner)
{
this.manufacturerName = manufacturerName;
this.numberCylinders = numberCylinders;
this.horsepower = horsepower;
this.owner = owner;
}
//copy constructor
public Vehicle(Vehicle v)
{
this.manufacturerName = v.manufacturerName;
this.numberCylinders = v.numberCylinders;
this.horsepower = v.horsepower;
this.owner = v.owner;
}
}
class Truck extends Vehicle
{
private double loadCapacity;
private double towingCapacity;
public boolean equals(Truck t)
{
if(this.loadCapacity == t.loadCapacity && this.towingCapacity == t.towingCapacity )
return true;
else
return false;
}
//set and get methods
public double getLoadCapacity()
{
return loadCapacity;
}
public double getTowingCapacity()
{
return towingCapacity;
}
public void setLoadCapacity(double loadCapacity)
{
this.loadCapacity = loadCapacity;
}
public void setTowingCapacity(double towingCapacity)
{
this.towingCapacity = towingCapacity;
}
public String toString()
{
return super.toString() + "Load Capacity : "+loadCapacity+" Towing Capacity : "+towingCapacity;
}
//parameterized constructor
public Truck(String manufacturerName, int numberCylinder, double horsepower, Person owner, double loadCapacity, double towingCapacity)
{
super(manufacturerName,numberCylinder,horsepower,owner);
this.loadCapacity = loadCapacity;
this.towingCapacity = towingCapacity;
}
//copy constructor
public Truck(Truck t)
{
super(t.getManufacturerName(),t.getNumberCylinder(),t.getHorsepower(),t.getOwner());
this.loadCapacity = t.loadCapacity;
this.towingCapacity = t.towingCapacity;
}
}
class Test
{
public static void main (String[] args)
{
Person p = new Person("James","Gosling");
Truck T = new Truck("Ford",6,567.34,p,400.5,345.54);
System.out.println(T);
Truck T1 = new Truck(T);
if(T1.equals(T))
System.out.println(" Both trucks are same ");
else
System.out.println(" Both trucks are not same ");
}
}
Output:
Horsepower : 567.34 Manufacturer name : Ford Number of cylinders : 6 Owner : James GoslingLoad Capacity : 400.5 Towing Capacity : 345.54
Both trucks are same