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;
	public Vehicle(Person initialOwner)
		this.owner = initialOwner;
//		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.

Hm, I can't see anywhere a method writeOutput, which takes those 3 arguments... But if You say that first argument should be passed as a Person object, then You have to create an instance of Person class and then pass it through Your method:

Person nick = new Person("Nick");
honda.writeOutput( nick, null, 0 );


honda.writeOutput( new Person("Nick"), null, 0 );
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.