Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessor methods on enum

 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to create a Poker game, starting off from the objects:



So I extended my imagination ...



4 suits of cards, 14 cards for each suit, however, I want the enum to be available globally within the application.
I'm also worried that by having enums declared within the Card class, and, within the Poker class - which in theory will call the card class as required since this is the "testing class" where my main method for calling the card class will reside, and, is the start of a spaghetti logic...

Further reading
1. Enum types at http://download.oracle.com/javase/tutorial/java/javaOO/enum.html.
2. Poker basics at http://en.wikipedia.org/wiki/Poker.
3. Card suits at http://en.wikipedia.org/wiki/Suit_(cards).
4. Encapsulation is described as "Under this definition, encapsulation means that the internal representation of an object is generally hidden from view outside of the object's definition. Typically, only the object's own methods can directly inspect or manipulate its fields.", as quoted on http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming).
 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First off the variable suit in line 8 does not exist so you can't compare the constant HEART to it.
The break in line 10 is unreachable since a return statement is called before it, you should let the switch case assign a value to a String variable and leave the return statement until you are out of the switch.

If you make the enum types public you can access them from anywhere by calling them through the Card class, like so: Card.suit.HEARTS.

Personally I usually give a class a number of public static constants:


And refer to it's suit and number with integers

 
Henry Wong
author
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//I can't call HEARTS because my interpreter does not recognize suit, although it's declared as a private enum


It doesn't recognize "suit", because it is expecting a variable for the switch statement. In this case, suit is a enum type, and not a variable. You need to declare a variable of that type to use.

You also have a ton of syntax errors too, that needs to be fixed.

Henry
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon Camilleri wrote:4 suits of cards, 14 cards for each suit, however, I want the enum to be available globally within the application.

So make the enum public, like Unnar suggested. Card.suit will then be the name for the enum. Although you should of course change it to Card.Suit (and Card.Number instead of Card.cardnumber).

Unnar Björnsson wrote:Personally I usually give a class a number of public static constants:


And refer to it's suit and number with integers


Which allows me to create a card with suite 13 and value 481, unless you check and throw an IllegalArgumentException. This example is a good application for enums, with a small fixed set of values.
 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, however, I needed to flag cards according to the suit and the cardnumber, so I coded as follows, after trying to initialize a boolean[] [] card unsuccessfully:





Further reading
Bug #7037278 on http://bugreport.sun.com/bugreport/submit_intro.do - not accessible.

 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a shame - because the enum approach is so much better than that. Shorter, clearer, and type safe (which that isn't).
 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:That's a shame - because the enum approach is so much better than that.


How do you mean?


My code includes enums for ease of reference, and, I cannot use a static variable since typically a deck of 52 cards must be unique - thus shared variables would not be useful for representing the card value, which means that a Deck will inherit the Card class, once I have this class working


 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You aren't using enums in that code - you're using shorts. Which means, as Rob pointed out, there's nothing stopping someone creating the 67 of Spades (or, indeed, the 67 of some new made up suit).

Usnig the boolean[][] is pretty strange as well, as it means that conceptually your Card can actually be more than one card at a time. And that massive if/else statement is completely unnecessary.

Try this. It probably doesn't match exactly what you're trying to do, but it's a starting point.

 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon Camilleri wrote:
My code includes enums for ease of reference


Where?

 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hauke Ingmar Schmidt wrote:
Jon Camilleri wrote:
My code includes enums for ease of reference


Where?



Scroll up..
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon Camilleri wrote:
Hauke Ingmar Schmidt wrote:
Jon Camilleri wrote:
My code includes enums for ease of reference


Where?


Scroll up..


I will just quote Matthew:

Matthew Brown wrote:You aren't using enums in that code - you're using shorts.
 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your cards are defined by two things, cardnumber and suit, it's name is therefore of the following format: "<cardnumber>of<suit>". The number and suit need to have a string representation. I'm not sure if you insist on using enums I don't use them much myself
Regardless you need to return a string which you could store in parts in an array or use enum, I'll show you the array version:

I don't know why on earth you would instantiate a card object of suit 13 and number 481 but, yes it's better to be safe than sorry so be sure that the constructor checks if the arguments are within reasonable bounds.

Now instead of your long block of if-else inside your getCard() method you can simply do:

You would ofcourse need to make constants for ease of reference, which is perhaps where this method falls short of enum.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic