0

The equals() Method Example

Drag and drop from the options below to check whether the two objects of type A are semantically equal. class A { private int x; public --1--- equals(Object o) { ---2--- ((A)o).x == this.x; } public static void main(String[ ] args) { A a = new A(); a.x = 9; A b = new -3--(); b.x = 5; System.out.println(a.--4----(b)); } } Options to Use: boolean new A return equals x b I think following : 1 = boolean 3 = A 4. equals I am thinking one more A is missing in given options. Because I think 2 also should be A

14th Apr 2020, 12:35 AM
Sanjay Patil
Sanjay Patil - avatar
2 Antworten
+ 1
equals() returns a boolean. In the head of the method you need to enter the return type. It should be clear that a method with a return type must return this type. public boolean equals(Object o){ return ((A)o).x == this.x; } In the main method: A b = new A(); And you need to call the method in the print statement: System.out.println(a.equals(b));
14th Apr 2020, 1:06 AM
Denise Roßberg
Denise Roßberg - avatar
0
Sanjay Patil Here is a code where you can see that it works correct: https://code.sololearn.com/cH943BOJBQQB/?ref=app
14th Apr 2020, 1:18 AM
Denise Roßberg
Denise Roßberg - avatar