Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Review newbie code and confirm?? 1

Status
Not open for further replies.

lespaul

Programmer
Feb 4, 2002
7,083
US
So, I'm creating a class and want to make sure that I've understood everything I've been reading about OOP and constructors and all other information.

In this bit of code, when I call the class constructor, it will automatically determine the number of arguments and use the correct constructor....am I missing something or does this seem okay.....
thanks!

Leslie
Code:
import functions.*;
import java.sql.Connection;
import java.sql.*;
//----------------------------------------------------------------------------------------
//Calculate next available TAHearing Date and Time
//Additionally cancel and reschedule an existing TAHearing
/*
 * Created December 2007 
 * Author: Leslie Andrews
 * declaration : first java program EVER!!
 */

//----------------------------------------------------------------------------------------
public class TAHearingDate {
	String requestType;
	String manNumber;
	String issueAgency;
	Date existingDateSetting;
	Time existingTimeSetting;
	Date newHearingDate;
	Time newHearingTime;
	
	/**
	 * @param args
	 */
	
	public static void main(String[] args) throws java.lang.InterruptedException
	{
		TAHearingDate NewRequest = new TAHearingDate(args[0], args[1], args[2], args[3], args[4]);
		
	}
	
	//constructor for TAHearingDate
	public TAHearingDate(String inputType, String inputAgency, String inputMan){
		requestType = inputType;
		issueAgency = inputAgency;
		manNumber = inputMan;		
	}
	
	//constructor for ExistingTAHearingDate
	public TAHearingDate(String inputType, String inputAgency, String inputMan, String inputExistingDate, String inputExistingTime){
		requestType = inputType;
		issueAgency = inputAgency;
		manNumber = inputMan;
		existingDateSetting = Date.valueOf(inputExistingDate);
		existingTimeSetting = Time.valueOf(inputExistingTime);
		
	}
	
	


}

I'm sure my naming conventions could use some work, but at this point using existing, and input really help me "see" what's happening!

thanks for any help.

Leslie

In an open world there's no need for windows and gates
 
No, it will not.

You allways call the one with 5 arguments.
Calling your code with 3 Arguments will throw an ArrayIndexOutOfBoundsException (which you could have tested easyly).
Check for the number of arguments, and call the right one yourself:
Code:
TAHearingDate newRequest = null;
if (args.length == 5)
	newRequest = new TAHearingDate (args [0], args [1], args [2], args [3], args [4]);
else 
	newRequest = new TAHearingDate (args [0], args [1], args [2]);

Another approach:
Code:
 public static void main (String [] args)
	{
		TAHearingDate newRequest = new TAHearingDate (args);
	}

	public TAHearingDate (String [] params)
	{
		if (params.length > 0) 
			requestType = params[0];
		if (params.length > 1) 
			issueAgency = params[1];
 		//...

don't visit my homepage:
 
Calling your code with 3 Arguments will throw an ArrayIndexOutOfBoundsException (which you could have tested easyly).

Yes, I did finally figure that out, but thanks for showing me how to detect the number of arguments and correctly determine what needs to be done next...I'm curious though why this site indicates that you CAN have more than a single constructor....what am I missing?

I'm going from Delphi to Java and it's not an easy transition! I appreciate your help and I KNOW I'll be back for more!



Leslie
 
You can have more than one, with different number and type of parameters, and depending on the parameters you use in the invocation one or another will be used.

Cheers,
Dian
 
My first examples calls either the 5- or the 3-argument constructor.

But
a) A constructor-call with 3 not null arguments and 2 null arguments will still call the constructor with 5 arguments (which is not, what happend in your case, because of...)
b) you try to access args[3], but the JVM just got a 3-argument array, which leads to the AIOOBException.



don't visit my homepage:
 
ok thanks for the expansion....part of my problem with this is that really what I'm creating as a class should really be a method of a different class.

I'm not sure which one since I've only started the UML model and that model is really just a sample to convince my superiors that instead of just throwing together a bunch of java classes we need to analyze our system and develop a model BEFORE we start generating tons of code.

sorry for the rant, I DO really appreciate your help!

Leslie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top