Results 1 to 6 of 6
- 10-25-2011, 04:26 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 2
- Rep Power
- 0
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
- 10-25-2011, 04:31 AM #2
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.
That is not legal syntax.Java Code:public static int compute (int integer[1]){
- 10-25-2011, 04:48 AM #3
Member
- Join Date
- Oct 2011
- Posts
- 2
- Rep Power
- 0
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)
- 10-25-2011, 04:50 AM #4
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
Re: Using Arrays and Overloading Methods.
Last edited by Solarsonic; 10-25-2011 at 04:53 AM.
- 10-25-2011, 04:50 AM #5
- 10-25-2011, 04:52 AM #6
Similar Threads
-
Overloading abstract methods??!?
By tfitz666 in forum New To JavaReplies: 2Last Post: 03-07-2010, 09:45 AM -
method overloading and arrays.
By glopez09 in forum New To JavaReplies: 5Last Post: 12-08-2009, 03:02 PM -
Arrays and methods
By namie in forum New To JavaReplies: 3Last Post: 10-05-2009, 09:43 AM -
Arrays & Methods
By TheRocket in forum New To JavaReplies: 1Last Post: 12-10-2008, 07:37 PM -
Overloading methods dynamically
By Syed.muddasir in forum New To JavaReplies: 6Last Post: 07-31-2007, 02:34 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks