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++; } /**...
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...
//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...
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...
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...
Complete the code to make the following main work public class Time { **put the solution...
Complete the code to make the following main work public class Time { **put the solution here** public static void main(String[] args){ Time a = new Time(15,10,30); Time b = new Time(); // default to 15:00:00(the start time of our class!) System.out.println(a); // should print out: 15:10:30 System.out.println(b); // should print out: System.out.println(a.dist(b)); // print the difference in seconds between the two timestamps } }
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** *...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** * Create a point with coordinates <code>(0, 0)</code>. */ public Point2() { complete JAVA code this.set(0.0, 0.0); COMPLETE CODE }    /** * Create a point with coordinates <code>(newX, newY)</code>. * * @param newX the x-coordinate of the point * @param newY the y-coordinate of the point */ public Point2(double newX, double newY) { complete Java code this.set(newX, newY); }    /** * Create a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT