Skip to main content

multi-valued variable

1 reply [Last post]
zscipio
Offline
Joined: 2008-11-08
Points: 0

As booleans can have 2 values, true & false, my program needs a new type of variable that has 5 states. The criteria for this concept must be:
1. Easy to understand.
2. short.

3. Hard to make a mistake.
I have come up with the followinng:
Define an Interface
public interface ExpandedBoolean
{
public static enum expandedBoolean {NO, RARE, MAYBE, OFTEN, YES};

}

include the interface in the program
import static multiValue.ExpandedBoolean.*;
The program can then be written:
expandedBoolean doesThisHappen = expandedBoolean.NO;
if (doesThisHappen == expandedBoolean.NO)
System.out.println("Should be NO = " + doesThisHappen);

doesThisHappen = expandedBoolean.RARE;
if (doesThisHappen == expandedBoolean.RARE)
System.out.println("Should be RARE = " + doesThisHappen);

doesThisHappen = expandedBoolean.MAYBE;
if (doesThisHappen == expandedBoolean.MAYBE)
System.out.println("Should be MAYBE = " + doesThisHappen);

doesThisHappen = expandedBoolean.OFTEN;
if (doesThisHappen == expandedBoolean.OFTEN)
System.out.println("Should be OFTEN = " + doesThisHappen);

doesThisHappen = expandedBoolean.YES;
if (doesThisHappen == expandedBoolean.YES)
System.out.println("Should be YES = " + doesThisHappen);
Does anyone have any comments on how to make the concept better according to the 3 criteria?
Thank you

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tianwang
Offline
Joined: 2008-06-07
Points: 0

You do not need the interface.
Try
public enum ExpandedBoolean{
NO, RARE, MAYBE, OFTEN, YES
}