Results 1 to 7 of 7
  1. #1
    Hector is offline Member
    Join Date
    Feb 2014
    Location
    Seattle, Washington
    Posts
    9
    Rep Power
    0

    Default Deck of Cards Program

    I need help getting this program to run. I have red underlines under "Card" and "cards" in my Deck class and "deal" in my driver and i don't know how to fix them to get it to properly run. Any help would be nice. Thanks in advance!

    Card Class:
    Java Code:
    public class Card
    {
       public final static int ACE   = 1; // note: or use enumerated types
       public final static int TWO   = 2;
       public final static int THREE = 3;
       public final static int FOUR  = 4;
       public final static int FIVE  = 5;
       public final static int SIX   = 6;
       public final static int SEVEN = 7;
       public final static int EIGHT = 8;
       public final static int NINE  = 9;
       public final static int TEN   = 10;
       public final static int JACK  = 11;
       public final static int QUEEN = 12;
       public final static int KING  = 13;
    
       public final static int CLUBS    = 1;
       public final static int DIAMONDS = 2;
       public final static int HEARTS   = 3;
       public final static int SPADES   = 4;
    
       private final static int NUM_FACES = 13;
       private final static int NUM_SUITS = 4;
    
       private int face, suit;
       private String faceName, suitName;
    
       //-----------------------------------------------------------------
       //  Creates a random card.
       //-----------------------------------------------------------------
       public Card ()
       {
          face = (int) (Math.random() * NUM_FACES) + 1;
          setFaceName();
    
          suit = (int) (Math.random() * NUM_SUITS) + 1;
          setSuitName();
       }
    
       //-----------------------------------------------------------------
       //  Creates a card of the specified suit and face value.
       //-----------------------------------------------------------------
       public Card (int faceValue, int suitValue)
       {
          face = faceValue;
          setFaceName();
    
          suit = suitValue;
          setSuitName();
       }
    
       //-----------------------------------------------------------------
       //  Sets the string representation of the face using its stored
       //  numeric value.
       //-----------------------------------------------------------------
       private void setFaceName()
       {
          switch (face)
          {
             case ACE:
                faceName = "Ace";
                break;
             case TWO:
                faceName = "Two";
                break;
             case THREE:
                faceName = "Three";
                break;
             case FOUR:
                faceName = "Four";
                break;
             case FIVE:
                faceName = "Five";
                break;
             case SIX:
                faceName = "Six";
                break;
             case SEVEN:
                faceName = "Seven";
                break;
             case EIGHT:
                faceName = "Eight";
                break;
             case NINE:
                faceName = "Nine";
                break;
             case TEN:
                faceName = "Ten";
                break;
             case JACK:
                faceName = "Jack";
                break;
             case QUEEN:
                faceName = "Queen";
                break;
             case KING:
                faceName = "King";
                break;
          }
       }
    
       //-----------------------------------------------------------------
       //  Sets the string representation of the suit using its stored
       //  numeric value.
       //-----------------------------------------------------------------
       private void setSuitName()
       {
          switch (suit)
          {
             case CLUBS:
                suitName = "Clubs";
                break;
             case DIAMONDS:
                suitName = "Diamonds";
                break;
             case HEARTS:
                suitName = "Hearts";
                break;
             case SPADES:
                suitName = "Spades";
                break;
          }
       }
    
       //-----------------------------------------------------------------
       //  Determines if this card is higher than the passed card. The
       //  second parameter determines if aces should be considered high
       //  (beats a King) or low (lowest of all faces).  Uses the suit
       //  if both cards have the same face.
       //-----------------------------------------------------------------
       public boolean isHigherThan (Card card2, boolean aceHigh)
       {
          boolean result = false;
    
          if (face == card2.getFace())
          {
             if (suit > card2.getSuit())
                result = true;
          }
          else
          {
             if (aceHigh && face == ACE)
                result = true;
             else
                if (face > card2.getFace())
                   result = true;
          }
    
          return result;
       }
    
       //-----------------------------------------------------------------
       //  Determines if this card is higher than the passed card,
       //  assuming that aces should be considered high.
       //-----------------------------------------------------------------
       public boolean isHigherThan (Card card2)
       {
          return isHigherThan (card2, true);
       }
    
       //-----------------------------------------------------------------
       //  Returns the face (numeric value) of this card.
       //-----------------------------------------------------------------
       public int getFace ()
       {
          return face;
       }
    
       //-----------------------------------------------------------------
       //  Returns the suit (numeric value) of this card.
       //-----------------------------------------------------------------
       public int getSuit ()
       {
          return suit;
       }
    
       //-----------------------------------------------------------------
       //  Returns the face (string value) of this card.
       //-----------------------------------------------------------------
       public String getFaceName ()
       {
          return faceName;
       }
    
       //-----------------------------------------------------------------
       //  Returns the suit (string value) of this card.
       //-----------------------------------------------------------------
       public String getSuitName ()
       {
          return suitName;
       }
    
       //-----------------------------------------------------------------
       //  Returns the string representation of this card, including
       //  both face and suit.
       //-----------------------------------------------------------------
       public String toString ()
       {
          return faceName + " of " + suitName;
       }
    }
    Deck Class(this is where i have red underlines under "Card" and "cards"):
    Java Code:
    import java.util.Random;
    
    public class Deck
    {
       private int numCards;
       private Card[] cards;
       private int NUM_CARDS = 52;
    
       //-----------------------------------------------------------------
       //  Creates a deck, cards are created in order
       //-----------------------------------------------------------------
       public Deck()
       {
          numCards = NUM_CARDS;
          Card cards = new Card[numCards];
    
          //create the deck (cards created in order)
          int cardIndex = 0;
          for (int face = Card.ACE; face <= Card.KING; face++)
             for (int suit = Card.CLUBS; suit <= Card.SPADES; suit++)
                cards[cardIndex++] = new Card(face, suit);
       }
    
       //-----------------------------------------------------------------
       //  Deals a card from the deck
       //-----------------------------------------------------------------
       public Card deal()
       {
          if (numCards > 0)
             return cards[--numCards];
          else
             return null;
       }
    
       //-----------------------------------------------------------------
       //  Returns the number of cards left in the deck
       //-----------------------------------------------------------------
       public int getNumCardsInDeck()
       {
          return numCards;
       }
    
       //-----------------------------------------------------------------
       //  Returns true is the deck has cards in it, else false
       //-----------------------------------------------------------------
       public boolean hasMoreCards()
       {
          return (numCards > 0);
       }
    
       //-----------------------------------------------------------------
       //  Shuffles the deck.  Resets the number of cards in the deck to 52
       //-----------------------------------------------------------------
       public void shuffle()
       {
          Random gen = new Random();
    
          numCards = NUM_CARDS;
    
          boolean[] taken= new boolean[NUM_CARDS];
          for (int i=0; i<numCards; i++)
             taken[i] = false;
    
          int[] shufflePositions = new int[NUM_CARDS];
          int count = 0;
    
          // determine shuffled positions
          while (count < 52)
          {
             int place = gen.nextInt(NUM_CARDS);
             if (!taken[place])
             {
                shufflePositions[count] = place;
                taken[place] = true;
                count++;
             }
          }
    
          // move cards to shuffled positions
          Card[] temp = new Card[NUM_CARDS];
    
          for (int i=0; i< numCards; i++)
             temp[shufflePositions[i]] = cards[i];
    
          cards = temp;
       }
    }
    Deck Driver(red underline under "deal"):
    Java Code:
    public class DeckDriver {
    
    	public static void main(String[] args) {
    		Deck deck = new Deck();
    
    	      deck.shuffle();
    
    	      int cardNumber = 0;
    
    	      System.out.println("Dealing shuffled cards:");
    	      while (deck.hasMoreCards())
    	         System.out.println("   " + ++cardNumber + ": " + deck.deal());
    
    	   }
    
    	}

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,049
    Rep Power
    6

    Default Re: Deck of Cards Program

    Red lines indicate compilation problems. Whatever IDE you are using should be telling you what those errors are. They'll have to be fixed before anything will run.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Hector is offline Member
    Join Date
    Feb 2014
    Location
    Seattle, Washington
    Posts
    9
    Rep Power
    0

    Default Re: Deck of Cards Program

    Quote Originally Posted by gimbal2 View Post
    Red lines indicate compilation problems. Whatever IDE you are using should be telling you what those errors are. They'll have to be fixed before anything will run.
    if tells me that i need to make a card class, even though i already have. i guess it isnt recognizing it?

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,049
    Rep Power
    6

    Default Re: Deck of Cards Program

    You'll have to provide more information about the setup you have. What IDE are you using? What files do you have and where did you put them?

    And by the way: who's code is this? Clearly its not yours. Are you even a programmer (or want to be)?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Deck of Cards Program

    1. The problem (red line) is on this line of code:
    Java Code:
    Card cards = new Card[numCards];
    You're instantiating an array, but you're assigning it to cards, which is of type Card (not an array).

    2. The subsequent problem,
    Java Code:
    cards[cardIndex++] = new Card(face, suit);
    is caused by #1. Fix #1, and the red line for cards will go away.

    3. Finally the problem with deal in the DeckDriver class is most likely due to the Deck class not being compiled by your IDE due to #1 mentioned above.

    With most IDEs, you can mouse-over red lines to find out what the problem is. E.g., with Eclipse the squiggly line for #1 states, "Type mismatch: cannot convert from Card[] to Card." It should be obvious what you need to do based on the message. Familiarise yourself further with your IDE so that you not only see a red line, you also know what the problem is based on the error message represented by the red line.

    Also, if you don't already know how to compile your code via command line using javac, I'd suggest you learn to do it immediately. Compiling using javac will give you error messages similar to what you'll get from your IDE, and they're more obvious as they'll appear on the console right after running javac.

  6. #6
    Hector is offline Member
    Join Date
    Feb 2014
    Location
    Seattle, Washington
    Posts
    9
    Rep Power
    0

    Default Re: Deck of Cards Program

    Quote Originally Posted by gimbal2 View Post
    You'll have to provide more information about the setup you have. What IDE are you using? What files do you have and where did you put them?

    And by the way: who's code is this? Clearly its not yours. Are you even a programmer (or want to be)?
    i appreciate your concern, but the Card class is code that i have been given and told to make do with. i just need help. The "Are you even a programmer?" question is inconsiderate at the least and almost makes you seem oblivious to the entire point of this website. Why would i be on this website at all if i wasn't a programmer or attempting to program? i thought this website was for help and constructive criticism, but it seems to be on the contrary.

  7. #7
    Hector is offline Member
    Join Date
    Feb 2014
    Location
    Seattle, Washington
    Posts
    9
    Rep Power
    0

    Default Re: Deck of Cards Program

    Quote Originally Posted by jashburn View Post
    1. The problem (red line) is on this line of code:
    Java Code:
    Card cards = new Card[numCards];
    You're instantiating an array, but you're assigning it to cards, which is of type Card (not an array).

    2. The subsequent problem,
    Java Code:
    cards[cardIndex++] = new Card(face, suit);
    is caused by #1. Fix #1, and the red line for cards will go away.

    3. Finally the problem with deal in the DeckDriver class is most likely due to the Deck class not being compiled by your IDE due to #1 mentioned above.

    With most IDEs, you can mouse-over red lines to find out what the problem is. E.g., with Eclipse the squiggly line for #1 states, "Type mismatch: cannot convert from Card[] to Card." It should be obvious what you need to do based on the message. Familiarise yourself further with your IDE so that you not only see a red line, you also know what the problem is based on the error message represented by the red line.

    Also, if you don't already know how to compile your code via command line using javac, I'd suggest you learn to do it immediately. Compiling using javac will give you error messages similar to what you'll get from your IDE, and they're more obvious as they'll appear on the console right after running javac.
    Thanks!!

Similar Threads

  1. Deck,shuffle, unwrap cards
    By pots in forum New To Java
    Replies: 6
    Last Post: 02-06-2013, 06:47 PM
  2. Tryin to create a deck of cards
    By mrbrendano in forum New To Java
    Replies: 6
    Last Post: 08-03-2011, 05:32 PM
  3. Help with creating a deck of cards
    By Carouselification in forum New To Java
    Replies: 5
    Last Post: 03-06-2011, 04:45 AM
  4. Deck of cards problem
    By VelvetMirror in forum New To Java
    Replies: 2
    Last Post: 02-16-2011, 08:02 PM
  5. Deck of Cards
    By khunmato in forum New To Java
    Replies: 13
    Last Post: 09-06-2009, 05:47 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
  •