• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ArrayList for a deck of cards

 
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there, I am having a little bit of trouble doing this code. I finished main and wrote the outline of what we're supposed to do but I'm not sure how to do any of the other methods. Please, someone help me.
 
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Beware of calling a method with public access from he constructor. Methods called from the constructor must be final or private, but don't need to be both. In this case, you have a public method which can ne called from elsewhere, which will alter the state of the pack of cards object (reset it to it original state). And I am quite sure that isn't what you want. Make that method private. Initialise the List from that method or in the constructor, I think, rather than at declaration.

I think your question is being answered in your other thread, so I think these two threads should be merged.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there, I just started learning about ArrayLists and I am trying to implement a deck of cards. The instructions are:
/*Pre-condition: none
  *Post-condition: A new ArrayList of cards will be created with 52 cards.  
  *There will be 4 suits with 13 cards each.
  */
This is what I have so far:

I don't know if I should add more code or not.
 
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pre- and post-conditions are usually present on methods. Also, according to the post-condition you need to return an ArrayList. So instead of writing a constructor, write a static factory method that returns an ArrayList.

Primitives and Strings are very poor data types to use to represent abstract concepts. Don't use an int to represent a playing card. Don't use a String to represent a suit or a rank. Instead, write Rank, Suit and Card classes. Rank and Suit can be enums, and Card can consist of a Rank and a Suit. Your static factory method will then return a List<Card>.

Finally, when using arrays, place the brackets with the element type name, not the name of the variable: "Type[] name", not "Type name[]".
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apart from the fact that you have the faces out of order, with the Ace below the Deuce, have a look what the Java® Language Specification (=JLS) has to say about cards. Go through this section.
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Apart from the fact that you have the faces out of order, with the Ace below the Deuce


There is no standard order that says that the Ace comes before the Deuce, or after the King.

For a Rank enum, I've always ordered the Ace before the Deuce, because that's what Ace means, and it's also not logical to start a sequence with the number 2. That the Ace has a higher value than the King in many games, is a game rule, not an inherent property of the Ace.

Something that IS wrong, is that the OP wrote "Joker" instead of "Jack".
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:. . . There is no standard order that says that the Ace comes before the Deuce, or after the King.

All right. But does a card have any value at all, apart from a game rule?

. . . the OP wrote "Joker" instead of "Jack".

Didn't notice that.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A new ArrayList of cards will be created with 52 cards.  There will be 4 suits with 13 cards each. This is what I have so far, I don't know what to fill in the for-loops.

 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please help I don't know what to put in the second for-loop or if I'm doing this right. If you have an idea please post the code.
 
Bartender
Posts: 206
14
Mac OS X IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, where is your ArrayList which you want to populate?

Also, are cards supposed to be just strings or do you need to create a class called Card?

 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't start new topics for what is obviously a continuation of the old topic; I have merged your topics again.
Please tell us what the logic error in line 11 is, and also why that error is cancelled out.
Please show us the constructor for your Card class. If you want 13×4 cards, surely you can work out how create those discrete card objects. Show us how you would create one card object. I have heard a song about “Jack of Diamonds”; please show us how to create a card object representing ♦J. Once you can create ♦J, you should be able to create the other 51 cards.

For anybody who finds red text hard to read: that bit is ♦J but in red.
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the logic error that I have two of the exact same lines and I should delete it?
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you show me how to code it? I'm not sure how to do it, that's why I came to this website.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your code above this line (30) is commented out:


Is there a reason for that?
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ana Yo wrote:Can you show me how to code it? I'm not sure how to do it, that's why I came to this website.


We can help you to get to the solution yourself, but we don't give out complete answers. We are NotACodeMill and you must ShowSomeEffort.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ana Yo wrote:Is the logic error that I have two of the exact same lines and I should delete it?

Yes, and no. You are declaring the same thing twice. Haven't they told you about declaring the same thing twice? In the second instance it is a local variable which goes out of scope before you ever do anything with it. I would make the changes shown below:-

public class StandardDeck
{
   
     ArrayList<Card> deck = new ArrayList<Card>(52);
   
      /*Pre-condition: none
       *Post-condition: An ArrayList of objects of type Card will be created
      */
     public StandardDeck()
     {
       ArrayList<Card> deck = new ArrayList<Card>(52);

         
        buildDeck();
     }
// ...

I would also check the formatting; the excess empty lines make the code harder to read. I would also declare deck as List<Card>.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since part of the comment (which I assume is the requirements) for the buildDeck method is:
"Post-condition: A new ArrayList of cards will be created with 52 cards."

I would argue that the place to be initialising the instance of deck is in buildDeck and not the constructor.
Especially since a second call to buildDeck would (as it stands) result in the deck holding 104 cards.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:. . . part of the comment (which I assume is the requirements) for the buildDeck method . . .

Thank you; I hadn't noticed that. In which case I would have to change the constructor to read:-. . . and buildDeck() has to change its return type.
 
Ranch Hand
Posts: 167
1
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ana Yo wrote:Hi there, I just started learning about ArrayLists and I am trying to implement a deck of cards. The instructions are:
/*Pre-condition: none
  *Post-condition: A new ArrayList of cards will be created with 52 cards.  
  *There will be 4 suits with 13 cards each.
  */
This is what I have so far:

I don't know if I should add more code or not.










Output:
Ace of spades
Two of spades
Three of spades
Four of spades
Five of spades
Six of spades
Seven of spades
Eight of spades
Nine of spades
Ten of spades
Jack of spades
Queen of spades
King of spades
Ace of hearts
Two of hearts
Three of hearts
Four of hearts
Five of hearts
Six of hearts
Seven of hearts
Eight of hearts
Nine of hearts
Ten of hearts
Jack of hearts
Queen of hearts
King of hearts
Ace of clubs
Two of clubs
Three of clubs
Four of clubs
Five of clubs
Six of clubs
Seven of clubs
Eight of clubs
Nine of clubs
Ten of clubs
Jack of clubs
Queen of clubs
King of clubs
Ace of diamonds
Two of diamonds
Three of diamonds
Four of diamonds
Five of diamonds
Six of diamonds
Seven of diamonds
Eight of diamonds
Nine of diamonds
Ten of diamonds
Jack of diamonds
Queen of diamonds
King of diamonds
52 Cards
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure that will work, because OP is required to produce a List<Card>. I don't know why she said anything about a List<Integer>.
 
Michael D Sims
Ranch Hand
Posts: 167
1
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I am not sure that will work, because OP is required to produce a List<Card>. I don't know why she said anything about a List<Integer>.



OOPS...
 
Michael D Sims
Ranch Hand
Posts: 167
1
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator






52 Cards
Ace of spades
Two of spades
Three of spades
Four of spades
Five of spades
Six of spades
Seven of spades
Eight of spades
Nine of spades
Ten of spades
Jack of spades
Queen of spades
King of spades
Ace of hearts
Two of hearts
Three of hearts
Four of hearts
Five of hearts
Six of hearts
Seven of hearts
Eight of hearts
Nine of hearts
Ten of hearts
Jack of hearts
Queen of hearts
King of hearts
Ace of clubs
Two of clubs
Three of clubs
Four of clubs
Five of clubs
Six of clubs
Seven of clubs
Eight of clubs
Nine of clubs
Ten of clubs
Jack of clubs
Queen of clubs
King of clubs
Ace of diamonds
Two of diamonds
Three of diamonds
Four of diamonds
Five of diamonds
Six of diamonds
Seven of diamonds
Eight of diamonds
Nine of diamonds
Ten of diamonds
Jack of diamonds
Queen of diamonds
King of diamonds
 
Michael D Sims
Ranch Hand
Posts: 167
1
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The summonCards() method in the Cards class should have been deleted . ... my bad.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael D Sims wrote:. . . . ... my bad.

Don't worry about it.

Beware of coloured text, please; some people can only read certain colours.
reply
    Bookmark Topic Watch Topic
  • New Topic