![]() I'll leave it to you to think things through and try to understand why this is. ![]() Note: I said 0 to 12 even though the for loop says i=0 i<13 i++. So, this means that we'll ensure that we get every single combination of CardValue and Suit. The outer for loop is running from 0 to 12 and the inner loop is running from 0 to 3. As you scan through the Deck code you'll notice that I have two for loops and one is nested inside the other. So, when you keep that in mind, it helps to understand what I'm doing in this Deck code. It doesn't like to give you back an Array in any random order. Java is very particular about the way it does things. ![]() It's important to mention that it's in that exact order because that's the order in which they are defined in the Suit object. So if I were to invoke Suit.values(), I would get an array of values back like so: If you hover your mouse over the values() part of the code in your IDE, you'll see that when you invoke the values() method, you'll get an Array that represents every value inside the Enum. We do this by referring to the Enums in a static way like so: CardValue.values() So, we all know that there are 52 Cards in a Deck right? So this means that we should iterate through all of our CardValues and Suits in order to match them up with each other. The basic concept of this code is that we want to properly represent a Deck of Cards. (aCard.getCardValue() + " of " + aCard.getSuit()) Let's see what I mean with an example: package Ĭard card = new Card(value, Suit.values()) I said that this is because you shouldn't be able to instantiate the class, as the contained values should be referenced in a static fashion. Okay, so let's talk a bit more about what I meant when I said that the Enums have to have a package or private level constructor. But you can see how this makes sense right? A Card is simply made up of a CardValue and a Suit! Just like real life! Don't you just love Object Oriented Programming? Okay nothing new going on in that code, it's a standard Object that defines some private (encapsulated) variables that are made publicly visible via getter and setter methods. Public void setCardValue(CardValue cardValue) Public Card (CardValue cardValue, Suit suit) First, let's put the CardValue and Suit together into a Card object to pull everything together. If you don't quite understand what I mean when I say that the values should be referred to in a static way, just hang in there, I'll have an example soon. This is because you aren't meant to instantiate Enums, you're supposed to reference the values in a static way, as they are meant to be constant values. You can also get away with not defining a constructor at all (like I did with the Suit Enum). Things that you should note are that Enums need to have a package or private scoped constructor, if you try to specify anything else, Java will fail the compilation of your code. Voila! We've now outlined what a CardValue enum could look like, now let's define what the Card's Suit could look like: package Īlrighty, so we have now defined what a Suit and CardValue looks like as Enums in Java. Wouldn't it be nice if you could assign numeric values to all of the cards? Like so: For example, let's say you would like to make an application that allows you to play a card game, like poker. This becomes helpful when you would like to express something in more human readable terms. It's short for Enumerated Type, which just means that Java allows you to define your own special variable type. Enums is a neat topic that applies to most programming languages (not just Java)! Enums
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |