/** A class representing sequences of 3 colors. Each color is represented by an integers. */ import java.util.*; public class TriColor implements Lexicographical { // the possible colors being represented private static String[] colorName = {"red","orange","yellow","green","blue","violet", "white","turquoise","gray","brown","black"}; public int[] color; // the number of legal characters, including // the terminator. private static int alphabetSize = 12; /** A 0-argument constructor, to construct a random TriColor */ public TriColor() { Random r=new Random(); color=new int[3]; color[0]=r.nextInt(alphabetSize-1); color[1]=r.nextInt(alphabetSize-1); color[2]=r.nextInt(alphabetSize-1); } /** A constructor, taking a Random object to construct a random TriColor. @param r the Random object */ public TriColor(Random r) { color=new int[3]; color[0]=r.nextInt(alphabetSize-1); color[1]=r.nextInt(alphabetSize-1); color[2]=r.nextInt(alphabetSize-1); } /** @return a string representing the 3 colors */ public String toString() { return "(" + colorName[color[0]] + "," + colorName[color[1]] + "," + colorName[color[2]] + ")"; } // implementation of the Lexicographical interface /** @return the length of the string being represented (including any illegal characters). */ public int length() { return 3; } /** @return the number of legal characters, including the terminator */ public int getAlphabetSize() { return alphabetSize; } /** @param i a character position @return the character code of the character of the string in the given position (that is, the position of the character in the alphabet's natural ordering, with the terminator character counting as the 0th character. Returns 0 if there is no legal character in the given position -- for this reason the representing integer is incremented before being returned. */ public int elementAt(int i) { if (i<0 || i>=length()) return 0; else return color[i]+1; } };