This repository has been archived by the owner on Mar 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StudentGoodHash.java
96 lines (79 loc) · 2.52 KB
/
StudentGoodHash.java
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package assign09;
import java.text.DecimalFormat;
/**
* This class provides a simple representation for a University of Utah student.
* Object's hashCode method is overridden with a correct hash function for this
* object, but one that does a poor job of distributing students in a hash
* table.
*
* @author Erin Parker, Nils Streedain & Paul Nuffer
* @version April 6, 2021
*/
public class StudentGoodHash {
private int uid;
private String firstName;
private String lastName;
/**
* Creates a new student with the specified uid, firstName, and lastName.
*
* @param uid
* @param firstName
* @param lastName
*/
public StudentGoodHash(int uid, String firstName, String lastName) {
this.uid = uid;
this.firstName = firstName;
this.lastName = lastName;
}
/**
* @return the UID for this student object
*/
public int getUid() {
return this.uid;
}
/**
* @return the first name for this student object
*/
public String getFirstName() {
return this.firstName;
}
/**
* @return the last name for this student object
*/
public String getLastName() {
return this.lastName;
}
/**
* @return true if this student and 'other' have the same UID, first name, and last name; false otherwise
*/
public boolean equals(Object other) {
// change to StudentMediumHash and StudentGoodHash for two new classes
if(!(other instanceof StudentGoodHash))
return false;
StudentGoodHash rhs = (StudentGoodHash) other;
return this.uid == rhs.uid && this.firstName.equals(rhs.firstName) && this.lastName.equals(rhs.lastName);
}
/**
* @return a textual representation of this student
*/
public String toString() {
DecimalFormat formatter = new DecimalFormat("0000000");
return firstName + " " + lastName + " (u" + formatter.format(uid) + ")";
}
public int hashCode() {
//This method utilizes the random distribution of uid, but combines it with
//adding the char values of all the chars from the first and last name strings,
//eliminating many hashing conflicts if a duplicate uid with different names
//exists. The majority of remaining hashing-caused conflicts will arise when there
//is a duplicate uid, and the both first and last names contain the same total number
//of each character. Minimal computation is done, so this should be very efficient still.
int hash = uid;
char[] firstArray = firstName.toCharArray();
char[] lastArray = lastName.toCharArray();
for (char curr : firstArray)
hash += curr;
for (char curr : lastArray)
hash += curr;
return hash;
}
}