ArrayList for a deck of cards

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.

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: 62881

203

posted 1 month ago

I merged your stuff with the following thread. I hope that is okay by you.

Ana Yo

Ranch Hand

Posts: 108

posted 1 month ago

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:

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: 62881

203

posted 1 month ago

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: 9710

192

posted 1 month ago

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: 62881

203

posted 1 month ago

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: 62881

203

posted 1 month ago

I merged your stuff with the following thread. I hope that is okay by you.

Ana Yo

Ranch Hand

Posts: 108

posted 1 month ago

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 Yo

Ranch Hand

Posts: 108

posted 1 month ago

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.

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

Ana Yo

Ranch Hand

Posts: 108

posted 1 month ago

Campbell Ritchie

Marshal

Posts: 62881

203

posted 1 month ago

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 &diamondsuit;J. Once you can create &diamondsuit;J, you should be able to create the other 51 cards.

For anybody who finds red text hard to read: that bit is &diamondsuit;J but in red.

Ana Yo

Ranch Hand

Posts: 108

posted 1 month ago

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

Ana Yo

Ranch Hand

Posts: 108

posted 1 month ago

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

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: 62881

203

posted 1 month ago

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

Master Rancher

Posts: 3887

45

posted 1 month ago

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: 62881

203

posted 1 month ago

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.

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

Michael Sims

Campbell Ritchie

Marshal

Posts: 62881

203

posted 1 month ago

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>.

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