I am working on a project that involves creating a Vehicle class which has 3 data fields, one of which is an instance of a class rather than a fundamental data type (owner is an instance of the class Person). This class has multiple constructors for the varying initial values.
Now within my driver program for the Vehicle class (called VehicleTest) I have created a new Vehicle called honda and I would like to initialize a values for all 3 data members, but I am getting an error when I attempt to set a initial value for the Person field
The parameters are in the order as follows:
(Owner Name, Manufacturer, Cylinders)
Yet the following yields an error becuase it tries to set "Nick" to a string value when really it should be passing through as a Person.
Vehicle honda = new Vehicle("Nick","Honda",250);
The following is the Vehicle Class
import person.Person;
public class Vehicle
{
Scanner keyboard = new Scanner(System.in);
private Person owner = new Person();
private String manufacturer;
private int cylinders;
// Constructors
//default constructor
public Vehicle()
{
manufacturer = "none";
cylinders = 0;
owner.setName("none");
}
public Vehicle(Person initialOwner)
{
this.owner = initialOwner;
setOwner(owner);
// setName(getOwnerName(owner));
// set(initialOwner,"none",0);
// setOwner(owner);
// owner.setName(initialOwner);
}
The person class that the owner objects refers to is the following:
public class Person {
private String name;
public Person()
{
name = "No name";
}
public Person(String initialName)
{
name = initialName;
}
public void setName(String newName)
{
name = newName;
}
public String getName()
{
return name;
}
public void writeOutput()
{
System.out.println("Name: " + name);
}
public boolean hasSameName(Person otherPerson)
{
return (this.name.equalsIgnoreCase(otherPerson.getName()));
}
}
And the Vehicle Test class is the following:
public class VehicleTest
{
public static void main(String[] args)
{
Vehicle honda = new Vehicle();
honda.writeOutput("Nick",null,0); //This is the line with the error, it thinks that "Nick" should be passed through as a string, I am looking for a way to set it to the Person Owner instead that will reference the correct constructor within my Vehicle class
Any sort of insight would be much appreciated.