This repository was 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
/
Copy pathStudentBadHash.java
81 lines (68 loc) · 1.98 KB
/
StudentBadHash.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
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 StudentBadHash {
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 StudentBadHash(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 StudentBadHash))
return false;
StudentBadHash rhs = (StudentBadHash) 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() {
//adds length of first and last name. This will hopefully not be too bad as to not run in
//timing, but will definitely cause many collisions
return firstName.length() + lastName.length();
}
}