Create a class called Vehicle that has the manufacturer\'s name (type String), n
ID: 3773964 • Letter: C
Question
Create a class called Vehicle that has the manufacturer's name (type String), number of cylinders in the engine (type int), and owner (type Person given next). Then, create a class called Truck that is derived from Vehicle and has the following 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 class has a reasonable complement of constructors, accessor and mutator methods, and suitably defined equals and toString methods. Write a program to test all your methods. The definition of the class Person follows. Completing the definitions of the methods is part of this programming project. public class Person {public Person() {...} public Person(String theName) public Person(Person theObject) {...} public String getName) public void setName(String theName) {...} public String toString() {...} public boolean equals(Object other) {...}}Explanation / Answer
Hi Please find my implementation.
I have implemented all th classes.
Please let me know in case of any issue.
########## Person.java ################
public class Person {
private String name;
public Person(String theName){
name = theName;
}
public Person(Person thePerson){
name = thePerson.name;
}
public String getName(){
return name;
}
public void setName(String theName){
name = theName;
}
public String toString(){
return "Name = "+name;
}
public boolean equals(Object other){
if(other instanceof Person){
Person p = (Person)other;
return name.equals(p.name);
}
return false;
}
}
############ Vehicle.java ##########
public class Vehicle {
// instance variables
private String manufacturerName;
private int numCylinder;
private Person owner;
public Vehicle(String maString, int n, Person person){
manufacturerName= maString;
numCylinder = n;
owner = new Person(person);
}
public String getManufacturerName() {
return manufacturerName;
}
public void setManufacturerName(String manufacturerName) {
this.manufacturerName = manufacturerName;
}
public int getNumCylinder() {
return numCylinder;
}
public void setNumCylinder(int numCylinder) {
this.numCylinder = numCylinder;
}
public Person getOwner() {
return owner;
}
public void setOwner(Person owner) {
this.owner = owner;
}
@Override
public String toString() {
return "Manufaturer Name: "+manufacturerName+" "+
"Number of Cylinder: "+numCylinder+" "+
"Owner: "+owner.toString();
}
@Override
public boolean equals(Object obj) {
if(obj instanceof Vehicle){
Vehicle v = (Vehicle)obj;
if(manufacturerName.equals(v.manufacturerName) &&
numCylinder == v.numCylinder &&
owner.equals(v.owner))
return true;
}
return false;
}
}
########### Truck.java ###########
public class Truck extends Vehicle{
private double loadCapacity;
private int towingCApacity;
public Truck(String maString, int n, Person person, double load, int tow){
super(maString, n, person);
loadCapacity = load;
towingCApacity = tow;
}
public double getLoadCapacity() {
return loadCapacity;
}
public void setLoadCapacity(double loadCapacity) {
this.loadCapacity = loadCapacity;
}
public int getTowingCApacity() {
return towingCApacity;
}
public void setTowingCApacity(int towingCApacity) {
this.towingCApacity = towingCApacity;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof Truck){
Truck t = (Truck)obj;
if(super.equals(t) && loadCapacity == t.loadCapacity &&
towingCApacity == t.towingCApacity)
return true;
}
return false;
}
@Override
public String toString() {
return super.toString()+" "+
"Load Capacity: "+loadCapacity+" "+
"Towing Cpacity: "+towingCApacity;
}
}
############ Test.java ############
public class Test {
public static void main(String[] args) {
// creating Person Object
Person p = new Person("Pravesh Kumar");
// creating Truck Object
Truck t = new Truck("Tata Motora", 21, p, 45.32, 45);
System.out.println(t.toString());
}
}
/*
Sample run:
Manufaturer Name: Tata Motora
Number of Cylinder: 21
Owner: Name = Pravesh Kumar
Load Capacity: 45.32
Towing Cpacity: 45
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.