Create a class called vehicle that has the manufacturer\'s name (type string), n
ID: 3837708 • 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 the Object) {...} public string getName() {...} public void setName (String theName) {...} public String tostring() {...} public boolean equals object other) {...} }Explanation / Answer
Person.java
public class Person {
//Declaring instance variables
private String name;
//Zero argumented constructor
public Person() {
super();
this.name="";
}
//Parameterized constructor
public Person(String name) {
super();
this.name = name;
}
//Copy Constructor
public Person(Person theObject) {
super();
this.name = theObject.name;
}
//getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//This method compares two Person Objects
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Person [name=" + name + "]";
}
}
______________________
Vehicle.java
public class Vehicle {
//Declaring instance variables
private String manufactures_name;
private int no_of_cylinders;
private Person owner;
//Parameterized constructor
public Vehicle(String manufactures_name, int no_of_cylinders, Person owner) {
super();
this.manufactures_name = manufactures_name;
this.no_of_cylinders = no_of_cylinders;
this.owner = owner;
}
//getters and setters
public String getManufactures_name() {
return manufactures_name;
}
public void setManufactures_name(String manufactures_name) {
this.manufactures_name = manufactures_name;
}
public int getNo_of_cylinders() {
return no_of_cylinders;
}
public void setNo_of_cylinders(int no_of_cylinders) {
this.no_of_cylinders = no_of_cylinders;
}
public Person getOwner() {
return owner;
}
public void setOwner(Person owner) {
this.owner = owner;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return " Manufactures Name=" + manufactures_name
+ ", No Of Cylinders=" + no_of_cylinders + ", owner=" + owner;
}
}
______________________
Truck.java
public class Truck extends Vehicle {
//Declaring instance variables
private double load_capacity;
private int towing_capacity;
//Parameterized constructor
public Truck(String manufactures_name, int no_of_cylinders, Person owner,
double load_capacity, int towing_capacity) {
super(manufactures_name, no_of_cylinders, owner);
this.load_capacity = load_capacity;
this.towing_capacity = towing_capacity;
}
//getters and setters
public double getLoad_capacity() {
return load_capacity;
}
public void setLoad_capacity(double load_capacity) {
this.load_capacity = load_capacity;
}
public int getTowing_capacity() {
return towing_capacity;
}
public void setTowing_capacity(int towing_capacity) {
this.towing_capacity = towing_capacity;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return super.toString()+" load_capacity=" + load_capacity + ", towing_capacity="
+ towing_capacity;
}
}
___________________
Test.java
public class Test {
public static void main(String[] args) {
//Creating Person Class Object P1
Person p1=new Person("Kane Williams");
//Creating Person Class Object P2
Person p2=new Person(p1);
//Comparing Person P1 and person P2 class objects
boolean bool=p1.equals(p2);
if(bool)
System.out.println("The Persons P1 and P2 are Equal");
else
System.out.println("The Persons P1 and P2 are not Equal");
//Creating Person Class Object P3
Person p3=new Person();
p3.setName("Johnson");
//Comparing Person P1 and person P3 class objects
boolean bool1=p2.equals(p3);
if(bool)
System.out.println("The Persons P2 and P3 are Equal");
else
System.out.println("The Persons P2 and P3 are not Equal");
//Creating Truck Class Object
Truck t=new Truck("Mahindra",4, p1,5000,4000);
//Displaying the truck class info
System.out.println(t.toString());
}
}
_____________________
Output:
The Persons P1 and P2 are Equal
The Persons P2 and P3 are Equal
Manufactures Name=Mahindra, No Of Cylinders=4, owner=Person [name=Kane Williams] load_capacity=5000.0, towing_capacity=4000
_______Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.