In: Computer Science
The Language is Java.
Sample Output:
There is no given sample Output
IsEqualToTest (30)
Write a simple generic version of method isEqualTo that compares its two arguments with the equals method and returns true if they’re equal and false otherwise. Use this generic method in a program that calls isEqualToTest with the built-in types Integer, String, Double and Object. The main reads in two Integer, and two Double values with autoboxing, as well as two String and calls the method. For Object just create two Object objects with no parameters. The method doesn’t need to include any correctness tests.
Please find the code below.
class isEqualToTest<T1, T2>
{
T1 obj1; // An object of type T1
T2 obj2; // An object of type T2
isEqualToTest(T1 obj1, T2 obj2) // Constructor of the class
'isEqualToTest'
{
this.obj1 = obj1;
this.obj2 = obj2;
}
// It will return the boolean value after comparison
public boolean isEqualTo()
{
if (obj1.equals(obj2)){
return true;
}
else
return false;
}
}
class Main
{
public static void main (String[] args)
{
Object ob1 = new Object();
Object ob2 = new Object();
isEqualToTest <Integer, Integer> int_test =
new isEqualToTest<Integer, Integer>(15, 15);
isEqualToTest <Double, Double> double_test =
new isEqualToTest<Double, Double>(15.8, 15.7);
isEqualToTest <String, String> str_test =
new isEqualToTest<String, String>("ABC", "ABC");
isEqualToTest <Object, Object> obj_test =
new isEqualToTest<Object, Object>(ob1, ob2);
// Printing all outputs here
System.out.println("*** OUTPUT ***");
System.out.println(int_test.isEqualTo());
System.out.println(double_test.isEqualTo());
System.out.println(str_test.isEqualTo());
System.out.println(obj_test.isEqualTo());
}
}
OUTPUT:
