• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java deck of cards and dealing five cards  RSS feed

 
Scott Dunning
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


Card Class



DeckOfCards Class


 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scott Dunning wrote:
I'm also having trouble creating the method to deal out five cards...

First, think of the act of dealing real cards. From where in the deck are the cards drawn, and which indexes of your deckOfCards array do they correspond to?
Scott Dunning wrote:
...that also tells how many cards are left in the deck...

Whenever a card is dealt, you could indicate that the deck array index no longer points to a Card object. Then, you could count the cards in the deck by checking how many indexes of the array actually point to a Card.
Scott Dunning wrote:
...and I believe I need a toString method but I honestly do not know how to go about that.

It seems as if your Card class already has a toString method, which would appear to be useful in the reporting of dealt cards.
 
Scott Dunning
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel Christophel wrote:
Scott Dunning wrote:
I'm also having trouble creating the method to deal out five cards...

First, think of the act of dealing real cards. From where in the deck are the cards drawn, and which indexes of your deckOfCards array do they correspond to?
Scott Dunning wrote:
...that also tells how many cards are left in the deck...

Whenever a card is dealt, you could indicate that the deck array index no longer points to a Card object. Then, you could count the cards in the deck by checking how many indexes of the array actually point to a Card.
Scott Dunning wrote:
...and I believe I need a toString method but I honestly do not know how to go about that.

It seems as if your Card class already has a toString method, which would appear to be useful in the reporting of dealt cards.


I got it! Well most of it. It's print 5 random cards every time now. I just need to figure out how and where to tell how many cards are left in the deck. Any ideas?

Also, how bad does my code look? I got a little bit of advice but I'd imagine it's still a mess. Any tips on cleaning it up?

CardsGame class



ALSO, in my card class below. I'm getting an error that in the method suitStr (line 28) that "The static field card.suit should be accessed in a static way" It doesn't make sense to me though.
Card class


Also, java now has a shuffle that will "shuffle" an array or list rather than using Math.random? If so how can I switch my code out to implement it that way? I guess it's no big deal to take to random "cards" from the deck and move them over and over. I'd imagine it may come in handy down the road though.
DeckOfCard Class...




Thanks again for any help!!



 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although the compiler will permit you to call a static member by objectName.member it is poor style and error‑prone in cases of polymorphism. The correct style is ClassName.member.

There is also something wrong about using numbers to represent enumerated types. you should use an enum, which you can read about in the Java Language Specification (=JLS). There is also a section in the Java Tutorials, but in this instance I recommend you persevere with the JLS section, however difficult it seems to be to read.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scott Dunning wrote:
I just need to figure out how and where to tell how many cards are left in the deck. Any ideas?

If I were doing this, I would use an ArrayList instead of an array. This is because you can actually remove indexes from an ArrayList. The size of an ArrayList isn't constant like that of an array. So when dealing a card, that card's index can simply be removed from the ArrayList. To get the number of cards in the deck, you would just call ArrayList's size() method. However, this requires you to do a little research on how to use ArrayList.

But maybe you don't want to use something you haven't learned in your course yet. You can also accomplish this with an array, but you'll have to make your own counting method and call it from deal(). But then, how do we count the cards without knowing whether a given card is in the deck or not? Well after "dealing" a card, you can set that card's index of the array to null, indicating that the card is no longer in the deck. And I'll let you figure out how you'd then count the cards from there.

Along with that, I'd advise a slightly different way of choosing which card to deal. What you're doing now is incrementing currentCard each time you deal a card. When you deal cards in real life, you just pick the card on the top of the deck. And it makes the most sense to do this in your program too. If you insist on using an array, you can just skip over any nulls at the beginning of the array until you get to a real card. Once again, ArrayList would make things easier. You could always use the card at the first index because the indexes for already-dealt cards no longer exist. You may be thinking, "What's the problem with how it is now?" Let's say your still using an array and you've dealt 50 cards and then shuffled the deck. The value of currentCard will be 49, and the last two cards will most likely be moved to indexes before 49. Once currentCard reaches 52 and you "run out of cards," there will still be two cards left.
Scott Dunning wrote:
Also, how bad does my code look? I got a little bit of advice but I'd imagine it's still a mess. Any tips on cleaning it up?

You're code isn't bad at all but could be touched up a bit. Here are some things:
  • Regular variables should follow thisConvention, and final variables should follow THIS_CONVENTION. So your Suit and Rank array variables should be all caps.
  • As Campbell mentioned, you should be using enums for suit and rank.
  • Return statements don't need parenthesis.
  • Generally, when you have a method returning an object's attribute (e.g. your suit() method), it is preceded with "get" (e.g. getSuit()).
  • Repetitive code should be avoided, like when you're dealing your cards in the main method. To remedy this, you could use a loop in your deal method with an integer parameter specifying how many cards to deal.
  • Whenever a chunk of code has a specific function and can be named, it should be made into a method. Also, constructors should be pretty clutter-free. So the code in your DeckOfCards constructor that populates your array/ArrayList should be put into its own method.
  • Don't use the keyword this unless you have to. Most of the time, it can be inferred.
  • Even with singe-statement if-statements and loops, you should always use parenthesis. Otherwise, you'll prone to a lot of stupid mistakes.


  • I hope I've been of some assistance. If you have any questions at all, feel free to bring them up.
     
    Campbell Ritchie
    Marshal
    Posts: 56534
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Joel Christophel wrote: . . . final variables should follow THIS_CONVENTION. . . .
    You mean constants follow THAT_CONVENTION. A final variable might not be a constant because it might point to a mutable reference type, so that is inconstant.
    As you say however, the members of enums are officially called enum constants, so their names are usually all CAPITAL_LETTERS.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!