Create a class called Vehicle that has a manufacturer’s name (type String), a nu
ID: 3677537 • Letter: C
Question
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 capacity 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.
I started the Person Class
public class Person
{
private String name;
public Person()
{ … }
public Person (String theName)
{ … }
public String getName ( )
{ … }
public void setName (String theName)
{ … }
public String toString ( )
{ … }
public boolean equals (Object other)
{ … }
}
Demo Class
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.setManufacturer("Ford");
v1.setCylinders(4);
v1.setOwner(new Person("Joe"));
System.out.println("v1's manufacturer is: "+v1.getManufacturer() );
System.out.println("v1's cylinders: " + v1.getCylinders() );
System.out.println("v1's owner is: " + v1.getOwner() );
Vehicle v2 = new Vehicle(v1);
System.out.println("v2: " + v2);
Truck t1 = new Truck();
System.out.println("t1: " + t1);
t1.setLoadCap(54.36);
t1.setTowingCap(10);
System.out.println("t1: " + t1);
}
}
Explanation / Answer
Vehicle.java
package org.students;
public class Vehicle {
protected String manufactureName;
protected int noofcyclinders;
private Person owner;
public Vehicle() {
super();
}
public Vehicle(String manufactureName, int noofcyclinders, Person owner) {
super();
this.manufactureName = manufactureName;
this.noofcyclinders = noofcyclinders;
this.owner = owner;
}
public String getManufactureName() {
return manufactureName;
}
public void setManufactureName(String manufactureName) {
this.manufactureName = manufactureName;
}
public int getNoofcyclinders() {
return noofcyclinders;
}
public void setNoofcyclinders(int noofcyclinders) {
this.noofcyclinders = noofcyclinders;
}
public Person getOwner() {
return owner;
}
public void setOwner(Person owner) {
this.owner = owner;
}
@Override
public String toString() {
return "Vehicle [manufactureName=" + manufactureName
+ ", noofcyclinders=" + noofcyclinders +","+owner +"]";
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
Truck.java
package org.students;
public class Truck extends Vehicle {
private double loadcapacity;
private int towingcapacity;
public Truck() {
super();
}
public Truck(double loadcapacity, int towingcapacity) {
super();
this.loadcapacity = loadcapacity;
this.towingcapacity = towingcapacity;
}
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 String toString() {
return "Truck [loadcapacity=" + loadcapacity + ", towingcapacity="
+ towingcapacity +" manufactureName=" + manufactureName
+ ", no ofcyclinders=" + noofcyclinders+","+super.toString()+"]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(loadcapacity);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + towingcapacity;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Truck other = (Truck) obj;
if (Double.doubleToLongBits(loadcapacity) != Double
.doubleToLongBits(other.loadcapacity))
return false;
if (towingcapacity != other.towingcapacity)
return false;
return true;
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Test.java
package org.students;
public class Test {
public static void main(String[] args)
{
Truck t=new Truck();
t.setOwner(new Person("Joe"));
t.setManufactureName("Ford");
t.setNoofcyclinders(4);
t.setLoadcapacity(54.36);
t.setTowingcapacity(10);
Truck t1=new Truck();
t1.setOwner(new Person("William"));
t1.setManufactureName("Scoda");
t1.setNoofcyclinders(4);
t1.setLoadcapacity(64.36);
t1.setTowingcapacity(9);
System.out.println(t);
System.out.println(" ");
System.out.println(t1);
System.out.println(" ");
System.out.println("Owner Name:"+t.getOwner());
System.out.println("ManufactureName:"+t.getManufactureName());
System.out.println("No Of Cylinders:"+t.getNoofcyclinders());
System.out.println("Owner Name:"+t.getLoadcapacity());
System.out.println("Towing Capacity:"+t.getTowingcapacity());
System.out.println(" ");
System.out.println("Owner Name:"+t1.getOwner());
System.out.println("ManufactureName:"+t1.getManufactureName());
System.out.println("No Of Cylinders:"+t1.getNoofcyclinders());
System.out.println("Owner Name:"+t1.getLoadcapacity());
System.out.println("Towing Capacity:"+t1.getTowingcapacity());
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.