-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMan.java
65 lines (50 loc) · 1.11 KB
/
Man.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
import java.util.*;
import java.lang.Object.*;
public class Man extends Human{
private final ArraySet<Woman> PREF;
private Woman partner;
public Man(String name, ArraySet<Woman> pref){
super(name);
PREF = pref;
partner = null;
}
public boolean isMarried(){
return partner != null;
}
public ArraySet<Woman> getPref(){
return PREF;
}
public Woman getPartner(){
return partner;
}
public void setPartner(Woman x){
partner = x;
}
// Rank "O" is top rank
// O(n)
public int getRankof(Woman woman){
int i = 0;
Iterator itr = PREF.iterator();
while (itr.hasNext()){
if (itr.next().equals(woman))
return i;
else
i++;
}
return - 1;
}
}
/*
Round 1
Bob, MaryAnn X you set the man to their first pref.
Jack, MaryAnn MaryAnn refects Bob
Ping, Ginger X
Round 2
Bob, Ginger Bob goes to his 2nd choice
Round 3
Ping MaryAnn X Ginger rejects Ping
Roudn 4 Ping goes to his 2nd choice
Ping, Sarah
end: marriage is stable
Names: 1,2,3....n
*/