2 Replies - 458 Views - Last Post: 03 May 2010 - 04:05 PM Rate Topic: -----

#1 kr4vk0dr  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 25-October 09

Constructor Parameters

Posted 03 May 2010 - 10:05 AM

if i have 3 different object constructors all taking one parameter more then the last so for instance
janitor class constructor takes 7 strings
employee class constructor takes 8 strings
and the manager constructor takes 9 strings

so for example the janitor constructor parameters would look like
public janitor(String fname, String lname, String age, String hours, String age, String pay, String ID);



is there a way to pass an arraylist as string parameters to a method instead of labelling each string individually

public static void main(String[] args)
{	
ArrayList <String> L = new ArrayList <String>();
int count = 0;
try {
  Scanner feed = new Scanner (new File(in));		
	while(feed.hasNextLine())
	{
		String Line = feed.nextLine().replaceAll("\t"," ");
		Line = Line.trim();
		
		if(Line.length() > 0)
		{
                //then this string will be stored and used as a nth parameter in the object constructor
                // nth = count // so if count is 5 this is the 4th parameter if count is 0 it is the first
                L.add(Line);
                count++
                }
                else
                {
                //found a blank line
                //add object then prepare for new one
                 if (L.size()-1 == 7)
                 L = new Janitor();
                 else if (L.size()-1 == 8)
                 L = new employee();
                 else
                 L = new manager();  
                count = 0; 
 



Is This A Good Question/Topic? 0
  • +

Replies To: Constructor Parameters

#2 ElCam  Icon User is offline

  • New D.I.C Head

Reputation: -11
  • View blog
  • Posts: 39
  • Joined: 16-April 09

Re: Constructor Parameters

Posted 03 May 2010 - 10:31 AM

Hello!

I hope this can be of assistance! http://java.sun.com/.../ArrayList.html
This posts all the java methods including the one you are asking about. I hope this helps in anyway.
Was This Post Helpful? 0
  • +
  • -

#3 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Constructor Parameters

Posted 03 May 2010 - 04:05 PM

yes, but you will have to be cautious.
the class that gets 9 String Objects, will get an ArrayList with 9 String Objects, and the class that gets 7 String Objects will get a list of 7 String Objects. so make sure you keep track which class gets what so you wont try to access invalid indexes.
here is a very simple example:
public class janitor {
  
  private String fname;
  private String lname;
  ....
  private String ID;
  public janitor(ArrayList<String> strings){
    fname = strings.get(0);
    lname = strings.get(1);
    ...
    //has 7 String Objects
    ID = strings.get(6);
  }
}
/****************************/
public class manager {
  
  private String fname;
  private String lname;
  ....
  private String ID;
  public manager(ArrayList<String> strings){
    fname = strings.get(0);
    lname = strings.get(1);
    ...
    //has 9 String Objects
    ID = strings.get(8);
  }
}

make sure the ArrayLists DO have 7 or 9 String Objects as needed. I suggest that you'll check it and throw exception, or produce an error messege if not.

I believe there is inheritance between these classes?
if yes you can take advantage of it. I'll let you think about it yourself though :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1