• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to use enum in Java?

 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone know how to use enum when you want to create a card game who have 1-9, king, queen, jack and ace. In additionally, 4 suit of cards who have 52 deck. any ideas how enum will work with that?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it sounds like you want two different enums and a class that has references to two of them. Let's start at the beginning. Do you know how to create an enum that has the 4 suits?
 
jin Otanashi
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:it sounds like you want two different enums and a class that has references to two of them. Let's start at the beginning. Do you know how to create an enum that has the f suits?


I have never heard of F suit and I think is like this

just frame of brain storm so far
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jin Otanashi wrote:


The constants of an enum must be valid names. Numbers are not valid names. Just like you can't name a class, variable or method "1" or "2", etc.

Do something like this:

Note: your two enums Value and CardType are not very useful. What happens if you combine them? You get cards like the 1 of Queens, or the 5 of Jacks?

You'll want to have two enums, one for the suits (spades, hearts, diamonds, clubs) and one for the ranks (1, 2, 3, ..., 10, Jack, Queen, King, Ace).
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should also get your terminology straight.

Suit: CLUBS, HEARTS, SPADES, DIAMONDS

Rank/Value: ACE, TWO, THREE, ..., TEN, JACK, QUEEN, KING

(Edit: Jesper is right, values are usually referred to as "rank")
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jin Otanashi wrote:I have never heard of F suit


That's because I made a typo. That was supposed to say 4 suits.
 
jin Otanashi
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:

jin Otanashi wrote:[code=java]

Note: your two enums Value and CardType are not very useful. What happens if you combine them? You get cards like the 1 of Queens, or the 5 of Jacks?

You'll want to have two enums, one for the suits (spades, hearts, diamonds, clubs) and one for the ranks (1, 2, 3, ..., 10, Jack, Queen, King, Ace).



Just thinking of making a games for 2 player, each class have different job that what i was thinking

 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The approach you are taking can be compared to building a house by first gathering up a bunch of nails and board that you think you'll need. In other words, you're doing "bottom up" design. I find that this approach only leads you down the path to confusion. I start designing from the "top" and work my way down to nitty gritty details later on, once I have a good idea of how the main parts are going to work. When you plan out a house, you first draw the general shape of the house, the layout of the rooms, the overall dimensions of the foundation.

In your case, card suits and ranks are the nails and boards of this program. You really should be thinking of the larger parts of the program first or you are probably going to lose sight of the forest for the trees.
 
jin Otanashi
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:The approach you are taking can be compared to building a house by first gathering up a bunch of nails and board that you think you'll need. In other words, you're doing "bottom up" design. I find that this approach only leads you down the path to confusion. I start designing from the "top" and work my way down to nitty gritty details later on, once I have a good idea of how the main parts are going to work. When you plan out a house, you first draw the general shape of the house, the layout of the rooms, the overall dimensions of the foundation.

In your case, card suits and ranks are the nails and boards of this program. You really should be thinking of the larger parts of the program first or you are probably going to lose sight of the forest for the trees.



I see what you mean
 
The fastest and most reliable components of any system are those that are not there. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic