Question

In: Computer Science

You need to complete the methods getSmallerValue, getLargerValue, compareTo, and equals. Code: public class Domino implements...

You need to complete the methods getSmallerValue, getLargerValue, compareTo, and equals.

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) {

}

}

Solutions

Expert Solution

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) {
            return val1;
        } else {
            return val2;
        }
    }

    public int getLargerValue() {
        if (val1 > val2) {
            return val1;
        } else {
            return val2;
        }
    }

    @Override
    public int compareTo(Domino other) {
        if (val1 == other.val1) {
            return val2 - other.val2;
        }
        return val1 - other.val1;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        Domino domino = (Domino) obj;
        return val1 == domino.val1 && val2 == domino.val2;
    }
}

Related Solutions

You need to complete the methods getSmallerValue, getLargerValue, compareTo, and equals. Must pass these tests: Test...
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....
Author code /** * LinkedList class implements a doubly-linked list. */ public class MyLinkedList<AnyType> implements Iterable<AnyType>...
Author code /** * LinkedList class implements a doubly-linked list. */ public class MyLinkedList<AnyType> implements Iterable<AnyType> { /** * Construct an empty LinkedList. */ public MyLinkedList( ) { doClear( ); } private void clear( ) { doClear( ); } /** * Change the size of this collection to zero. */ public void doClear( ) { beginMarker = new Node<>( null, null, null ); endMarker = new Node<>( null, beginMarker, null ); beginMarker.next = endMarker; theSize = 0; modCount++; } /**...
//Complete the incomplete methods in the java code //You will need to create a driver to...
//Complete the incomplete methods in the java code //You will need to create a driver to test this. public class ManagedArray { private int[] managedIntegerArray; //this is the array that we are managing private int maximumSize; //this will hold the size of the array private int currentSize = 0; //this will keep track of what positions in the array have been used private final int DEFAULT_SIZE = 10; //the default size of the array public ManagedArray()//default constructor initializes array to...
Write a java code for LinkedStack implementation and the code is: public final class LinkedStack<T> implements...
Write a java code for LinkedStack implementation and the code is: public final class LinkedStack<T> implements StackInterface<T> {    private Node topNode; // References the first node in the chain       public LinkedStack()    {        topNode = null;    } // end default constructor       public void push(T newEntry)    { topNode = new Node(newEntry, topNode); //       Node newNode = new Node(newEntry, topNode); //       topNode = newNode;    } // end push    public...
myLinkedList.java>>>>>>>> public class MyLinkedList implements MiniList<Integer>{ /* Private member variables that you need to declare: **...
myLinkedList.java>>>>>>>> public class MyLinkedList implements MiniList<Integer>{ /* Private member variables that you need to declare: ** The head pointer ** The tail pointer */ public class Node { // declare member variables (data and next) // finish these constructors public Node(int data, Node next) {} public Node(int data) {} // HINT: use this() with next = null } // Initialize the linked list (set head and tail pointers) public MyLinkedList() {} @Override public boolean add(Integer item) { return false; }...
1. Consider the following code: public class Widget implements Serializable { private int x; public void...
1. Consider the following code: public class Widget implements Serializable { private int x; public void setX( int d ) { x = d; } public int getX() { return x; } writeObject( Object o ) { o.writeInt(x); } } Which of the following statements is true? I. The Widget class is not serializable because no constructor is defined. II. The Widget class is not serializable because the implementation of writeObject() is not needed. III. The code will not compile...
COMPLETE JAVA CODE: public final class Vector2 {       // NOTE:    Before you get...
COMPLETE JAVA CODE: public final class Vector2 {       // NOTE:    Before you get started with the constructors, implement the class variables and    // =====   the accessor methods (getX,getY). The tester for the constructors relies on these    //           methods being implemented. After this, move ahead with the constructors          // class variables here       COMPLETE JAVA CODE: /** * Creates the vector <code>(0.0, 0.0)</code>. */ public Vector2() {       COMPLETE...
irst, you will complete a class called HazMath (in the HazMath.java file) that implements the interface...
irst, you will complete a class called HazMath (in the HazMath.java file) that implements the interface Mathematical (in the Mathematical.java file). These should have the following definitions: public boolean isPrime(int n) You cannot change the signature for the method. This method is similar to the ones we've discussed in the lab and will return true or false depending on if the passed in integer values is prime or not, respectively. Return false if the invoker passes in a number less...
USING JAVA: Complete the following class. input code where it says //TODO. public class BasicBioinformatics {...
USING JAVA: Complete the following class. input code where it says //TODO. public class BasicBioinformatics { /** * Calculates and returns the complement of a DNA sequence. In DNA sequences, 'A' and 'T' are * complements of each other, as are 'C' and 'G'. The complement is formed by taking the * complement of each symbol (e.g., the complement of "GTCA" is "CAGT"). * * @param dna a char array representing a DNA sequence of arbitrary length, * containing only...
I need to translate my java code into C code. import java.util.Scanner; class CS_Lab3 { public...
I need to translate my java code into C code. import java.util.Scanner; class CS_Lab3 { public static void main( String args[] ) { Scanner input = new Scanner( System.in ); // create array to hold user input int nums[] = new int[10]; int i = 0, truthCount = 0; char result = 'F', result2 = 'F'; // ask user to enter integers System.out.print("Please Enter 10 Different integers: "); // gather input into array for ( i = 0; i <...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT