Question: Need some help with some refresher questions for this chapter. Thanks Edit: public class Pair { private T item1, item2; public Pair(T item1, T item2)

Need some help with some refresher questions for this chapter. Thanks

Edit:

public class Pair { private T item1, item2; public Pair(T item1, T item2) { this.item1 = item1; this.item2 = item2; } public T getItem1() { return item1; } public T getItem2() { return item2; } public void setItem1(T item1) { this.item1 = item1; } public void setItem2(T item2) { this.item2 = item2; } @Override public String toString() { return item1.toString() + "\t" + item2.toString(); } public boolean sameItems() { return item1.equals(item2); // return item1==item2; INCORRECT! tests for aliases, not logical equivalence }

}

Question 1: Pair Modification A- equals Method

Add an equals method to the Pair class from the Module 2 Lecture Code. Two Pair objects are logically equivalent if their two objects are logically equivalent (ignoring order). For example, using this definition, the Pair (1,2) would be equal to the Pair (2,1). Post only the equals method (do not post the rest of the class that was unchanged).

Question 2: Pair Modification B- compareTo Method

Modify the Pair class from the Module 2 Lecture Code so that Pair implements Comparable. (Use the class header and method header below.) Implement the compareTo method. Order Pairs by the first and then second item in the Pair. Post only the compareTo method (do not post the rest of the class that was unchanged.)

public class Pair> implements Comparable> public int compareTo(Pair pair)

(Note: with the definition of equals and compareTo required here, two Pair objects can be equal but compareTo would not return 0! This is rare, but it does happen! See here (Links to an external site.)Links to an external site. for the description of this on the API page and here (Links to an external site.)Links to an external site. for an example.)

Question 3: Box Modification A- Box with History

Modify the Box class from the Module 2 Lecture Code. Add an ArrayList to keep track of the "box history"- all items that have been previously added to the box.

Post the new instance data variable, modified constructor, and the modified replaceContents method. Post only your new, added code- do not post the rest of the code that was not changed.

Question 4: Box Modification B- Box with History equals method

Using the modified Box class described above in Part A, update the Box equals method. Two boxes are logically equivalent if they have the same item and same history of items.

Question 5: Box Modification C- Box with History compareTo method

Using the modified Box class described above in Part A, update the compareTo method. Boxes should be ordered based on the current item in the box. If the current item is the same, then order based on the number of times the item has been replaced. If the number of replacements is also the same, then order based on the comparison of the first item in the history.

Question 6: Quad Class

Write a generic class that represents a Quad, defined as a pair of Pair objects. The Pair objects could hold different types (for example, a Quad could be made up of a Pair of Integers and a Pair of Strings). Post the class header, instance data variables, constructor, and equals method. Two Quad objects are equal if the two Pair objects are equal.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!