Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello I am working on a Compare Object Java Program. Assignment: Create a class,

ID: 3761985 • Letter: H

Question

Hello I am working on a Compare Object Java Program.

Assignment:

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 &

I am lost on how to start this, any help would be appreciated!!!!

Explanation / Answer

public class ObjCompare {
  
   /**
   * @param c1 First Comparable object
   * @param c2 Second Comparable object
   * @return whether c1 is equal to c2
   */
   public static boolean compareObj(Comparable c1, Comparable c2) {
       return c1.equals(c2);
      
   }

   public static void main(String[] args) {
       // Testing method compareObj
      
       // Testing with two Integers for both cases
       System.out.println("Testing with two Integers for both cases");
       Integer int1 = new Integer(0);
       Integer int2 = new Integer(1);
       Integer int3 = new Integer(0);
       System.out.println(compareObj(int1, int2)); // returns false
       System.out.println(compareObj(int1, int3)); // returns true
      
       // Testing with two Doubles for both cases
       System.out.println("Testing with two Integers for both cases");
       Double d1 = new Double(1.0);
       Double d2 = new Double(-2.2);
       Double d3 = new Double(1.0);
       System.out.println(compareObj(d1, d2)); // returns false
       System.out.println(compareObj(d1, d3)); // returns true
      
       // Testing with two Strings for both cases
       System.out.println("Testing with two Strings for both cases");
       String s1 = new String("Welcome");
       String s2 = new String("Home");
       String s3 = new String("Welcome");
       System.out.println(compareObj(s1, s2)); // returns false
       System.out.println(compareObj(s1, s3)); // returns true
      
       // Testing with two Characters for both cases
       System.out.println("Testing with two Characters for both cases");
       Character c1 = new Character('&');
       Character c2 = new Character('#');
       Character c3 = new Character('&');
       System.out.println(compareObj(c1, c2)); // returns false
       System.out.println(compareObj(c1, c3)); // returns true
      
       // Testing with one Integer and one Double
       System.out.println("Testing with one Integer and one Double");
       System.out.println(compareObj(int1, d1)); // returns false
      
       // Testing with one Integer and one String
       System.out.println("Testing with one Integer and one String");
       System.out.println(compareObj(int1, s1)); // returns false
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote