Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By fredlo2010

Thread: Access a method of a class from Main.

  1. #1
    fredlo2010 is offline Member
    Join Date
    Jan 2014
    Posts
    17
    Rep Power
    0

    Default Access a method of a class from Main.

    Hello guys,

    As some of you might already know I am a student trying to learn Java and I found this Forum extremely helpful so I have returned here several times.

    So here I again. This time I have to make a Black Jack game ( I guess this is a classic) I have created Three classes for this BlackJack( Main), Card, and Player.

    What I am trying to do is put the Give one card to the player and remove it from the deck into a separate procedure because I will be doing this several times during the game.

    This is the code I have so far Under the class BlackJack.

    Java Code:
    package black.jack;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.Scanner;
    
    /**
     *
     * @author Alfredo
     */
    public class BlackJack
    {
    
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args)
        {
            // TODO code application logic here
           /**
            * Create a deck of cards to give to the players.
            */
           ArrayList<Card> deck = new ArrayList<>();
           
           // Create a loop that will go iterate four times       
            for (int i = 1; i < 14; i++) 
            {
                // Create a new loop to add the single Cards to the ArrayList.            
                for (int j = 0; j < 4; j++) 
                {
                    Card c = new Card(i);
                    c.getValue(i);
                    deck.add(c);
                    System.out.println(c);
                }
            }      
            
            
           // Randomize the deck of cards
            Collections.shuffle(deck);       
            /**
             * Create one player and the dealer to start the game. 
             */              
            Player player1= new BlackJack().createPlayer(false);
            Player dealer= new BlackJack().createPlayer(true);
            
            
           /**
            * Start the game by giving  cards to the player.
            * and removing them from the deck. Loop twice because its
            * two cards at the beginning of the game.
            */    
            for (int i = 0; i < 2; i++) 
            {
                BlackJack().giveCard(player1,deck);  // I am getting an error here. I cannot access giveCard.
                
            }
    
            
            
            
        }
            
        
            /**
             * Procedure to give a card to the player/dealer and remove it
             * from the deck.
             * @param player    The player that's receiving the first card from the deck.
             * @param deck      The dealer that's getting the first card from the deck.
             */    
            public void giveCard(Player player, ArrayList<Card> deck)
            {
                player.hand.add(deck.get(0));
                deck.remove(0);
            }
    
        
             public  Player createPlayer(boolean dealer)
            {
                String sName= "";           
    
                if (dealer = false) 
                {
                    Scanner s = new Scanner(System.in);
    
                    //Prompt the user for a name for the 1st Player.            
                    do 
                    {
                        System.out.println("Plese enter the name of Player");            
                        sName= s.nextLine();
                    } while (sName.length()==0);          
                }else{
                    ArrayList<String> names= new ArrayList<>(Arrays.asList("Dealer (Nick)",
                                                                           "Dealer (Sam)",
                                                                           "Dealer (Jeniffer)",
                                                                           "Dealer (Sandra)"));
                    Collections.shuffle(names);
                    sName= names.get(0);
                }
                // Create the player
                 Player p = new Player(sName, 0, null);         
                // Return the player
                 return p;
              }
    }
    Thanks a lot for the help. :) All suggestions and help is more than welcome.
    Last edited by fredlo2010; 02-09-2014 at 07:44 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Access a method of a class from Main.

    Give one card to the player and remove it from the deck into a separate procedure
    What problems are you having doing that?
    I'd say it in the other order: remove card from deck and give that card to a player.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    fredlo2010 is offline Member
    Join Date
    Jan 2014
    Posts
    17
    Rep Power
    0

    Default Re: Access a method of a class from Main.

    Thanks for the prompt reply Norm

    I am getting an error "Cannot find symbol" In this line of code:

    Java Code:
            for (int i = 0; i < 2; i++) 
            {
                BlackJack().giveCard(player1,deck);  // I am getting an error here. I cannot access giveCard.
                
            }
    If I change it to something like below, I get an non static variable cannot be referenced from a static context.

    Java Code:
            for (int i = 0; i < 2; i++) 
            {
                BlackJack.giveCard(player1,deck);  // I am getting an error here. I cannot access giveCard.
                
            }
    This is the method inside the BlackJack class

    Java Code:
    /**
             * Procedure to give a card to the player/dealer and remove it
             * from the deck.
             * @param player    The player that's receiving the first card from the deck.
             * @param deck      The dealer that's getting the first card from the deck.
             */    
            public void giveCard(Player player, ArrayList<Card> deck)
            {
                player.hand.add(deck.get(0));
                deck.remove(0);
            }
    Thanks

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Access a method of a class from Main.

    I am getting an error "Cannot find symbol"
    What symbol?
    Please copy the full text of the error message and paste it here. It has important info about the error.

    If you move all the code from the main() method to the class's constructor, there won't be a problem with static.
    Otherwise everything needs to be defined as static for the methods to access each other and the data.
    Last edited by Norm; 02-09-2014 at 08:14 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    fredlo2010 is offline Member
    Join Date
    Jan 2014
    Posts
    17
    Rep Power
    0

    Default Re: Access a method of a class from Main.

    Hi,

    I am sorry for the ambiguity.

    If I have the line as this:

    Java Code:
            for (int i = 0; i < 2; i++) 
            {
                BlackJack().giveCard(player1,deck);  // I am getting an error here. I cannot access giveCard.
                
            }
    I get the error:
    "cannot find symbol
    symbol: method BlackJack()
    location: class BlackJack

    But it works fine for a line calling a similar method:
    Java Code:
            Player player1= new BlackJack().createPlayer(false);
    Access a method of a class from Main.-error1.png


    If I change the line to something like this:

    Java Code:
            for (int i = 0; i < 2; i++) 
            {
                BlackJack.giveCard(player1,deck);  // I am getting an error here. I cannot access giveCard.
                
            }
    I get the error:
    non-static method giveCard(Player, ArrayList<Card>) cannot be referenced from a static context.

    Access a method of a class from Main.-error-2.png

    I have a attached a zip with the full program.

    Thanks a lot for the help.
    Attached Files Attached Files

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Access a method of a class from Main.

    Did you try moving all the code out of the main() to the constructor and having only code in main() be the new statement to create an instance of BlackJack?
    Java Code:
    BlackJack().giveCard(player1,deck);
    That statement says to use the value (an object reference) returned by the BlackJack() method to call the giveCard() method.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    fredlo2010 is offline Member
    Join Date
    Jan 2014
    Posts
    17
    Rep Power
    0

    Default Re: Access a method of a class from Main.

    No,

    But I am not sure how to do this. I will have to research on it.
    gimbal2 likes this.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Access a method of a class from Main.

    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: 01-08-2014, 06:09 PM
  2. Re: executing a method in main class
    By TauJVProgramer in forum New To Java
    Replies: 7
    Last Post: 05-30-2012, 06:26 PM
  3. access method of main class (android)
    By Stamoulohta in forum New To Java
    Replies: 8
    Last Post: 03-21-2011, 01:42 PM
  4. Running main method class from another main class
    By tlrocketman in forum New To Java
    Replies: 3
    Last Post: 12-06-2010, 09:30 AM
  5. why i cant access a method in a sub class?
    By javanew in forum New To Java
    Replies: 7
    Last Post: 05-03-2010, 07:29 PM

Posting Permissions

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