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

Exercise 1 Write a generic method that compares its 2 arguments using the equals

ID: 3819622 • Letter: E

Question

Exercise 1

Write a generic method that compares its 2 arguments using the equals method and returns true if they are equal and false otherwise. Ensure that the name of your method includes your last name.

Use your generic method in a program that tests it with a variety of built-in types such as Object or Integer.

Take a screenshot of your output and paste it into a new Word document under an “Exercise 1” heading.

Below the screenshot in the Word document, briefly discuss the results that you saw when you ran the program. Did you see what you expected to see? Why or why not?

Explanation / Answer

public class IsEqual {

       public static void main(String[] args) {

              Object>new Object();

              Object two = one;

              if(isEqualTo(one,two)) {

                     System.out.println("Objects one and two are equal");

              }

              Object three = new Object();

              if(isEqualTo(one,three)) {

                     System.out.println("Objects one and three are equal");

              }

              int four = 5;

              int five = 5;

              if(isEqualTo(four,five)) {

                     System.out.println("5 is equal to 5");

              }

              int six = 17;

              if(isEqualTo(four,six)) {

                     System.out.println("5 is equal to 17");

              }

       }

       public static <T> boolean isEqualTo(T arg1, T arg2) {

              return arg1.equals(arg2);

       }

}