This week's giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Production-Ready Serverless (Operational Best Practices) and have Yan Cui on-line!
See this thread for details.
Win a copy of Production-Ready Serverless (Operational Best Practices) this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

ArrayList for a deck of cards  RSS feed

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • 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: 63781
209
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 63781
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Ana Yo
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • 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: 9986
206
  • Mark post as helpful
  • send pies
  • 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: 63781
209
  • Mark post as helpful
  • send pies
  • 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: 9986
206
  • Mark post as helpful
  • send pies
  • 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: 63781
209
  • Mark post as helpful
  • send pies
  • 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: 63781
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Ana Yo
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • 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 Yo
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • 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.
 
Ranch Hand
Posts: 57
2
  • Mark post as helpful
  • send pies
  • 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 Yo
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Campbell Ritchie
Marshal
Posts: 63781
209
  • Mark post as helpful
  • send pies
  • 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 Yo
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the logic error that I have two of the exact same lines and I should delete it?
 
Ana Yo
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • 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: 3983
47
  • Mark post as helpful
  • send pies
  • 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: 9986
206
  • Mark post as helpful
  • send pies
  • 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: 63781
209
  • Mark post as helpful
  • send pies
  • 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: 3983
47
  • Mark post as helpful
  • send pies
  • 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: 63781
209
  • Mark post as helpful
  • send pies
  • 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: 162
1
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • 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: 63781
209
  • Mark post as helpful
  • send pies
  • 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: 162
1
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • 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: 162
1
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • 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: 162
1
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The summonCards() method in the Cards class should have been deleted . ... my bad.
 
Campbell Ritchie
Marshal
Posts: 63781
209
  • Mark post as helpful
  • send pies
  • 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.
 
It's fun to be me, and still legal in 9 states! Wanna see my tiny ad?
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!