Question: Java! Implement a generic version of the Couple class. The following code is an ADT for two String members. To convert this ADT to a

Java! Implement a generic version of the Couple class.

The following code is an ADT for two String members. To convert this ADT to a generic Couple, you will introduce a type parameter T. This parameter can be thought of as a variable for which one can substitute a (reference) type. You will need to:

use the Diamond after the class name

replace String with T everywhere else

use the member object's toString() method within the Couple.toString() method to access

its value. (Recall that every Java object inherits from Object, so a toString() method is available.)

class Couple of two Strings:

// An ADT for Two strings

class Couple { // fields

private String first; private String second; // constructor Couple(String f, String s) {

 first = f; second = s; 

}

 // getters and setters 

String getFirst() { return first;

 } String getSecond() { 

return second; void setFirst(String f) {

first = f; void setSecond(String s) {

second = s;

}

 // override an Object method 

public String toString() { return "(" + first + ", " + second + ")";

}

}

Now add one more method to the Couple class. Creating an equals() method requires some thought. We don't want to compare the object references but the members first and second in one object to first and second in the other object. We need a boolean method to which we pass in the object whose member variables should be compared to this object's members. The complication arises when we try to compare our Couple-of-anything ADT to another Couple-of- anything ADT. Our first attempt might be:

public boolean equals(Object otherObj){ boolean isEqual = false; Couple o = null; if (otherObj instanceof Couple) {

 o = (Couple) otherObj; 

isEqual = (this.first.equals(o.first) && (this.second.equals(o.second)); }

}

Adding this to Couple.java, we find it does not compile. How very sad. The problem is that the instanceof operator does not work on parameterized types. Instead we can call the getClass() method (ask yourself where this method is defined) on both this and otherObj then compare the

}

}

returned values. (Did you answer that getClass() is inherited from Object? Good job!). So now our next attempt might be:

public boolean equals(Object otherObj){ boolean isEqual = false; Couple o = null; if (this.getClass() == otherObj.getClass()) {

 o = (Couple) otherObj; 

isEqual = (this.first.equals(o.first) && (this.second.equals(o.second)); }

}

This survived the compile process, but there is a warning that our program has used an unsafe operation. Eclipse reports, "Type safety: unchecked cast from Object to Couple". The problem is with this line: o = (Couple) otherObj; Even though we will check the class in the if condition, the compiler is not able to prove that a ClassCastException will not be thrown, so it provides a warning. What to do? Nothing for it but to suppress the warning by adding an annotation (a note to the compiler, like we do with @override).

 @SuppressWarnings("unchecked") 
 public boolean equals(Object otherObj){ boolean isEqual = false; Couple o = null; if (this.getClass() == otherObj.getClass()) { 
 o = (Couple) otherObj; 

isEqual = (this.first.equals(o.first) && (this.second.equals(o.second)); }

}

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!