I was working on the wrong assignment, so I hammered this out, it's almost due, and I am having trouble figuring out how to test my program. Eclipse keeps showing errors when trying to figure out how to populate my fields, assuming I have the rest of the program right. We have worked prior to this with keyboard input and this is the first assignment with inheritance.
present errors
toString cannot be resolved or is not a field, and Constructor Person("String") is undefined.
/**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
below). 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 muta-
tor methods, and suitably defined equals and toString methods. Write a pro-
gram to test all your methods.
*
*
*
*/
public class Program5 {
/**
*
*/
public static void main(String[] args) {
Truck chevy = new Truck(new Person ("ChevyOwner"), "Chevy", 8, .75, 5000);
String testString;
System.out.println(chevy.toString);
}
}
public class Person {
private String name;
public Person() {
name = "No name";
}
public Person(String initialName) {
name = initialName;
}
public Person(Person theObject) {
name = theObject.name;
}
public String getName() {
return name;
}
public void setName(String theName) {
name = theName;
}
public String toString() {
return("Name: "+name);
}
//equals method
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Person))
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;
}
}
public class Vehicle {
private Person owner = new Person();
private String manufacturerName;
private int numCylinders;
//default constructor
//Override toString
public String toString() {
return "Vehicle [manufacturerName=" + manufacturerName + ", numCylinders="
+ numCylinders + ", owner=" + owner + "]";
}
public Vehicle(Person owner, String manufacturerName, int numCylinders) {
super();
this.owner = owner;
this.manufacturerName = manufacturerName;
this.numCylinders = numCylinders;
}
public String getManufacturerName() {
return manufacturerName;
}
public void setManufacturerName(String manufacturerName) {
this.manufacturerName = manufacturerName;
}
public int getNumCylinders() {
return numCylinders;
}
public void setNumCylinders(int numCylinders) {
this.numCylinders = numCylinders;
}
public Person getOwner() {
return owner;
}
public void setOwner(Person owner) {
this.owner = owner;
}
//Override equals
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Vehicle))
return false;
Vehicle other = (Vehicle) obj;
if (manufacturerName == null) {
if (other.manufacturerName != null)
return false;
} else if (!manufacturerName.equals(other.manufacturerName))
return false;
if (numCylinders != other.numCylinders)
return false;
if (owner == null) {
if (other.owner != null)
return false;
} else if (!owner.equals(other.owner))
return false;
return true;
}
}
public class Truck extends Vehicle{
private double loadCapacityTons;
private int towCapacityLbs;
public Truck(Person owner, String manufacturerName, int numCylinders, int loadCapacity, int towCapacity) {
super(owner, manufacturerName, numCylinders);
setLoadCapacityTons(loadCapacity);
setTowCapacityLbs(towCapacity);
}
public double getLoadCapacityTons() {
return loadCapacityTons;
}
public void setLoadCapacityTons(double loadCapacityTons) {
this.loadCapacityTons = loadCapacityTons;
}
public int getTowCapacityLbs() {
return towCapacityLbs;
}
public void setTowCapacityLbs(int towCapacityLbs) {
this.towCapacityLbs = towCapacityLbs;
}
//toString override
public String toString() {
return "Truck [loadCapacityTons=" + loadCapacityTons
+ ", towCapacityLbs=" + towCapacityLbs
+ ", getLoadCapacityTons()=" + getLoadCapacityTons()
+ ", getTowCapacityLbs()=" + getTowCapacityLbs()
+ ", toString()=" + super.toString()
+ ", getManufacturerName()=" + getManufacturerName()
+ ", getNumCylinders()=" + getNumCylinders() + ", getOwner()="
+ getOwner() + ", getClass()=" + getClass();
}
//equals override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (!(obj instanceof Truck))
return false;
Truck other = (Truck) obj;
if (Double.doubleToLongBits(loadCapacityTons) != Double
.doubleToLongBits(other.loadCapacityTons))
return false;
if (towCapacityLbs != other.towCapacityLbs)
return false;
return true;
}
}