Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Junky

Thread: Using Arrays and Overloading Methods.

  1. #1
    dalek is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default 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

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default 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.
    Java Code:
    public static int compute (int integer[1]){
    That is not legal syntax.
    Solarsonic likes this.

  3. #3
    dalek is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default 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)

  4. #4
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default Re: Using Arrays and Overloading Methods.

    Quote Originally Posted by dalek View Post
    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:
    Java 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.
    Last edited by Solarsonic; 10-25-2011 at 05:53 AM.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Using Arrays and Overloading Methods.

    Quote Originally Posted by Junky View Post
    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.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Using Arrays and Overloading Methods.

    Quote Originally Posted by dalek View Post
    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.
    Java Code:
    if(....) {
        method(arr[0], arr[1]);
    } else {
        method(arr[0], arr[1], arr[2]);
    }

Similar Threads

  1. Overloading abstract methods??!?
    By tfitz666 in forum New To Java
    Replies: 2
    Last Post: 03-07-2010, 10:45 AM
  2. method overloading and arrays.
    By glopez09 in forum New To Java
    Replies: 5
    Last Post: 12-08-2009, 04:02 PM
  3. Arrays and methods
    By namie in forum New To Java
    Replies: 3
    Last Post: 10-05-2009, 10:43 AM
  4. Arrays & Methods
    By TheRocket in forum New To Java
    Replies: 1
    Last Post: 12-10-2008, 08:37 PM
  5. Overloading methods dynamically
    By Syed.muddasir in forum New To Java
    Replies: 6
    Last Post: 07-31-2007, 03:34 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •