-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathName
More file actions
61 lines (54 loc) · 1.63 KB
/
Name
File metadata and controls
61 lines (54 loc) · 1.63 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
52
53
54
55
56
57
58
59
60
61
public class Name implements Comparable {
private String first;
private String last;
public Name(String firstName, String lastName)
{
first = firstName;
last = lastName;
}
public String getFirstName() {
return first;
}
public String getLastName() {
return this.last;
}
public int compareTo(Object name) {
// -ve means: this name comes after other name
// +ve means: this name comes before other name
Name name1 = (Name) name;
//Name name2 = new Name(first, last);
//name1 goes before
if (name1.getLastName().compareTo(this.getLastName())<0) {
//name1 comes after name2
return -1;
}
else if (name1.getLastName().compareTo(this.getLastName())>0){
//name1 comes before name2
return 1;
}
else {
if (name1.getFirstName().compareTo(this.getFirstName())<0) {
//name1 comes after name2
return -1;
}
else if (name1.getFirstName().compareTo(this.getFirstName())>0) {
//name1 comes before name2
return 1;
}
else {
//the two names are the same
return 0;
}
}
}
public String toString() {
return first + " " + last;
}
}
public class Driver {
public static void main(String[] args) {
Name name1 = new Name("Alice", "Kang");
Name name2 = new Name("Bella", "Kang");
System.out.println(name1.compareTo(name2));
}
}