Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help with program that deals five cards from a deck

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with program that deals five cards from a deck

    Hello, I have an assignment for my summer class. FYI, I'm super new to programming in general and this is my first time with java and it's a summer class so it's going super fast (please be nice!) lol.

    Basically I started writing my code in one class, then I split it up into a Card class and a DeckOfCards class and I now need to figure out how to get it all to work together. I get a little confused with calling methods sometimes, especially when separate classes are in play. I think I just need a method to deal out . Besides getting it all working together correctly. I'm also having trouble creating the method to deal out five cards that also tells how many cards are left in the deck and I believe I need a toString method but I honestly do not know how to go about that. Any help is greatly appreciated! If you could help explain things too that would be awesome! I think I have everything SO FAR correct but I could be wrong and I'm sure there are better ways to write the code, I'll take any suggestions for a cleaner look too. FYI, I think the prof would rather arrays then enums since we're dealing with arrays right now hence the array for the 52 card deck.

    Here are the directions...
    Design and implement a class called Card that represents a standard playing card. Each card has a suit and a face value. Then create a class called DeckOfCards that stores 52 objects of the Card class. Include methods to shuffle the deck, deal a card and report the number of cards left in the deck. The shuffle methods should assume a full deck. Create a driver class (CardsGame) with a main method that deals five cards from the shuffled deck, printing each card as it is dealt. Make sure to write the appropriate constructors, getters, setters, toString and other methods as required for both classes.


    The main class, CardsGame Class
        import java.util.Scanner;
     
        public class CardsGame {
            public static void main (String [] args) {  
     
            DeckOfCards deck = new DeckOfCards();
            //call shuffle
            deck.shuffle();
     
           }
        }

    Card Class

        class Card {
           public static final int SPADE   = 4;
           public static final int HEART   = 3;
           public static final int CLUB    = 2;
           public static final int DIAMOND = 1;
     
           private int rank;
           private int suit;
           private static final String[] Suit = {"Hearts", "Clubs", "Spades", "Diamonds"};
           private static final String[] Rank = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};                     
     
           private int cardSuit;
           private int cardRank;
     
           public Card(int suit, int rank) {
              if (rank == 1)
                 cardRank = 14;     // Give Ace the rank 14
              else
                 cardRank = (int) rank;
                 cardSuit = (int) suit;
           }
     
           public int suit() {
              return this.cardSuit;         
           }
     
           public String suitStr() {
              return(this.Suit[ this.cardSuit ]);                                
           }
     
           public int rank() {
              return this.cardRank;
           }
     
           public String rankStr() {
              return ( Rank[ cardRank ] );
           }
     
     
           public String toString() {
              return ( Rank[ cardRank ] + Suit[ cardSuit ] );
           }
        }

    DeckOfCards Class

        class DeckOfCards {
           public static final int NEWCARDS = 52;
           private Card[] deckOfCards;         // Contains all 52 cards
           private int currentCard;            // deal THIS card in deck
     
        public DeckOfCards( ) {
          deckOfCards = new Card[NEWCARDS];
          int i = 0;
     
          for ( int suit = Card.DIAMOND; suit <= Card.SPADE; suit++ )
             for ( int rank = 1; rank <= 13; rank++ )
                 deckOfCards[i++] = new Card(suit, rank);
                 currentCard = 0;
         }
     
     
           //shuffle(n): shuffle the deck
           public void shuffle(int n) {
              int i, j, k;
              for ( k = 0; k < n; k++ ) {
    	          i = (int) ( NEWCARDS * Math.random() );  // Pick 2 random cards
    	          j = (int) ( NEWCARDS * Math.random() );  // in the deck?
     
    	  //swap these randomly picked cards
    	      Card temp = deckOfCards[i];
    	      deckOfCards[i] = deckOfCards[j];
    	      deckOfCards[j] = temp;
           }
          currentCard = 0;   // Reset current card to deal
          }
        }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with program that deals five cards from a deck

    Try to focus on one thing at a time. Posting a general "How am I doing?" topic is often ignored because it involves examining all of the code, grading it according to the requirements, and making largely subjective judgments about it that may or may not be appropriate. The key points are:

    Does it comply with the assignment's requirements?
    Does it work?
    Is it what the instructor wants to see?

    You should be able to answer the first and last (but you can ask our opinions), and we're most able to help when you get stuck on the middle item.

    In this case, in your driver class (CardsGame) you've created a DeckOfCards object, deck, and then called the shuffle() method on it:

    deck.shuffle();

    This statement is unexecutable due to the following error (you should post errors that you want help with like this):
    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    	The method shuffle(int) in the type DeckOfCards is not applicable for the arguments ()
     
    	at TestClass2.main(TestClass2.java:13)
    The error indicates that there is no corresponding DeckOfCards.shuffle() method that takes no arguments. To fix that bug, ask yourself, "Why does my shuffle() method take an int argument?" First, look at the requirements:

    "Include methods to shuffle the deck, deal a card and report the number of cards left in the deck. The shuffle methods should assume a full deck."

    If the shuffle() methods (why plural?) assumes a full deck, why does it need a parameter? What does the parameter define? You didn't add comments to your shuffle() method to describe why you decided the parameter 'n' was needed, so I can't answer those questions.

    Once you've decided whether your existing shuffle() method is correct, correct the method call or the method to eliminate the error.

    Hope this helps. What else?

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with program that deals five cards from a deck

    Also posted here.

Similar Threads

  1. Deck of cards
    By gavinqotsa92 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 20th, 2013, 10:57 AM
  2. [SOLVED] Shuffling a deck of cards
    By aterrorbite in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 5th, 2013, 07:44 PM
  3. Please help with unwrap, deck, shuffle cards
    By pots in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 6th, 2013, 12:46 PM
  4. Need help with Deck of Cards
    By yanksin1st in forum Java Theory & Questions
    Replies: 1
    Last Post: March 1st, 2012, 01:28 PM
  5. A deck of cards
    By glebovg in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 1st, 2012, 09:46 AM

Tags for this Thread