• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help With Creating Enum And Using It  RSS feed

 
Beg Gok
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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.

 
Paul Folder
Greenhorn
Posts: 8
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Zachary Griggs
Ranch Hand
Posts: 83
10
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • 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.
 
Norm Radder
Rancher
Posts: 2240
28
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!