Using Arrays and Overloading Methods.

I am currently in an AP computer science class where we learn how to program in Java. Our assignment is as follows

"Use method overloading to write a program that will test to see if the parameters sent in are equal. The program should use loops to allow the user to enter in 2 or 3 integers and call the appropriate method based on how many numbers were put in. The program should loop until either the user enters a -1 (their sign that they are through entering in numbers) or they have entered 3 numbers in. You may assume that only positive integers will be input."

I was wondering if it was possible to summon a method for my array when it only has two numbers in it, and summon a different array for when it has 3 numbers in it. My program is designed to fill the array with two input numbers and then ask if they want to enter a third integer. If they do, the array will have three values, but if not, it will continue with just two. Is this even the right way to go about solving this problem? Our teacher doesn't explain it very well and I am really confused.

Here is my program so far

import java.util.Scanner;

public class equalparameters {

public static void main (String[] args){

Scanner reader = new Scanner(System.in);

int x, one, two, three, inumber,y;

int integer [] = new int[3];

y=0;

x=0;

inumber=0;

System.out.println("enter two or three integers. ");

for (x=0; x<3; x++);{

System.out.println("Please enter an integer");

inumber = reader.nextInt();

integer[x]=inumber;

x++;

if (x==1){

System.out.println("are you going to want to enter a third integer? 1. yes 2. no");

y = reader.nextInt();

if (y==2){

x = 3;

}

}

}

}

public static int compute (int integer[1]){

}

public static int compute (int integer[]){

int x, y;

return

}

}

Thanks for the help

Re: Using Arrays and Overloading Methods.

Reading in and storing the input from the user is not important. You can do it many ways. Just pick one. What is important is what method should be called. You should have 2 methods, one that has 2 int parameters and one that has 3 int parameters.

Code:

`public static int compute (int integer[1]){`

That is not legal syntax.

Re: Using Arrays and Overloading Methods.

What I was trying to ask was how to I differentiate between two parameters and three parameters. What do I put in the parentheses to show the same array, once with two values and once with three?

public static int compute (?????? two parameters)

public static int compute (?????? three parameters)

Re: Using Arrays and Overloading Methods.

Quote:

Originally Posted by

**dalek** What I was trying to ask was how to I differentiate between two parameters and three parameters. What do I put in the parentheses to show the same array, once with two values and once with three?

public static int compute (?????? two parameters)

public static int compute (?????? three parameters)

Here's an example of method overloading:

Code:

`public static int compute (int number1, int number2) // method 1`

public static int compute (int number1, int number2, int number3) // method 2

compute(5, 7) calls method 1 and compute(5, 7, 9) calls method 2.

Re: Using Arrays and Overloading Methods.

Quote:

Originally Posted by

**Junky** Reading in and storing the input from the user is not important. You can do it many ways. Just pick one.

Actually after re-reading the assignment it states clearly how you should read in the user input. Storing is still upto you.

Re: Using Arrays and Overloading Methods.

Quote:

Originally Posted by

**dalek** What I was trying to ask was how to I differentiate between two parameters and three parameters.

That is something for you to work out. Maybe using an if statement.

Code:

`if(....) {`

method(arr[0], arr[1]);

} else {

method(arr[0], arr[1], arr[2]);

}