-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHW_Sched.java
executable file
·112 lines (90 loc) · 3.02 KB
/
HW_Sched.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
package A2;
import java.util.*;
class Assignment implements Comparator<Assignment>{
int number;
int weight;
int deadline;
protected Assignment() {
}
protected Assignment(int number, int weight, int deadline) {
this.number = number;
this.weight = weight;
this.deadline = deadline;
}
/**
* This method is used to sort to compare assignment objects for sorting.
* The way you implement this method will define which order the assignments appear in when you sort.
* Return 1 if a1 should appear after a2
* Return -1 if a1 should appear before a2
* Return 0 if a1 and a2 are equivalent
*/
@Override
public int compare(Assignment a1, Assignment a2) {
//YOUR CODE GOES HERE, DONT FORGET TO EDIT THE RETURN STATEMENT
if (a1.deadline == a2.deadline) {
if (a1.weight > a2.weight) {
return -1;
} else if (a1.weight < a2.weight) {
return 1;
} else {
return 0;
}
} else if (a1.deadline > a2.deadline) {
return 1;
} else
return -1;
}
}
public class HW_Sched {
ArrayList<Assignment> Assignments = new ArrayList<Assignment>();
int m;
int lastDeadline = 0;
protected HW_Sched(int[] weights, int[] deadlines, int size) {
for (int i=0; i<size; i++) {
Assignment homework = new Assignment(i, weights[i], deadlines[i]);
this.Assignments.add(homework);
if (homework.deadline > lastDeadline) {
lastDeadline = homework.deadline;
}
}
m =size;
}
/**
*
* @return Array where output[i] corresponds to when assignment #i will be completed. output[i] is 0 if assignment #i is never completed.
* The homework you complete first will be given an output of 1, the second, 2, etc.
*/
public int[] SelectAssignments() {
//Use the following command to sort your Assignments:
//Collections.sort(Assignments, new Assignment());
//This will re-order your assignments. The resulting order will depend on how the compare function is implemented
Collections.sort(Assignments, new Assignment());
//Initializes the homeworkPlan, which you must fill out and output
// Initializes the homeworkPlan, which you must fill out and output
int[] homeworkPlan = new int[Assignments.size()];
int other = 0;
for (int x = 0; x < Assignments.size(); x++) {
Assignment Assignment1 = Assignments.get(x);
if (x == 0) {
other++;
homeworkPlan[Assignment1.number] = other;
} else if (x < Assignments.size() - 1) {
Assignment Assignment2 = Assignments.get(x + 1);
if ( other < Assignment2.deadline && Assignment2.deadline == Assignment1.deadline ) {
other++;
homeworkPlan[Assignment1.number] = other;
}
if (Assignment2.deadline != Assignment1.deadline) {
other++;
homeworkPlan[Assignment2.number] = other;
}
} else {
if (other < Assignment1.deadline) {
other++;
homeworkPlan[Assignment1.number] = other;
}
}
}
return homeworkPlan;
}
}