Skip to content

Commit f383f18

Browse files
Update User.java
1 parent cf32e0a commit f383f18

1 file changed

Lines changed: 72 additions & 70 deletions

File tree

EqualsAndHashcode/User.java

Lines changed: 72 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ public class User
44
{
55
private String firstName;
66
public String lastName;
7-
7+
88
static void log(Object o)
9-
{
10-
System.out.print(o);
9+
{
10+
System.out.print(o);
1111
}
1212

13-
static void logln(Object o)
14-
{
15-
System.out.println(o);
16-
}
13+
static void logln(Object o)
14+
{
15+
System.out.println(o);
16+
}
1717

1818

1919

@@ -26,7 +26,7 @@ public String output()
2626
{
2727
return "Hi, my name is "+ getFirstName() + " " + getLastName() + ".";
2828
}
29-
29+
3030
//Now I will overload output
3131
public String output(boolean nice)
3232
{
@@ -37,7 +37,7 @@ public String output(boolean nice)
3737
}
3838
return "You\'re an idiot"+ " " + getFullName() + ".";
3939
}
40-
40+
4141
public String getFirstName()
4242
{
4343
return firstName;
@@ -51,7 +51,7 @@ public void setFirstName(String fN)
5151
//the .trim at the end will raise a null pointer exception because
5252
//it doesn't know what are we referencing
5353
//the .strip remove any whitespaces before output.
54-
54+
5555
}
5656

5757
public String getLastName()
@@ -65,7 +65,7 @@ public void setLastName(String lN)
6565
//Assigning the lN value we pass in to the field lastName
6666
lN=lastName;
6767
}
68-
68+
6969

7070
public static void printUsers(List<User> users)
7171
{
@@ -76,12 +76,12 @@ public static void printUsers(List<User> users)
7676
logln(o.getFullName());
7777
}
7878
}
79-
79+
8080
//Overload search and pass in this time a USER OBJECT
81-
public static int searchList(List<User> users, User u)
82-
{
83-
return searchList(users, u.getFullName());
84-
}
81+
public static int searchList(List<User> users, User u)
82+
{
83+
return searchList(users, u.getFullName());
84+
}
8585

8686
public static int searchList(List<User> users, String fN, String lN)
8787
{
@@ -106,65 +106,67 @@ public static int searchList(List<User> users, String fullName)
106106
}
107107

108108

109-
//Now I will override the ToString
110-
@Override
111-
public String toString()
112-
{
113-
//To override remember to give the method to override the same name
114-
return " " + getFullName() + "]";
115-
}
116-
117-
//Intended to put obj in a hash table ∴ turn the hash code into a number
118-
@Override
119-
public int hashCode()
120-
{
121-
final int prime = 19;
122-
int res = 1;
123-
res= prime * res + ((firstName==null) ? 0 : firstName.hashCode());
124-
res= prime * res + ((lastName==null) ? 0 : lastName.hashCode());
125-
return res;
126-
}
127-
128-
@Override
129-
public boolean equals(Object obj)
130-
{
131-
if(this == obj)
132-
{
133-
return true;
134-
}
135-
if(obj == null)
136-
{
137-
return false;
138-
}
139-
if(getClass() != obj.getClass())
140-
{
141-
return false;
142-
}
143-
144-
User other = (User) obj;
145-
146-
if(firstName == null){
147-
if(other.firstName != null)
148-
{
149-
return false;
150-
}
109+
//Now I will override the ToString
110+
@Override
111+
public String toString()
112+
{
113+
//To override remember to give the method to override the same name
114+
return " " + getFullName() + "]";
115+
}
116+
117+
//Intended to put obj in a hash table ∴ turn the hash code into a number
118+
@Override
119+
public int hashCode()
120+
{
121+
final int prime = 19;
122+
int res = 1;
123+
res= prime * res + ((firstName==null) ? 0 : firstName.hashCode());
124+
res= prime * res + ((lastName==null) ? 0 : lastName.hashCode());
125+
return res;
126+
}
127+
128+
@Override
129+
public boolean equals(Object obj)
130+
{
131+
if(this == obj)
132+
{
133+
return true;
134+
}
135+
if(obj == null)
136+
{
137+
return false;
138+
}
139+
if(getClass() != obj.getClass())
140+
{
141+
return false;
142+
}
143+
144+
User other = (User) obj;
145+
146+
if(firstName == null)
147+
{
148+
if(other.firstName != null)
149+
{
150+
return false;
151+
}
151152
}
152153
else if(!firstName.equals(other.firstName))
153154
{
154-
logln(false);
155-
156-
if(lastName == null)
157-
{
155+
logln(false);
156+
157+
if(lastName == null)
158+
{
158159
if(other.lastName != null)
159160
{
160-
return false;
161+
return false;
161162
}
162-
}
163-
else if(!lastName.equals(other.lastName))
164-
{
165-
return false;
166-
}
163+
164+
else if(!lastName.equals(other.lastName))
165+
{
166+
return false;
167+
}
168+
}
167169
}
168-
return true;
169-
}
170+
return true;
171+
}
170172
}

0 commit comments

Comments
 (0)