Hello there!

I am new to Java and DW, though I usually find great help already here!

Say I need a constructor that accepts variable number of parameters up to 2 parameters, but if I pass only one parameter to this constructor, both parameters will take this number, and if I passed two parameters, they're treated normally. I tried to do it with only 1 parameter, but my program crashes with:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:

I think because it's supposed to take 2 params? if so, any idea how can I do that? (to pass only 1 param to a 2 param constructor)

Sample code:

public createTables (int rows, int columns) // Or is it better to do it like (int[][] Table) ?
{
	int[][] Table = new int [row][column];
	
		if (Table[column].length < 0) // no second parameter is passed ( is that how to track the second dimension in multi-D arrays?)
		column = row;
			else
			{
			row = row;
			column = column;
			}
}

The above code is just a sample, the real assignment is far too long from this :-/

Thanks in advance!

That sounds like the way to go, but sadly I cannot use something we didn't study yet :(

It's my very first programming course.

Who says (its called initiative)? Did you ask?

In any case, he is probably hinting at method overloading in that case

public void doit(int argOne) {
    doit(argOne, argOne);
}
public void doit(int argOne, int argTwo) {
    // do whatever
}

Who says (its called initiative)? Did you ask?

In any case, he is probably hinting at method overloading in that case

public void doit(int argOne) {
    doit(argOne, argOne);
}
public void doit(int argOne, int argTwo) {
    // do whatever
}

method overloading is something we've been doing lately, I could use that but the assignment specifically said:

A second constructor is required. This constructor accepts a variable number of parameters. The maximum
number of parameters that this constructor accepts is two (2).
a. If the user passes more than 2 parameters to this constructor a meaningful error message is
displayed and the program terminates.
b. If the user passes 1 parameter to this constructor, a square table is constructed.
For example: if the user enters: TimesTable(5), this will create a 5 x 5 table in memory. If the user enters: TimesTable(7), this will create a 7 x 7 table in memory etc.
c. If the user passes 2 parameters to this constructor, a rectangle (or perhaps a square) table is constructed.
The first parameter defines the number of columns; the second parameter defines the number of rows.
For example: if the user enters: TimesTable(5,2), this will create a table that has 5 columns and 2
rows in memory. If the user enters: TimeTable(2,5), this will create a table that has 2 columns
and 5 rows in memory. And should the user enter: TimesTable(5,5), a square table with 5 rows
and 5 columns is created.

Any thoughts? As you saw guys, I am not trying to find someone to do the assignment for me, but rather, to help me through the areas I am stuck at and understand what's going on lol.

Thanks a lot!

The only way a method or costructor can take a "varying number of parameters" is through varargs. The only other thing is to simply use an array (as the parameter) and check its length. But then, the constructor is still taking just one parameter (not two as stated in the description) its just an array that its receiving, but that doesn't jive with the description either. The only thing that matches the description, as written, is varargs, so, take your choice, use an array (and one parameter), or mulitple overloaded constructors, or do it right and use varargs.

You know, I am not trying to hide things from or mislead you, you know. If you have to do exactly what that description says, then varargs is the only way. As already mentioned, using an array means the constructor takes one parameter, not two. Using overloaded constructors is not really feasible due to the description that more than two parameters must produce a "useful" error message, meaning you would have to create hundreds of the constructors to handle all possible invalid constructor calls (i.e. one with 3, one with four, one with five, etc and a constructor can have hundreds of parameters and you'd have to write a constructor for everyone. You could also write you own object for "passing" the parameters but everything said about the array applies to this, too.

So, as already said, the only "feasable" way to completely satisfy the description is varargs.

I'm not sure I'm understanding the question fully. Is the "user" in this case the consumer of a piece of library code or a luser at the terminal running some program you've written.

That is, is the user's input a piece of code running a method, or some text entered at the command line in response to a prompt?

If the former, then I believe Masijade is correct - I see no way to create the functionality you're talking about, specifically the "useful error message", without varargs. If it's the latter, I think I see where your instructor is going.

As I read it, the user is prompted to enter the dimensions of an array, probably in the form x[,y?] where y is optional. The program then is to parse this string and determine whether it is an x or an x and a y, and to respond correctly.
In this case, it's actually not difficult to do what you're being asked to do. You need take input from the user as a String, and split it on ','. The resulting array will have 1, 2, or more members. If it's "more", report incorrect input. If it's two, convert to integer values and construct as x,y. If it's one, construct as x,y, where y = x.

The portion of the instructions you've posted isn't very clear, so I don't know whether this is closer to what you're meant to be doing, but it seems at least plausible to me that this is what's intended. Maybe if you post the full assignment text, it'll be easier to determine what's going on.

It's should be done using Varargs, I happened to be abscent during that lecture, my bad lol.

Now it's soooooooo much easier using Varargs, thanks masijade, jon.kiparsky and sorry for the misunderstanding!

Be a part of the DaniWeb community

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