1/* 2 * Domino.java3 * 4 * Computer Science S-225 */6 7/**8 * A class representing a domino tile9 * 10 * For example, the 5-2 domino looks like this:11 * 12 * ╭───────────┬───────────╮13 * │ ● ● │ ● │14 * │ ● │ │15 * │ ● ● │ ● │16 * ╰───────────┴───────────╯17 *18 * Instances of this class are immutable. To obtain the flipped version of19 * a domino, use the return value of the flipped() method. The equals() method20 * returns true when both sides of two dominoes are the same, flipped or not.21 */22public class Domino {23 private final int left;24 private final int right;25 26 public Domino(int left, int right) {27 if (left < 0 || right < 0) {28 throw new IllegalArgumentException("must have positive # of dots");29 }30 if (left > 6 || right > 6) {31 throw new IllegalArgumentException("# of dots must be 6 or less");32 }33 this.left = left;34 this.right = right;35 }36 37 /**38 * Return the left side of this domino.39 */40 public int getLeft() {41 return this.left;42 }43 44 /**45 * Return the right side of this domino.46 */47 public int getRight() {48 return this.right;49 }50 51 /**52 * Return a flipped version of this domino.53 */54 public Domino flipped() {55 return new Domino(this.right, this.left);56 }57 58 /**59 * Return true if the domino is a "double" (same value on both sides).60 */61 public boolean isDouble() {62 return this.right == this.left;63 }64 65 /**66 * Two dominoes are equal when they have the same sides, flipped or not.67 */68 @Override69 public boolean equals(Object otherObj) {70 if (!(otherObj instanceof Domino)) {71 return false;72 }73 74 Domino other = (Domino)otherObj;75 return this.left == other.left && this.right == other.right ||76 this.left == other.right && this.right == other.left;77 }78 79 @Override80 public String toString() {81 return String.format("%d-%d", this.left, this.right);82 }83 84 public static void main(String[] args) {85 Domino oneFour = new Domino(1, 4);86 Domino fourOne = oneFour.flipped(); // i.e., new Domino(4, 1)87 88 System.out.println("1-4 equals 4-1? " + oneFour.equals(fourOne));89 90 // Two different objects in memory, so == gives us false91 System.out.println("1-4 == 4-1? " + (oneFour == fourOne));92 }93}
int left
Field defined at line 23.
int right
Field defined at line 24.
Domino(left, right)
Constructor defined at line 26.
getLeft()
Method defined at line 40.
getRight()
Method defined at line 47.
flipped()
Method defined at line 54.
isDouble()
Method defined at line 61.
equals(otherObj)
Method defined at line 69.
toString()
Method defined at line 80.
main(args)
Method defined at line 84.