Question

In: Computer Science

Using your solution or your instructor's solution from the Module 8 ungraded practice exercise, implement a...

Using your solution or your instructor's solution from the Module 8 ungraded practice exercise, implement a unit test for the Pair class by adding a main method.

Create three small constituent classes (I created PeanutButter, Jelly, and Mustard classes) and insert various combinations of them into an array of Pair objects. Thoroughly test the equals(), hashCode(), and toString() methods from the Pair class with your main method.

Note: override the toString() method in each of your constituent classes so they are properly displayed in the output when printed from the Pair toString() method, otherwise only the hexadecimal Object representation will be displayed).

Sample output:

pair 0: [Peanut Butter, Jelly]
pair 0 hashcode: 2027766238
pair 1: [Peanut Butter, Mustard]
pair 1 hashcode: 1837727328
pair 2: [Peanut Butter, Peanut Butter]
pair 2 hashcode: 366712642
pair 3: [Peanut Butter, Peanut Butter]
pair 3 hashcode: 366712642

[Peanut Butter, Jelly] equals [Peanut Butter, Mustard]? false
[Peanut Butter, Mustard] equals [Peanut Butter, Peanut Butter]? false
[Peanut Butter, Peanut Butter] equals [Peanut Butter, Peanut Butter]? true

Module 8 ungraded practice solution:

// Pair.java
// Aggregate (non-generic) pairs of arbitrary objects
// adapted from Mughal Ch. 8

public final class Pair {
private final Object first, second;
  
// construct a Pair object
public Pair(Object one, Object two) {
first = one;
second = two;
}
  
// return the first constituent object
public Object getFirst() { return first; }
  
// return the second constituent object
public Object getSecond() { return second; }
  
// return true if the pair of objects are identical
@Override
public boolean equals(Object other) {
boolean result;
if (this == other)
result = true;
else if (! (other instanceof Pair))
result = false;
else {
Pair otherPair = (Pair) other;
result = first.equals(otherPair.first) && second.equals(otherPair.second);
}
return result;
}
  
// return hash code for the aggregate pair
@Override
public int hashCode() {
// XOR hash codes to create a hashcode for the pair
// if the objects are equal, use one hashcode, otherwise
// the XOR result (and subsequent hashCode value) is 0.
return first.equals(second) ? first.hashCode() : first.hashCode() ^ second.hashCode();
}
  
// return textual representation of aggregated object
@Override
public String toString() {
return "[" + first + ", " + second + "]";
}
}

Solutions

Expert Solution

I am using your class Pair class. I am created three small constituent classes with name PeanutButter.java , Jelly.java , Mustard.java ....And also Created for testing with name Test.java

Note: I am created above 4 classses and your class all are created in Eclipse. and I have created package with name "Chapter_15",(all classes are created in this package)

**************************Java code**********************

package Chapter_15;
/**
* @author ammuarjun
* @version 1.0
* classes PeanutButter,Jelly,Mustard
*
*/
//Create three small constituent classes with name PeanutButter,Jelly,Mustard
//along with the overridden toString() method
/*******************PeanutButter********************/
class PeanutButter {
   @Override
   public String toString() {
       return "Peanut Butter";
   }
}
/*******************Jelly********************/
class Jelly {
   @Override
   public String toString() {
       return "Jelly";
   }
}
/*******************Mustard********************/
class Mustard {
   @Override
   public String toString() {
       return "Mustard";
   }
}

/*******************Test********************/
public class Test {

   public static void main(String[] args) {

       // Create objects for those small constituent classes
       PeanutButter peanutButter = new PeanutButter();
       Jelly jelly = new Jelly();
       Mustard mustard = new Mustard();
       // Create a pair
       Pair pair = new Pair(peanutButter, peanutButter);
       /**
       * Create an array of Pair objects
       * and initialize it with various combinations
       * of the objects of those constituent classes.
       * This can also be written in multiple lines as:
       * Pair[] array = new Pair[11];
       * array[0] = new Pair(peanutButter, peanutButter);
       * array[1] = new Pair(peanutButter, jelly);
       * array[5] = pair
       * ...... and so on
       */
       Pair[] array = new Pair[] {
               new Pair(peanutButter, jelly),
               new Pair(peanutButter, mustard),
               pair,
               new Pair(peanutButter, peanutButter),
               new Pair(jelly, peanutButter),
               new Pair(jelly, jelly),
               pair,
               pair,
   new Pair(jelly, mustard),
   new Pair(jelly, mustard),
   new Pair(new PeanutButter(), new Mustard()),
   new Pair(new PeanutButter(), mustard)
       };
       // Test toString() and hashCode() methods
       for (int i = 0; i < array.length; i++) {
           System.out.println("pair " + i + ": " + array[i]);
           System.out.println("pair " + i + " hashcode: " + array[i].hashCode());
       }

       // Test equals() method
       for (int i = 1; i < array.length; i++) {
           System.out.println(array[i-1] + " equals " + array[i] + "? " + array[i-1].equals(array[i]));
       }

   }

}

Output

pair 0: [Peanut Butter, Jelly]
pair 0 hashcode: 2027766238
pair 1: [Peanut Butter, Mustard]
pair 1 hashcode: 1837727328
pair 2: [Peanut Butter, Peanut Butter]
pair 2 hashcode: 366712642
pair 3: [Peanut Butter, Peanut Butter]
pair 3 hashcode: 366712642
pair 4: [Jelly, Peanut Butter]
pair 4 hashcode: 2027766238
pair 5: [Jelly, Jelly]
pair 5 hashcode: 1829164700
pair 6: [Peanut Butter, Peanut Butter]
pair 6 hashcode: 366712642
pair 7: [Peanut Butter, Peanut Butter]
pair 7 hashcode: 366712642
pair 8: [Jelly, Mustard]
pair 8 hashcode: 357842878
pair 9: [Jelly, Mustard]
pair 9 hashcode: 357842878
pair 10: [Peanut Butter, Mustard]
pair 10 hashcode: 1227423489
pair 11: [Peanut Butter, Mustard]
pair 11 hashcode: 607557415
[Peanut Butter, Jelly] equals [Peanut Butter, Mustard]? false
[Peanut Butter, Mustard] equals [Peanut Butter, Peanut Butter]? false
[Peanut Butter, Peanut Butter] equals [Peanut Butter, Peanut Butter]? true
[Peanut Butter, Peanut Butter] equals [Jelly, Peanut Butter]? false
[Jelly, Peanut Butter] equals [Jelly, Jelly]? false
[Jelly, Jelly] equals [Peanut Butter, Peanut Butter]? false
[Peanut Butter, Peanut Butter] equals [Peanut Butter, Peanut Butter]? true
[Peanut Butter, Peanut Butter] equals [Jelly, Mustard]? false
[Jelly, Mustard] equals [Jelly, Mustard]? true
[Jelly, Mustard] equals [Peanut Butter, Mustard]? false
[Peanut Butter, Mustard] equals [Peanut Butter, Mustard]? false


Related Solutions

Instructions Use your solution from Programming Assignment 5 (or use your instructor's solution) to create a...
Instructions Use your solution from Programming Assignment 5 (or use your instructor's solution) to create a modular Python application. Include a main function (and a top-level scope check),and at least one other non-trivial function (e.g., a function that deals the new card out of the deck). The main function should contain the loop which checks if the user wants to continue. Include a shebang, and ID header, descriptive comments, and use constants where appropriate. Sample Output (user input in red):...
Instructions Use your solution from Programming Assignment 5 (or use your instructor's solution) to create a...
Instructions Use your solution from Programming Assignment 5 (or use your instructor's solution) to create a modular Python application. Include a main function (and a top-level scope check),and at least one other non-trivial function (e.g., a function that deals the new card out of the deck). The main function should contain the loop which checks if the user wants to continue. Include a shebang, and ID header, descriptive comments, and use constants where appropriate. Sample Output (user input in red):...
Implement the following function in the PyDev module functions.py and test it from a PyDev module...
Implement the following function in the PyDev module functions.py and test it from a PyDev module named : def statistics(n): """ ------------------------------------------------------- Asks a user to enter n values, then calculates and returns the minimum, maximum, total, and average of those values. Use: minimum, maximum, total, average = statistics(n) ------------------------------------------------------- Parameters: n - number of values to process (int > 0) Returns: minimum - smallest of n values (float) maximum - largest of n values (float) total - total of...
Implement the following function in the PyDev module functions.py and test it from a PyDev module...
Implement the following function in the PyDev module functions.py and test it from a PyDev module named : def gym_cost(cost, friends): """ ------------------------------------------------------- Calculates total cost of a gym membership. A member gets a discount according to the number of friends they sign up. 0 friends: 0% discount 1 friend: 5% discount 2 friends: 10% discount 3 or more friends: 15% discount Use: final_cost = gym_cost(cost, friends) ------------------------------------------------------- Parameters: cost - a gym membership base cost (float > 0) friends...
From question 19: findAndTruncate "...+2 EC (at the instructor's discretion) will go to the (correct) solution...
From question 19: findAndTruncate "...+2 EC (at the instructor's discretion) will go to the (correct) solution with the least number of statements ." Find two unneeded statements and one performance issue in the below code: findAndTruncate(char *trunc, const char placeholder) { for(int i = 0; *(trunc + i) != '\0'; i++)          { if(*(trunc + i) == placeholder)                    {    * (trunc + i) = '\0'; } } }
using Java Your mission in this exercise is to implement a very simple Java painting application....
using Java Your mission in this exercise is to implement a very simple Java painting application. Rapid Prototyping The JFrame is an example that can support the following functions: 1. Draw curves, specified by a mouse drag. 2. Draw filled rectangles or ovals, specified by a mouse drag (don't worry about dynamically drawing the shape during the drag - just draw the final shape indicated). 3. Shape selection (line, rectangle or oval) selected by a combo box OR menu. 4....
Design and implement a C++ class called Module that handles information regarding your assignments for a specific module.
Design and implement a C++ class called Module that handles information regarding your assignments for a specific module. Think of all the things you would want to do with such a class and write corresponding member functions for your Module class. Your class declaration should be well-documented so that users will know how to use it.Write a main program that does the following: Declare an array of all your modules. The elements of the array must be of type Module....
For this activity, you will select one of the four module objectives from the Module 8...
For this activity, you will select one of the four module objectives from the Module 8 Overview and Objectives page 1. Analyze International Trade Theory. 2. Examine trade restrictions and their effectiveness. 3. Discuss the foreign exchange market. 4. Distinguish between fixed and flexible exchange rate systems. Your presentation is required to be narrated and at maximum be five minutes in length. Here are your required slides: Title Slide Introductory Slide Content Slide Summary/Conclusion Slide Reference Slide
For practice using exceptions, this exercise will use a try/catch block to validate integer input from...
For practice using exceptions, this exercise will use a try/catch block to validate integer input from a user. Write a brief program to test the user input. Use a try/catch block, request user entry of an integer, use Scanner to read the input, throw an InputMismatchException if the input is not valid, and display "This is an invalid entry, please enter an integer" when an exception is thrown. InputMismatchException is one of the existing Java exceptions thrown by the Scanner...
Cat worksheet: use in combination with the cat photos provided in the module to practice your...
Cat worksheet: use in combination with the cat photos provided in the module to practice your dihybrid cross skills **refer to text sections 9.4 & 9.5 to help you during this exercise** Look at the photographs of domestic cats found in this module and take a look at the information on Mendelian traits that control the appearance of cats’ coats found below: Coat uniformity is controlled by genes at the P locus: genotype (P--) = white patches of fur &...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT