Welcome to Dream.In.Code
Become a Java Expert!

Join 149,606 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,903 people online right now. Registration is fast and FREE... Join Now!




Undefined Method

 
Reply to this topicStart new topic

Undefined Method

Israel
21 Aug, 2007 - 05:49 PM
Post #1

D.I.C Addict
Group Icon

Joined: 21 Nov, 2004
Posts: 626


Dream Kudos: 175
My Contributions
I've been working on some code from my Java book in class, but to my surprise it did not compile. I checked my code a couple times but I still have errors at lines 65, 74, 76. The error at line 65 & 74 says "public type (name of class) must be defined in its own file" I tried putting that in it a separate file of the same package but still got errors. Line 76 says "GuessGame could not be resolved as a type" Here is my code:

CODE
public class guessingGame {
    Player p1;
    Player p2;
    Player p3;
    
    public void startGame(){
        p1 = new Player();
        p2 = new Player();
        p3 = new Player();
        
        int guessp1 = 0;
        int guessp2 = 0;
        int guessp3 = 0;
        
        boolean p1isRight = false;
        boolean p2isRight = false;
        boolean p3isRight = false;
        
        int targetNumber = (int) (Math.random() * 10);
        System.out.println("I'm thinking of a number between 0 and 9...");

        while(true) {
            System.out.println("Number to guess is " + targetNumber);
            
            p1.guess();
            p2.guess();
            p3.guess();
            
            guessp1 = p1.number;
            System.out.println("Player one guessed " + guessp1);
            
            guessp2 = p2.number;
            System.out.println("Player two guessed " + guessp2);
            
            guessp3 = p3.number;
            System.out.println("Player three guessed " + guessp3);
            
            if(guessp1 == targetNumber){
                p1isRight = true;
            }
            if(guessp2 == targetNumber){
                p2isRight = true;
            }
            if(guessp3 == targetNumber){
                p3isRight = true;
            }
            
            if (p1isRight || p2isRight || p3isRight){
                
                System.out.println("We have a winner!");
                System.out.println("Player one got it right? " + p1isRight);
                System.out.println("Player two got it right? " + p2isRight);
                System.out.println("Player three got it right? "+ p3isRight);
                System.out.println("Game is over.");
                break; // game is over break the loop
                
            }     else{
                // keep going until someone gets it right
                System.out.println("Try again Bitch!");
            }    
        }
    }    
} // end class

public class Player {
    int number = 0; // where the guess goes
    
    public void guess() {
        number = (int) (Math.random() * 10);
        System.out.println("I'm guessing " + number);
    }    
}

public class GameLauncher {
    public static void main (String[] args){
        guessingGame game = new GuessGame();
        game.startGame();
    }
}

User is offlineProfile CardPM
+Quote Post

GWatt
RE: Undefined Method
21 Aug, 2007 - 07:04 PM
Post #2

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,356



Thanked: 31 times
Dream Kudos: 500
My Contributions
Each class needs to be in its own file that shares the same name as the class. Also, you really don't need to declare a package. I've found that that confuses the issue greatly. Just make sure they're all in the same directory.
User is offlineProfile CardPM
+Quote Post

Israel
RE: Undefined Method
21 Aug, 2007 - 10:21 PM
Post #3

D.I.C Addict
Group Icon

Joined: 21 Nov, 2004
Posts: 626


Dream Kudos: 175
My Contributions
Ok, I did that. I took GameLauncher and Player and put them in there own separate class files like so:

CODE
public class GameLauncher {
    public static void main (String[] args){
        guessingGame game = new guessingGame();
        game.startGame();
    }
}

and...
CODE
public class Player {
    int number = 0; // where the guess goes
    
    public void guess() {
        number = (int) (Math.random() * 10);
        System.out.println("I'm guessing " + number);
    }    
}


But I still get the error: " The method guess() is undefined for type Player"

User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Undefined Method
22 Aug, 2007 - 01:40 AM
Post #4

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Hi Israel, I compiled it and I didn't get that error " The method guess() is undefined for type Player" , it compiled just fine for me, try to recompile or check if you are missing something.

This post has been edited by PennyBoki: 22 Aug, 2007 - 01:40 AM
User is offlineProfile CardPM
+Quote Post

Israel
RE: Undefined Method
22 Aug, 2007 - 08:38 PM
Post #5

D.I.C Addict
Group Icon

Joined: 21 Nov, 2004
Posts: 626


Dream Kudos: 175
My Contributions
I did create a new Java project, copied the code I last posted above and I'm still getting a couple errors in lines 25,26,27,29,32, and 35?
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Undefined Method
23 Aug, 2007 - 03:27 AM
Post #6

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,306



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Which lines are those referring to in your code??
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 11:58PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month