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 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...
//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...
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?
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...
You are opening a doughnut franchise, but first, you must pass quality control on sugar glaze...
You are opening a doughnut franchise, but first, you must pass quality control on sugar glaze thickness before you can open. You randomly select 10 doughnuts from your production and send the following glaze thickness data to corporate headquarters for analysis: (units in mm) 1.21, 1.25, 1.23, 1.21, 1.28, 1.22, 1.27, 1.29, 1.20, 1.26 At α = 0.05, your sample data must statistically demonstrate that the mean glaze thickness is at least 1.20 mm. Determine whether or not your doughnuts...
''' string_funcs ========== Complete the following functions. You MUST, MUST, MUST add: 1) a doc comment...
''' string_funcs ========== Complete the following functions. You MUST, MUST, MUST add: 1) a doc comment in the proper location for EACH function that describes the basic purpose of the function. 2) AT LEAST 3 doc test comments in EACH function that: * test that the function does what it is supposed to do * tests that it does what it's supposed to do with odd inputs * tests "edge" cases (numbers at, just above, just below min/max, empty strings,...
Suppose you want to test the claim that a population mean equals 36. (a) State the...
Suppose you want to test the claim that a population mean equals 36. (a) State the null hypothesis. H0: μ > 36 H0: μ < 36     H0: μ = 36 H0: μ ≠ 36 H0: μ ≥ 36 (b) State the alternate hypothesis if you have no information regarding how the population mean might differ from 36. H1: μ > 36 H1: μ < 36     H1: μ = 36 H1: μ ≠ 36 H1: μ ≥ 36 (c) State the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT