• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

constructors with arguments calling constructors with arguments

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am new to this forum and I am desperate
I am going batty with what must be a simple problem.
I have a Company class and an Employee class. I also have a Project class that contains main() and instantiates the constructors of Company and Employee. From Project I can successfully get and set values of the private variables of the Company and Employee class. What I cannot figure out how to do is create a constructor and instantiate the constructor so that I can create a Company object within the Employee class.
Below is a snippet of the Company class:
public class Company
{ //beginning of Company class

//declare private variables and initialize values to empty strings
private String name = "";
private String address = "";
private String city = "";
private String state = "";
private String zip = "";

//constructor method
public Company()
{
}

public Company(String inName, String inAddress, String inCity,
String inState, String inZip)
{
name = inName;
address = inAddress;
city = inCity;
state = inState;
zip = inZip;
}

//define accessor methods for private local variables
public String getName()
{
return name;
}

public String getAddress()
{
return address;
}

public String getCity()
{
return city;
}

public String getState()
{
return state;
}

public String getZip()
{
return zip;
}
//end accessor methods

//begin mutator methods for private local variables
public void setName(String inName)
{
name=inName;
}

public void setAddress(String inAddress)
{
address=inAddress;
}

public void setCity(String inCity)
{
city=inCity;
}

public void setState(String inState)
{
state=inState;
}

public void setZip(String inZip)
{
zip=inZip;
}
//end mutator methods


} //end of Company class
Here is a snippet of the Employee Class:
public class Employee
{ //begin Employee class

//declare private variables and initialize values
private String firstName = "";
private String lastName = "";
private short seniority = 0;
private double baseSalary = 500.0;
private String address = "";
private String city = "";
private String state = "";
private String zip = "";
private Company company;

//constructor methods

public Employee(String inFirstName, String inLastName, short inSeniority,
double inBaseSalary, String inAddress, String inCity,
String inState, String inZip, Company inCompany)
{
firstName = inFirstName;
lastName = inLastName;
seniority = inSeniority;
baseSalary = inBaseSalary;
address = inAddress;
city = inCity;
state = inState;
zip = inZip;
company = inCompany;
}


//define accessor methods for private local variables
public String getFirstName()
{
return firstName;
}

public String getLastName()
{
return lastName;
}

public String getAddress()
{
return address;
}

public String getCity()
{
return city;
}

public String getState()
{
return state;
}

public String getZip()
{
return zip;
}

public short getSeniority()
{
return seniority;
}

public double getBaseSalary()
{
return baseSalary;
}

public Company getCompany()
{
return company;
}

//end accessor methods

//begin mutator methods for private local variables
public void setFirstName(String inFirstName)
{
firstName=inFirstName;
}

public void setLastName(String inLastName)
{
lastName=inLastName;
}

public void setAddress(String inAddress)
{
address=inAddress;
}

public void setCity(String inCity)
{
city=inCity;
}

public void setState(String inState)
{
state=inState;
}

public void setZip(String inZip)
{
zip=inZip;
}

public void setSeniority(short inSeniority)
{
seniority=inSeniority;
}
public void setBaseSalary(double inBaseSalary)
{
baseSalary=inBaseSalary;
}

public void setCompany(Company inCompany)
{
company = inCompany;
}

//end mutator methods
}//end Employee class
Now what I would like to do in Project is instantiate a Company object inside an Employee object so that I could do the following:
//create new Employee object
//this is obviously wrong, but I can figure out what to do, empCompany is
//supposed to instantiate a Company object and all its arguments

Employee salesEmployee = new Employee(empFirstName, empLastName,
empSeniority, empBaseSalary, empAddress, empCity, empState, empZip, empCompany);
salesEmployee.setCompany().setName();
salesEmployee.getCompany().getName();
Thanks in advance. If this is unclear it is due to no sleep in 48 hours, please just ask if you need clarification
Thanks
Dan
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi -
I don't know if got you right. But what you can do is - within the constructor of the employee class, you could call the constructor of the company class -
//pass whatever arguments you want to the employee constructor
public Employee( Company cmp){
cmp = new Company(String inName, String inAddress, String inCity,
String inState, String inZip);
// other stuff
}
 
Dan Taylor
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I tried that before and I just tried it again. I get an error when I try to build the Employee Class. Below is a snippet of what I did to the Employee constructor. All other code has remained unchanged:
public Employee(String inFirstName, String inLastName, short inSeniority,
double inBaseSalary, String inAddress, String inCity,
String inState, String inZip, Company inCompany)
{
firstName = inFirstName;
lastName = inLastName;
seniority = inSeniority;
baseSalary = inBaseSalary;
address = inAddress;
city = inCity;
state = inState;
zip = inZip;
inCompany = new Company(String inName, String inAddress, String inCity, String inState, String inZip);
}
I get 2 errors

This is driving me nuts. I am taking a Java course and I haven't had any problems with any assignment till now. Thanks
Dan
[ April 16, 2004: Message edited by: Dan Taylor ]
[ April 16, 2004: Message edited by: Dan Taylor ]
 
Hari priya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Heyy if thats the code you exactly wrote in the constructor -
inCompany = new Company(String inName, String inAddress, String inCity, String inState, String inZip);
change it to
inCompany = new Company(inName, inAddress,inCity, inState,inZip);
don't mention the argument types. try that out and see if you still get errors.
 
Dan Taylor
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Radha
thanks that solved the constructor in the Employee class. How would I instantiate the object?
I tried this:

But I get this build error

[ April 16, 2004: Message edited by: Dan Taylor ]
 
Dan Taylor
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have determined that I can execute
System.out.println(salesEmployee.getCompany().getName());
And it works
So my getCompany() method works inside the salesEmployee object but my setCompany() method gives me the error I mentioned in the last post. My setCompany() code is listed in the Employee class file snippet listed in my first post.
 
Hari priya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dan -
You are getting that error because, set company method in your code takes and argument Company but the way you are using it is without arguments like
salesEmployee.setCompany().setName(MyInput.readString());
INSTEAD,
send an object of Company in that made. If cmp is an object of the company, use
salesEmployee.setCompany(cmp).setName(MyInput.readString());
That should solve the problem.
 
Dan Taylor
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get an error stating that 'void cannot be dereferenced'
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this one:

First, get a company object; second, perform setXY() on it.
[ April 17, 2004: Message edited by: Tobias Hess ]
 
Dan Taylor
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
I actually figured that out late last night.
Dan
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic