-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathequalsmethod.java
More file actions
52 lines (50 loc) · 1.17 KB
/
Copy pathequalsmethod.java
File metadata and controls
52 lines (50 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.util.Scanner;
public class equalsmethod
{
public static void main(String[] args)
{
Newclass a=new Newclass();
Newclass b=new Newclass();
System.out.println("Enter the text for the first instance:");
a.setQ();
System.out.println("Enter the number for first instance:");
a.setI();
System.out.println("\nEnter the text for the second instance:");
b.setQ();
System.out.println("Enter the number for second instance:");
b.setI();
if(a.equals(b))
{
System.out.println("\n\nThe two objects are equal.");
}
else
{
System.out.println("\n\nThe two objects are not equal.");
}
}
}
class Newclass
{
public static Scanner x=new Scanner(System.in);
private String q;
private int i;
public void setQ()
{
this.q=x.next();
}
public void setI()
{
this.i=x.nextInt();
}
public boolean equals(Object obj)
{
if(this==obj) //an object must be equal to itself
return true;
if(this==null) //no object equals null
return false;
if(this.getClass()!=obj.getClass()) //objects of different types are never equal
return false;
Newclass x=(Newclass) obj;
return (this.q).equals(x.q) && (this.i)==(x.i);
}
}