wood burning stoves*
The moose likes Beginning Java and the fly likes Accessor methods on enum Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Accessor methods on enum" Watch "Accessor methods on enum" New topic
Author

Accessor methods on enum

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

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


Jon
Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
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
Sheriff

Joined: Sep 28, 2004
Posts: 18532
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

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

Joined: Apr 06, 2010
Posts: 4343
    
    8

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

Joined: Apr 25, 2008
Posts: 660

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

Joined: Apr 06, 2010
Posts: 4343
    
    8

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

Joined: Nov 18, 2008
Posts: 433
    
    2
Jon Camilleri wrote:
My code includes enums for ease of reference


Where?

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

Hauke Ingmar Schmidt wrote:
Jon Camilleri wrote:
My code includes enums for ease of reference


Where?



Scroll up..
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
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

Joined: Apr 30, 2005
Posts: 164
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessor methods on enum
 
Similar Threads
Compare an Object to an Enum list
Logging problem and grammatical problems
Need Help in java
class question
cannot read a Collection of a custom class as a parameter