Results 1 to 7 of 7

Thread: Calling Methods in Guessing Game;

  1. #1
    Join Date
    Nov 2005
    Posts
    2,008
    Rep Power
    0

    Exclamation Calling Methods in Guessing Game;

    I have this code to complete but the instructions state the main method should be left as is which is as follows:

    Code:
    import javax.swing.JOptionPane;
    public class GuessingGame {
        
        public static void main(String[] args){
            int guess; // the user’s guess
            int target = getnum(); //a random number selected by the computer
            
            do{
                guess = getguess(); //gets a guess from the user
                checkguess(guess,target); // compares the guess to the target
            }while (guess != target); /* keep doing this as long as the user has not
                                      * guessed the target number
                                      */        
        }
    I get errors on the int target = getnum(); and guess = getguess(); lines.

    GuessingGame.java:12: getnum(int) in GuessingGame cannot be applied to ()
    int target = getnum(); //a random number selected by the computer

    GuessingGame.java:15: getguess(int) in GuessingGame cannot be applied to ()
    guess = getguess(); //gets a guess from the user


    I have more code below without error so is it safe to assume the instructions are wrong about not changing the main method? It seems the blank parameters ( ) called by the parameters I wrote is the problem in the main method.
    [AMD Gallardo] [Asus M2N SLI Deluxe] [AMD 5000+ X2 black edition @ 3.2GHZ] [Zalman CNPS9700 LED][ EVGA 8800GT 512MB] [Gskill 2GB x 2 DDR2800] [Seagate 320GB IDE][Seagate 1TB, 750GB, 320GB SATA II][Western Digital 1TB SATA II][XION 600W] [NEC DL DVD-RW] [Antec Nine Hundred MidATX] [19" BenQ FP93GX LCD][40" Samsung LNA500LCD HDTV 1080p][Logitech X-230 2.1]

  2. #2
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    yeah, i think the method was declared to take an integer, but youre not passing one to it when its being called
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  3. #3
    Join Date
    Nov 2005
    Posts
    2,008
    Rep Power
    0

    Default

    hmm here is the rest of my code. I know the last part is probably wrong but I can fix that once I get to compile it first since its a compile error and not a run-time error.


    Code:
    public static int getnum(int target){
    // returns a random integer from 1-100
        target = (int) (Math.random() * 100);
                return target;
    }
    public static int getguess(int guess){
    // asks the user for their guess, an integer from 1-100
        String guessString = JOptionPane.showInputDialog(
                "Enter an Integer between 0 and 100: ");
        guess = Integer.parseInt(guessString);
        // returns the guess
        return guess;
    }
    public static void checkguess(int g, int t){
    /** compares the user’s guess, passed in as parameter g, with
    * the random target, passed in as parameter t.
    * prints a message stating whether the guess is too high, too low,
    * or an exact match (thus ending the game)
    */
        String result;
        if (g < t)
            result = "Your guess is too low. Guess again! ";
        else if (g > t)
            result = " Your guess is too high. Guess again! ";
        else if (g == t)
            result = "You have guessed RIGHT! Congratulations!!!";
        
         JOptionPane.showMessageDialog(null, result);
            
        }
    Last edited by Genesis2kx; Mar 25, 2008 at 09:19 PM.
    [AMD Gallardo] [Asus M2N SLI Deluxe] [AMD 5000+ X2 black edition @ 3.2GHZ] [Zalman CNPS9700 LED][ EVGA 8800GT 512MB] [Gskill 2GB x 2 DDR2800] [Seagate 320GB IDE][Seagate 1TB, 750GB, 320GB SATA II][Western Digital 1TB SATA II][XION 600W] [NEC DL DVD-RW] [Antec Nine Hundred MidATX] [19" BenQ FP93GX LCD][40" Samsung LNA500LCD HDTV 1080p][Logitech X-230 2.1]

  4. #4
    Join Date
    May 2005
    Posts
    59
    Rep Power
    0

    Default

    check out changes to all 3 methods...btw notice the methods are
    only static because you are calling them directly
    in the static main method, normally you would create an instance and use that
    Code:
    *************
    public static void main(String[] args){
            GuessingGame newGame = new GuessingGame();
           ....................
            int target = newGame.getnum();
         //a random number selected by the computer
            .....................      
    
    *************
    
    
    public static int getnum(){
    // returns a random integer from 1-100
       return  (int) (Math.random() * 100);
    }
    
    public static int getguess(){
    // asks the user for their guess, an integer from 1-100
        String guessString = JOptionPane.showInputDialog(
                "Enter an Integer between 0 and 100: ");
        return Integer.parseInt(guessString);
    }
    
    public static void checkguess(int g, int t){
    /** compares the user’s guess, passed in as parameter g, with
    * the random target, passed in as parameter t.
    * prints a message stating whether the guess is too high, too low,
    * or an exact match (thus ending the game)
    */
        String result = "";
        if (g < t)
            result = "Your guess is too low. Guess again! ";
        else if (g > t)
            result = " Your guess is too high. Guess again! ";
        else 
            result = "You have guessed RIGHT! Congratulations!!!";
        
         JOptionPane.showMessageDialog(null, result);        
     }
    Last edited by jagfun; Mar 26, 2008 at 07:04 PM. Reason: added CODE tags
    "I don't believe in astrology; I'm a Capricorn and we're skeptical."

  5. #5
    Join Date
    Nov 2005
    Posts
    2,008
    Rep Power
    0

    Default

    Hmm....is there a solution without changing the Main method...The task was to leave it as is and fill in the other 3 methods.
    [AMD Gallardo] [Asus M2N SLI Deluxe] [AMD 5000+ X2 black edition @ 3.2GHZ] [Zalman CNPS9700 LED][ EVGA 8800GT 512MB] [Gskill 2GB x 2 DDR2800] [Seagate 320GB IDE][Seagate 1TB, 750GB, 320GB SATA II][Western Digital 1TB SATA II][XION 600W] [NEC DL DVD-RW] [Antec Nine Hundred MidATX] [19" BenQ FP93GX LCD][40" Samsung LNA500LCD HDTV 1080p][Logitech X-230 2.1]

  6. #6
    Join Date
    May 2005
    Posts
    59
    Rep Power
    0

    Default

    you don't need to touch the main method just use the other 3 methods as shown...I only edited the main to point out that using static methods was not recommended.
    "I don't believe in astrology; I'm a Capricorn and we're skeptical."

  7. #7
    Join Date
    Nov 2005
    Posts
    2,008
    Rep Power
    0

    Default

    Oh zeen. Thanks yute! I see what the prob was now.
    [AMD Gallardo] [Asus M2N SLI Deluxe] [AMD 5000+ X2 black edition @ 3.2GHZ] [Zalman CNPS9700 LED][ EVGA 8800GT 512MB] [Gskill 2GB x 2 DDR2800] [Seagate 320GB IDE][Seagate 1TB, 750GB, 320GB SATA II][Western Digital 1TB SATA II][XION 600W] [NEC DL DVD-RW] [Antec Nine Hundred MidATX] [19" BenQ FP93GX LCD][40" Samsung LNA500LCD HDTV 1080p][Logitech X-230 2.1]

Posting Permissions

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