Question

In: Computer Science

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.

*/

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

// 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


Related Solutions

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...
YOU MUST COMPLETE THIS TO PASS THIS CLASS. Discussion Assignment: Referencing the ROME mnemonic listed below,...
YOU MUST COMPLETE THIS TO PASS THIS CLASS. Discussion Assignment: Referencing the ROME mnemonic listed below, prepare a factual submission article explaining the significance of the following lab results. Submit your results as your discussion contribution. Mrs. Breathless is a 42 year old female, just getting off a late shift. She reports to the ER in the early morning with shortness of breath. After several test, the following results are confirmed: ABG (arterial blood gases) lab results are: pH=7.44, PaCO2...
//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...
You must change file HelloWorld.java in hello-java so that the tests all pass HelloWorld.java package hw;...
You must change file HelloWorld.java in hello-java so that the tests all pass HelloWorld.java package hw; public class HelloWorld { public String getMessage() { return "hello world"; } public int getYear() { return 2019; } } Main.java package hw; import java.util.Arrays; public class Main { public static void main(final String[] args) { System.out.println("args = " + Arrays.asList(args)); final HelloWorld instance = new HelloWorld(); System.out.println(instance.getMessage()); System.out.println(instance.getYear()); System.out.println("bye for now"); } } TestHelloWorld.java package hw; import static org.junit.Assert.*; import org.junit.After; import org.junit.Before;...
As a condition of employment, Fashion Industries applicants must pass a drug test. Of the last...
As a condition of employment, Fashion Industries applicants must pass a drug test. Of the last 210 applicants, 24 failed the test. (1) Develop a 90% confidence interval for the proportion of applicants that fail the test. (Round your answers to 3 decimal places.) (2) Yes or no...Would it be reasonable to conclude that more than 7% of the applicants are now failing the test?
//ONLY PUT SCREENSHOTS OF THE ANSWER FROM INTELLIJ //Complete the incomplete methods //You will need to...
//ONLY PUT SCREENSHOTS OF THE ANSWER FROM INTELLIJ //Complete the incomplete methods //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...
You must write tests for the following: all tests must be written in JUnit 5. Customer...
You must write tests for the following: all tests must be written in JUnit 5. Customer Class Checking that the customer number autoincrements with each new customer object created Order Class Checking that the order number autoincrements with each new order object created Adding a product that is already in the order Removing a product that is in the order - quantity less than the quantity in the order Removing a product that is in the order - quantity that...
c++ ///////////////////////////////////////////////////////////////////////// need to be named Subsequences.h and pass test cpp file SubsequenceTester.cpp at the end...
c++ ///////////////////////////////////////////////////////////////////////// need to be named Subsequences.h and pass test cpp file SubsequenceTester.cpp at the end of this question. This assignment will help you solve a problem using recursion Description A subsequence is when all the letters of a word appear in the relative order of another word. Pin is a subsequence of Programming ace is a subsequence of abcde bad is NOT a subsequence abcde as the letters are not in order This assignment you will create a Subsequence...
In some states, high school students must pass a graduation test before they are able to...
In some states, high school students must pass a graduation test before they are able to receive their high school diplomas. Suppose the students are given up to three chances to pass the exam. If they pass it, they don't need to take it again. If they do not pass the test on the first try, they take it a second time. If they don't pass on the second try, they take it a third time. From past data, we...
For all hypothesis tests, you must show the four steps: 1. Hypotheses 2. Test statistic 3....
For all hypothesis tests, you must show the four steps: 1. Hypotheses 2. Test statistic 3. p-value or p-value approximation 4. Conclusion sentence (Do no just say ”Reject the null hypothesis” or ”Fail to reject the null hypothesis”) If doing the hypothesis test in jamovi, you must include the jamovi output but show the four steps separately as well. Exercises 1. The Department of Natural Resources (DNR) received a complaint from recreational fishermen that a community was releasing sewage into...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT