In: Computer Science
You need to complete the methods getSmallerValue, getLargerValue, compareTo, and equals.
Must pass these tests: Test that each domino is smaller than every domino after it in the list (compareTo method). - Test that each domino is greater than every domino before it in the list (compareTo method). - Tests if three dominoes with the same values are equal (equal method).
Code:
public class Domino implements Comparable<Domino> {
/**
* The smallest possible value for a side of a domino.
*/
public static final int MIN_VALUE = 0;
/**
* The largest possible value for a side of a domino.
*/
public static final int MAX_VALUE = 6;
/**
* The two values on the domino.
*/
private int val1;
private int val2;
public Domino() {
this(0, 0);
}
public Domino(int value1, int value2) {
if (!isValueOK(value1) || !isValueOK(value2)) {
throw new IllegalArgumentException();
}
this.val1 = value1;
this.val2 = value2;
}
public Domino(Domino other) {
this(other.val1, other.val2);
}
private static boolean isValueOK(int value) {
return value >= MIN_VALUE && value <= MAX_VALUE;
}
@Override
public int hashCode() {
return this.getSmallerValue() + 11 * this.getLargerValue();
}
@Override
public String toString() {
return "[" + this.getSmallerValue() + " : " + this.getLargerValue() + "]";
}
/*
* You need to implement the four methods below. Both compareTo and equals
* should make use of getSmallerValue and getLargerValue.
*/
public int getSmallerValue() {
}
public int getLargerValue() {
}
@Override
public int compareTo(Domino other) {
}
@Override
public boolean equals(Object obj) {
}
}
// Domino.java
public class Domino implements Comparable<Domino>{
/**
* The smallest possible value for a side of a domino.
*/
public static final int MIN_VALUE = 0;
/**
* The largest possible value for a side of a domino.
*/
public static final int MAX_VALUE = 6;
/**
* The two values on the domino.
*/
private int val1;
private int val2;
public Domino() {
this(0, 0);
}
public Domino(int value1, int value2) {
if (!isValueOK(value1) || !isValueOK(value2)) {
throw new IllegalArgumentException();
}
this.val1 = value1;
this.val2 = value2;
}
public Domino(Domino other) {
this(other.val1, other.val2);
}
private static boolean isValueOK(int value) {
return value >= MIN_VALUE && value <= MAX_VALUE;
}
@Override
public int hashCode() {
return this.getSmallerValue() + 11 * this.getLargerValue();
}
@Override
public String toString() {
return "[" + this.getSmallerValue() + " : " + this.getLargerValue() + "]";
}
/*
* You need to implement the four methods below. Both compareTo and equals
* should make use of getSmallerValue and getLargerValue.
*/
public int getSmallerValue() {
if(val1 < val2) // if val1 < val2 , return val1
return val1;
return val2; // val2 <= val1
}
public int getLargerValue() {
if(val1 > val2) // if val1 > val2, return val1
return val1;
return val2; // val2 >= val1
}
@Override
public int compareTo(Domino other) {
// if larger value of this domino > larger value of other domino then return 1
if(getLargerValue() > other.getLargerValue())
return 1;
// if larger value of this domino < larger value of other domino then return -1
else if(getLargerValue() < other.getLargerValue())
return -1;
else // if larger values of this and other domino are equal
{
// if smaller value of this domino > smaller value of other domino then return 1
if(getSmallerValue() > other.getSmallerValue())
return 1;
// if smaller value of this domino < smaller value of other domino then return -1
else if(getSmallerValue() < other.getSmallerValue())
return -1;
else // smaller values of this and other domino are equal , return 0 (both domino are equal)
return 0;
}
}
@Override
public boolean equals(Object obj) {
// check if obj is an instance of Domino class
if(obj instanceof Domino)
{
Domino other = (Domino)obj; // cast obj to Domino
// Both dominoes are equal only if there larger and smaller values are equal
return((getLargerValue() == other.getLargerValue()) &&
getSmallerValue() == other.getSmallerValue());
}
return false; // obj is not an instance of Domino
}
}
//end of Domino.java