Wednesday, 28 September 2011

Java: Enumerated Types ( The Deck )

Alright mateys, we have our card representation (No images, but lets...avoid those for now. We`re just working with basics!), Well, we can now work on our representation of a deck of these cards. The construction isn`t exactly straightforward, let alone the rest of the class, so let`s do it in parts!

Please remember, that normally, you`ll want to leave some kind of commentary in your code, I don`t just because I explain everything after the screenie, and to save space in the image, which is often over-sized.

Okay, let`s get started, line by line!

1: Declaration of the class, simple enough

3: We make this public so it can be accessed, final because there is no reason it should change and static so it can be accessed everywhere! The name is all caps, slightly out of convention just to denote its importance. Not necessary, up to the programmer.

5: Since the Card class was part of the same project in Netbeans (Or whatever IDE you use), it can be freely accessed from this class. We've got the instance initialization going on, too!

6: Counter for location in the pack!

7:  Each enumeration has this .values() function. This is the documentation for it:
:Returns an array containing the constants of this enum type, in the order they are declared. 
Simple enough to understand, it just gives back an array of all the elements in the enumeration, essentially. So this line is saying: For every suit in the list of suits (Remember, this is a foreach loop, it doesn`t require its own counter to iterate over everything), do the following:

8:  This line, similar to the last, iterates over every potential value in the list of values. Together, 7 & 8 read: "For each possible suit, do the following: For each possible value, do the following:...", or, more simply: "For each value in each suit..."

9: We are finally doing something! 9 is telling the program to actually add a card to the pack, based on which iteration its at. "For each value, in each suit, make a card" is the short form of these 4 lines.

10: This just increments our pack location to put the next card in.

 Okay, so now we have a constructor. What else do we need? In the interest of saving space, I'm compressed a lot of it, but we'll go through line by line so you don't miss anything!

18: This creates a random object. Used to generate random integers and booleans.

19-25: This is just a shuffle function. For every element in the pack, a random number is generated, then that position of card is flipped with the current position of card.

26: This is just a standard get() function.

27-33: StringBuilder is a new class for us, its part of Javas Built-In classes. It just acts as a simple way to append each card to a single string. That string is then returned, representing a shuffled deck.

You can then test this out on your own in a main function! First, create an object, print it out. You should get a list of all the cards. At this point, you can shuffle the deck and print it out again, seeing how it works.
This has been immensely long, and we aren't even done yet. Guess that gives me more time to ask you guys to give me any questions you want answered for the summary I'll be doing once this is over!

Oh well, that's it for tonight.Questions welcome! Comment, follow, subscribe, share etc, and see you tomorrow!

    And as part of a shameless plug for a friend, if you're interested in classic movies/books/music, visit his site here (fixed), and feel free to throw loads of criticism at us. 


  1. I will try this if i decide to take on Java one day.

  2. The comment tip is really good, I've started leaving comments on what stuff does what and it's really helping me remember, I'll copy-paste this for keeping, if you don't mind :).

  3. @neatfit: go crazy, save the pics and all. The more I can help out the better :)

  4. Will show this to my friend. Could come in handy for his midterm.

  5. Very nice job , thanks for sharing.