Results 1 to 12 of 12
  1. #1
    Exothesis is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default Help with classes

    I'm quite new to Java and I have to write a class that works with two previously written files (one class by me, one by my lecturers).

    Here is the code written by my lecturers:

    Java Code:
    import java.util.Scanner;
    
    public class CardGuessingGame
    {
        private static Player house;
        private static Player player;
    
        private static int wins;
        private static int loses;
    
        private static Scanner scan;
    
        /**
         * Initialise the game state.
         */
        public static void init()
        {
            house = new Player("House");
            player = new Player("Player");
            wins = 0;
            loses = 0;
            scan = new Scanner(System.in);
        }
    
        /**
         * Used to simulate playing our game.
         */
        public static void playGame()
        {
            char option, playAgain;
            int houseHandStrength, playerHandStrength;
    
            System.out.println("Welcome to our card guess 1.0 game!");
            System.out.println();
    
            do {
                // Deal cards to the house and player.
                house.acceptDeal(new Card(), new Card());
                player.acceptDeal(new Card(), new Card());
    
                System.out.println(house);
    
                // Determine whether the player wants to play this hand.
                do {
                    System.out.print("Deal cards? (Y/N) ");
                    option = Character.toLowerCase(scan.next().charAt(0));
                }
                while (option != 'n' && option != 'y');
    
                if (option == 'y')
                {
                    System.out.println(player);
    
                    // Display hand strength of both players.
                    houseHandStrength = house.getHandStrength();
                    playerHandStrength = player.getHandStrength();
                    System.out.println("The dealer's hand strength is: " + houseHandStrength);
                    System.out.println("Your hand strength is: " + playerHandStrength);
                    System.out.println();
    
                    // If the player has a stronger hand.
                    if (player.getHandStrength() > house.getHandStrength())
                    {
                        System.out.println("** You won the hand! **");
                        wins++;
                    }
                    else {
                        System.out.println("The house wins this round!");
                        loses++;
                    }
                }
    
                // Display the win/lose statistics.
                System.out.println("Current wins: " + wins);
                System.out.println("Current loses: " + loses);
    
                // Prompt whether the user wants to play again.
                do {
                    System.out.print("Would you like to play again? (Y/N) ");
                    playAgain = Character.toLowerCase(scan.next().charAt(0));
                }
                while (playAgain != 'n' && playAgain != 'y');
    
                System.out.println();
                System.out.println("*******************************************************");
            }
            while (playAgain == 'y');
    
            System.out.println();
            System.out.println("Thank you for playing!");
        }
    
        public static void main(String[] args)
        {
            init();
            playGame();
        }
    }
    Here is a class written previously by myself:

    Java Code:
    public class Card
    {
        int value;
        String suit;
        int suitValue;
        String valueName;
        String cardName;
    
        public Card()
        {
            value = (int)(Math.random() * 13) + 1;
            suitValue = (int)(Math.random() * 4) + 1;
    
            getSuit();
            getValue();
            getValueName();
            toString();
        }
    
        public String getSuit()
        {
            
        //Selects a suit using a random int
        
            if(suitValue == 1)
                suit = "Spades";
    
            if(suitValue == 2)
                suit = "Clubs";
    
            if(suitValue == 3)
                suit = "Hearts";
    
            if(suitValue == 4)
                suit = "Diamonds";
    
            return suit;
        }
    
        public int getValue()
        {
            return value;
        }
    
        public String getValueName()
        {
            if(value == 1)
                valueName = "Ace";
            else
                if(value == 11)
                    valueName = "Jack";
                else
                    if(value == 12)
                        valueName = "Queen";
                        else
                            if(value == 13)
                                valueName = "King";
    
            return valueName;
        }
    
        public String toString()
        {
            if(value == 1)
                cardName = ("The " + valueName + " of " + suit);
            else
                if(value == 11)
                    cardName = ("The " + valueName + " of " + suit);
                else
                    if(value == 12)
                        cardName = ("The " + valueName + " of " + suit);
                    else
                        if(value == 13)
                            cardName = ("The " + valueName + " of " + suit);
                        else
                            cardName = ("The " + value + " of " + suit);
    
            return cardName;
        }
    }
    And here is what I've written for this new piece of code:

    Java Code:
    public class Player
    {
        private Card card1;
        private Card card2;
        private int handStrength;
    
        public String Player(String name)
        {
            return name;
        }
    
        public Card acceptDeal(Card cardOne,Card cardTwo)
        {
            card1 = cardOne;
            card2 = cardTwo;
    
            return cardOne;
            return cardTwo;
        }
    
        public int getHandStrength()
        {
            handStrength = (card1.getValue + card2.getValue);
    
            return handStrength;
        }
    }
    I use JCreator LE to compile the program, and I get error messages saying "cannot find symbol constructor Player(java.lang.String)" as part of the CardGuessingGame.java, and "cannot find symbol variable getValue" for the Player class under the getHandStrength method.

    I have no idea what I am doing wrong, can someone please give me a pointer in the right direction? Thank you in advance.

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    Java Code:
    public String Player(String name)
        {
            return name;
        }
    currently what you probably mean to be the constructor is a method.
    constructors don't have a return type right.

    so perhaps instead of this, change the player signature to be that of a constructor, and create a getName() method to return name separately
    Java Code:
      private String name;
      public Player(String name)
        {
            this.name = name;
        }
    
      public String getName() {
        return this.name;
      }

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

    Default

    Can you post the full text of the error messages?
    Your edited verions left off some valuable info, like showing the line with the error.

    Is getValue a variable or a method? The name would look like a method.
    Mehtod calls end with ()

  4. #4
    Exothesis is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    Errors for the CardGuessingGame.java:

    cannot find symbol constructor Player(java.lang.String) - Line 18 and 19

    I totally forgot about putting the () after a method call, so now that I've changed that, the only error I get for the Player.java is:

    unreachable statement - Line 19

    I get no errors for the Card.java

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

    Default

    Where is the unreachable statement? What file is it in?
    Is it after a return statement?

  6. #6
    Exothesis is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Where is the unreachable statement? What file is it in?
    Is it after a return statement?
    Java Code:
    public class Player
    {
        private String name;
        private Card card1;
        private Card card2;
        private int handStrength;
    
        public String Player(String name)
        {
            return name;
        }
    
        public Card acceptDeal(Card cardOne,Card cardTwo)
        {
            card1 = cardOne;
            card2 = cardTwo;
    
            return cardOne;
            [COLOR="Red"]return cardTwo;[/COLOR] //unreachable statement here
        }
    
        public int getHandStrength()
        {
            handStrength = (card1.getValue() + card2.getValue());
    
            return handStrength;
        }
    }

  7. #7
    Exothesis is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    I should also mention that I shouldn't have to modify the CardGuessingGame.java file in any way for it to work, if it doesn't work, the problem lies with the Player.java file.

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

    Default

    Can you explain when the second return would be executed?
    When the first return is executed, that's the end of execution of statements in that method.

  9. #9
    Exothesis is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    So how would I be able to return both?

    EDIT: I changed the acceptDeal to type void, I don't have to return anything. I still have the two errors with the CardGuessingGame.java file
    Last edited by Exothesis; 10-11-2010 at 02:59 AM.

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

    Default

    Please explain the logic of the method.
    What does it do?
    and what is it supposed to return?

    It looks like it should be called setCards() because it only saves the two cards

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Exothesis View Post
    So how would I be able to return both?
    You can't and what's more, you have no need to try to do this here.

  12. #12
    Exothesis is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    Yeah I only just realised that, but I still have the cannot find symbol constructor error in the CardGuessingGame.java file.

Similar Threads

  1. gui classes
    By phantom135 in forum AWT / Swing
    Replies: 12
    Last Post: 08-05-2010, 06:28 PM
  2. Three classes help!!
    By arrech326 in forum New To Java
    Replies: 4
    Last Post: 11-24-2009, 07:34 AM
  3. Help with classes
    By gnarly hogie in forum New To Java
    Replies: 14
    Last Post: 10-10-2008, 03:29 PM
  4. Get name of available classes
    By escuja in forum CLDC and MIDP
    Replies: 0
    Last Post: 07-26-2008, 01:03 PM
  5. Using a JAR from other classes
    By Joe2003 in forum Advanced Java
    Replies: 1
    Last Post: 01-02-2008, 08:08 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
  •