Im having a real tough time on this problem Can someone plea

I\'m having a real tough time on this problem. Can someone please provide this code in Java??

Create a class, ObjCompare, with a simple generic method, compareObj that

compares any two objects whose class implements Comparable. The

compareObj method must use the equals() method of the Comparable

objects, and returns true is they are equal, otherwise it returns false. Test

your class by comparing two Integers, two Doubles, two Strings, and two

Characters. What happens when you attempt to compare diverse types? For

instance try comparing an Integer to a Double; and, try comparing an Integer

to a String. Partial sample output:

1 is NOT equal to 0

1 is equal to 1

1.0 is NOT equal to -2.2

1.0 is equal to 1.0

Welcome is NOT equal to Home

Welcome is equal to Welcome

& is NOT equal to #

& is equal to &

Solution

Hi, Please find my implementation.

Please let me know in case of any issue.

########### ObjCompare.java ############

public class ObjCompare {

   // takes two parameters that implements Comparable

   public <T extends Comparable<T>> boolean compareObj(T obj1, T obj2){

      

       // calling object \'equals\' method

       if(obj1.equals(obj2))

           return true;

       else

           return false;   

   }

}

############   ObjCompareTest.java ###########

public class ObjCompareTest {

  

   public static void main(String[] args) {

      

       // creating an Object of ObjCompare

       ObjCompare compare = new ObjCompare();

      

       Integer i1 = new Integer(34);

       Integer i2 = new Integer(33);

       Integer i3 = new Integer(34);

      

       if(compare.compareObj(i1, i2)){

           System.out.println(i1 +\" is equal to \"+i2);

       }else{

           System.out.println(i1 +\"is NOT to \"+i2);

       }

      

       if(compare.compareObj(i1, i3)){

           System.out.println(i1 +\" is equal to \"+i3);

       }else{

           System.out.println(i1 +\"is NOT to \"+i3);

       }

      

       String s1 = \"Welcome\";

       String s2 = new String(\"Welcome\");

       if(compare.compareObj(s1, s2)){

           System.out.println(s1 +\" is equal to \"+s2);

       }else{

           System.out.println(s1 +\"is NOT to \"+s2);

       }

      

   }

}

/*

Sample Run:

34is NOT to 33

34 is equal to 34

Welcome is equal to Welcome

*/

IF you call compareObj method with two different type of object, it throws compile time error

I\'m having a real tough time on this problem. Can someone please provide this code in Java?? Create a class, ObjCompare, with a simple generic method, compareO
I\'m having a real tough time on this problem. Can someone please provide this code in Java?? Create a class, ObjCompare, with a simple generic method, compareO
I\'m having a real tough time on this problem. Can someone please provide this code in Java?? Create a class, ObjCompare, with a simple generic method, compareO

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site