Please help me write these Java classes. We are using the latest version of Java
ID: 3726297 • Letter: P
Question
Please help me write these Java classes. We are using the latest version of Java. Please post screenshots of your code and output since it is easier to see the format that way.
Create a class called Vehicle that has a manufacturer's name (type String), a number of cylinders in the engine (type int), and an owner (type Person given below). Then, create a class called Truck that is a derived class from the Vehicle class and has additional properties such as a load capac ity in tons (type double since it may contain a fractional part) and a towing capacity in pounds (type int). Be sure that your class has appropriate constructors, accessors, mutators, equals, and toString methods. The definition of the class Person is below. Complete the definitions of the methods. public class Person private String name; public Person public Person (String theName) ) public String getName ) ) public void setName (String theName) C ) public String toString)f... public boolean equals (Object other) .. ) The following presents a sample test program. If there's an error in the program, fix the error. public class Lab7Demo ( public static void main(Stringl] args) Person pinew Person System.out.println("p1: "pl); p1.setName("Bob"); System. out.println ("p1's name is: "p1.getName; Person p2 new Person("Joe"); System.out.println("p2:" p2);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 "name=" + name;
}
}
_____________________
Vehicle.java
public class Vehicle {
//Declaring instance variables
private String manufactures_name;
private int no_of_cylinders;
private Person owner;
public Vehicle() {
}
//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;
public Truck() {
}
//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;
}
}
____________________
Lab7Demo.java
public class Lab7Demo {
public static void main(String[] args) {
Person p1=new Person();
System.out.println("p1: "+p1);
p1.setName("Bob");
System.out.println("p1's name is : "+p1.getName());
Person p2=new Person("Joe");
System.out.println("p2: "+p2);
Person p3=new Person(p1);
System.out.println("p3 equal to p1: "+p3.equals(p1));
System.out.println("p2 equal to p1: "+p2.equals(p1));
Vehicle v1=new Vehicle();
System.out.println("v1: "+v1);
v1.setManufactures_name("ford");
v1.setNo_of_cylinders(4);
v1.setOwner(new Person("Joe"));
System.out.println("v1's manufacture is :"+v1.getManufactures_name());
System.out.println("v1's cylinder is :"+v1.getNo_of_cylinders());
System.out.println("v1's owner is :"+v1.getOwner());
Vehicle v2=new Vehicle();
System.out.println("v2: "+v2);
Truck t1=new Truck();
System.out.println("t1: "+t1);
t1.setLoad_capacity(54.36);
t1.setTowing_capacity(10);
System.out.println("t1: "+t1);
}
}
_______________________
Output:
p1: name=
p1's name is : Bob
p2: name=Joe
p3 equal to p1: true
p2 equal to p1: false
v1: Manufactures Name=null, No Of Cylinders=0, owner=null
v1's manufacture is :ford
v1's cylinder is :4
v1's owner is :name=Joe
v2: Manufactures Name=null, No Of Cylinders=0, owner=null
t1: Manufactures Name=null, No Of Cylinders=0, owner=null load_capacity=0.0, towing_capacity=0
t1: Manufactures Name=null, No Of Cylinders=0, owner=null load_capacity=54.36, towing_capacity=10
______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.