-
Notifications
You must be signed in to change notification settings - Fork 1
/
Activity.java
143 lines (121 loc) · 4.22 KB
/
Activity.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* A simple class which represents an activity or a task which has start time
* and an end time. The time is represented as integers which can be
* easily replaced with actual inbuilt data and time types
*/
import java.util.Arrays;
import java.util.Comparator;
import java.util.Random; // only test in main
// Immutable class for Activity or task
public class Activity {
private final int start;
private final int end;
public static final Comparator<Activity> BY_START_TIME = new StartOrder();
public static final Comparator<Activity> BY_END_TIME = new EndOrder();
public Activity(int s, int e) {
start = s;
end = e;
}
// Compare activity according to start time
private static class StartOrder implements Comparator<Activity> {
public int compare(Activity a, Activity b) {
if (a.start < b.start) {
return -1;
} else if (a.start > b.start) {
return 1;
} else {
return 0;
}
}
}
// Compare activity according to end time
private static class EndOrder implements Comparator<Activity> {
public int compare(Activity a, Activity b) {
if (a.end < b.end) {
return -1;
} else if (a.end > b.end) {
return 1;
} else {
return 0;
}
}
}
public int getStart() {
return start;
}
public int getEnd() {
return end;
}
// Is this activity overlapping with other
// if start of one has same time as end of another,
// then acitivites are considered as overlapping
public boolean overlappingInclusive(Activity other) {
if (other == null) {
throw new IllegalArgumentException();
}
return (this.start <= other.end) && (other.start <= this.end);
}
// Is this activity overlapping with other
// if start of one has same time as end of another,
// then acitivites are considered as non overlapping
public boolean overlappingExclusive(Activity other) {
if (other == null) {
throw new IllegalArgumentException();
}
return (this.start < other.end) && (other.start < this.end);
}
// Returns an integer hash code for this object
@Override
public int hashCode() {
int hashStart = ((Integer)start).hashCode();
int hashEnd = ((Integer)end).hashCode();
return 31 * hashStart + hashEnd;
}
// Compare this acitivty to the other activity
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if (other == null || other.getClass() != this.getClass()) {
return false;
}
Activity that = (Activity) other;
return (this.start == that.start) && (this.end == that.end);
}
// Return string representation of this object
@Override
public String toString() {
return new String("(" + start + "," + end + ")");
}
public static void main(String[] args) {
// Generate n random start and end time within range
int n = 10;
int minStart = 10;
int maxEnd = 200;
Random rand = new Random();
Activity[] activities = new Activity[n];
for (int i = 0; i < n; ++i) {
int start = minStart + rand.nextInt(maxEnd + 1);
int end = minStart + rand.nextInt(maxEnd + 1);
activities[i] = new Activity(start, end);
}
System.out.println("Activities: ");
for (int i = 0; i < activities.length; ++i) {
System.out.print(activities[i] + " ");
}
System.out.println("");
System.out.println("Activities sorted by start time: ");
Arrays.sort(activities, Activity.BY_START_TIME);
for (int i = 0; i < activities.length; ++i) {
System.out.print(activities[i] + " ");
}
System.out.println("");
System.out.println("Activities sorted by end time: ");
Arrays.sort(activities, Activity.BY_END_TIME);
for (int i = 0; i < activities.length; ++i) {
System.out.print(activities[i] + " ");
}
System.out.println("");
}
}