• 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

Help With Creating Enum And Using It

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. In the murach.card package, create an enumeration named Suit. This enumeration should specify four suits: spades, hearts, diamonds, and clubs.
2. In the Card class, modify the code so it uses the Suit enumeration as the type for the suit instance variable.
3. In the Main class, modify the displayCard method so it uses the Suit enumeration to check the suit of the card.
4. In the Main class, experiment by modifying the code that sets the suit and number for the Card object. Note that you can’t set an illegal suit, but you can set an illegal number.
I've create the ENUM but just need to know how to implement it and use it. Please help me.

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have already implemented the Suit enumeration, though you are not yet using it.

Look at and consider what you could replace it with.

Taking into account that your enumeration has 4 possible values, what does accomplish?

Consider using switch statements instead of repeated else ifs. It supports enumerations.
 
Beg Gok
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Folder wrote:You have already implemented the Suit enumeration, though you are not yet using it.

Look at and consider what you could replace it with.

Taking into account that your enumeration has 4 possible values, what does accomplish?

Consider using switch statements instead of repeated else ifs. It supports enumerations.



Can you help me out with this because it is really confusing for me.
 
Bartender
Posts: 242
27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at this instruction:

2. In the Card class, modify the code so it uses the Suit enumeration as the type for the suit instance variable.


You're done with #1, so work on this next. As pointed out above, this is the suit instance variable:

So you need to change it to use the Suit enum as the type for the variable. Currently, the type is "int". How do you change that to use the enumeration?

After that, you'll need to change the getters and setters to use the enum type too.
 
Marshal
Posts: 79263
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Folder wrote:. . . Consider using switch statements instead of repeated else ifs. It supports enumerations.

Disagree. I wouldn't use anything in that display method. The object‑oriented way to display the card is for the Card object to produce its details. The Java® object‑oriented way to do it is to use the toString method. Multiple if‑elses and the use of static methods look very non‑object‑oriented to me. If you have the object set up completely, you can simply writeOf course single‑letter variable names are a bad idea.


Beg Gok, welcome to the Ranch Please tell us where that exercise comes from; some of us might have the book in question.
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also posted at: http://www.dreamincode.net/forums/topic/404814-need-help-creating-enum-and-using-it/
 
Campbell Ritchie
Marshal
Posts: 79263
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Also posted at: . . .

. . .where there are some helpful answers Than kyou for noticing.
OP: Please read this. It is discourteous to people on both sites not to inform them.
 
New rule: no elephants at the chess tournament. Tiny ads are still okay.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic