Results 1 to 2 of 2
  1. #1
    khurls is offline Member
    Join Date
    Oct 2013
    Posts
    1
    Rep Power
    0

    Default Help with calling a public method from another class

    In my AP Computer Science class we are coding a game called Nim. I have to call the getMode function from my Player class to display it in my Nim class and I can't figure out for the life of me what I am doing wrong. I keep getting a "Cannot find symbol error". Can anyone help me out?

    Java Code:
    public class Nim
    {
        private Pile thePile;
        private Player humanPlayer;
        private Player computerPlayer;
        
        public Nim()
        {
            humanPlayer = new Player("human");
            computerPlayer = new Player("computer");
            thePile =  new Pile();
        }
        
        public void playGame()
        {
            Player cointoss = coinToss();
                System.out.println(cointoss.getName() + " goes first!");
            String nimMode = new String();
                System.out.println("Computer is playing: " + nimMode.getMode());
            
            //while(no winner)
            //{
                
        }
        
        private Player coinToss()
        {
            int choice = (int)(Math.random() * 1);
            //System.out.println(choice);
            if(choice==0)
                return(humanPlayer);
            else
                return(computerPlayer); 
        }
    }
    Java Code:
    public class Player
    {
        private String name;
        private String playerType;
        private String mode;
        
        public Player (String inPlayerType)
        {
            playerType = inPlayerType;
            name = askName();
            mode = initializeMode();
            
        }
        
        public String askName()
        {
            Scanner inputReader = new Scanner(System.in);
            
            if(playerType.equals("human"))
            {
                System.out.print("Enter name: ");
                name = inputReader.nextLine();
            }
            else
            {
                System.out.print("Enter computer: ");
                name = inputReader.nextLine();
            }
            return(name);
        }
        
        private String initializeMode()
        {
            int pickmode = (int)(Math.random()*1);
            if(pickmode==0)
                mode = "smart";
            else
                mode = "stupid";
            return (mode);
        }
        
        public String getMode()
        {
            return mode;    
        }
        
        public String getName()
        {
            return name;
        }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,378
    Rep Power
    5

    Default Re: Help with calling a public method from another class

    Your trying to call getMode() from a String instance. You need to call getMode from either of your Player instances. Next time, please show the entire error
    message with correct line numbers.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 8
    Last Post: 04-18-2012, 04:58 PM
  2. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 02:48 AM
  3. How to return to next line of code after calling a public class?
    By devdevi@comcast.net in forum New To Java
    Replies: 1
    Last Post: 02-02-2009, 06:46 PM
  4. calling a public void method from a class button
    By supa_kali_frajilistik in forum AWT / Swing
    Replies: 4
    Last Post: 05-23-2008, 01:05 PM
  5. calling a public void method from a class button
    By supa_kali_frajilistik in forum AWT / Swing
    Replies: 1
    Last Post: 05-21-2008, 05:40 AM

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
  •