Reputation: 14126
Herbert Schildt mentions while there is nothing technically wrong with the approaches, the enumeration is a better choice.
Consider the following:
Choice 1:
//An enumeration of the possible answers.
enum Answers {
NO, YES, MAYBE, LATER, SOON, NEVER
}
public class Question {
Random rand = new Random();
Answers ask() {
int prob = (int) (100 * rand.nextDouble());
if (prob < 15)
return Answers.MAYBE; // 15%
else if (prob < 30)
return Answers.NO; // 15%
else if (prob < 60)
return Answers.YES; // 30%
else if (prob < 75)
return Answers.LATER; // 15%
else if (prob < 98)
return Answers.SOON; // 13%else
return Answers.NEVER; // 2%
}
}
public class AskMe {
static void answer(Answers result) {
switch (result) {
case NO:
System.out.println("No");
break;
case YES:
System.out.println("Yes");
break;
case MAYBE:
System.out.println("Maybe");
break;
case LATER:
System.out.println("Later");
break;
case SOON:
System.out.println("Soon");
break;
case NEVER:
System.out.println("Never");
break;
}
}
public static void main(String args[]) {
Question q = new Question();
answer(q.ask());
answer(q.ask());
answer(q.ask());
answer(q.ask());
}
}
Choice 2:
public interface SharedConstants {
int NO = 0;
int YES = 1;
int MAYBE = 2;
int LATER = 3;
int SOON = 4;
int NEVER = 5;
}
public class Question implements SharedConstants {
Random rand = new Random();
int ask() {
int prob = (int) (100 * rand.nextDouble());
if (prob < 30)
return NO; // 30%
else if (prob < 60)
return YES; // 30%
else if (prob < 75)
return LATER; // 15%
else if (prob < 98)
return SOON; // 13%
else
return NEVER; // 2%
}
}
public class AskMe implements SharedConstants {
static void answer(int result) {
switch (result) {
case NO:
System.out.println("No");
break;
case YES:
System.out.println("Yes");
break;
case MAYBE:
System.out.println("Maybe");
break;
case LATER:
System.out.println("Later");
break;
case SOON:
System.out.println("Soon");
break;
case NEVER:
System.out.println("Never");
}
}
public static void main(String args[]) {
Question q = new Question();
answer(q.ask());
answer(q.ask());
answer(q.ask());
answer(q.ask());
}
}
Herbert Schildt gives no reason for the same. I am confused as to why the choice regarding the enum is a better one?
Upvotes: 0
Views: 67
Reputation: 533910
You can be more confident as to what all the possible values are. For example, when you did the int
version you dropped one of the options.
You can also include encapsulate more information rather than distributing it through your code
enum Answers {
NO("No", 0.30),
YES(Yes", 0.30),
MAYBE("Maybe", 0.15),
LATER("Later", 0.15),
SOON("Soon", 0.13),
NEVER("Never", 0.02);
private final String description;
private final double chance;
private Answer(String description, double chance) {
this.description = description;
this.chance = chance;
}
public String getDescription() { return description; }
public Answers getRandom() {
double r = Math.random();
for(Answer a : values())
if ((r -= a.chance) <= 0)
return a;
return NEVER;
}
}
Upvotes: 2
Reputation: 200296
Misusing a generic numerical type such as int
as an enumeration is never a bright idea. The only reason to ever consider using it in Java is performance—and when I say performance, I mean nanoseconds.
You lose a lot by taking the int
approach: the enum
is a full-blown class and can have arbitrary methods, making it easy to streamline and organize code. You also get full type safety. In fact, the advantages so many, this answer would get too long if I tried to enumerate them all.
Upvotes: 3