-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScheduler.java
70 lines (61 loc) · 2.22 KB
/
Scheduler.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
/* COMP2240 Assignment 1
* File: Scheduler.java
* Author: Nicholas Steuart c3330826
* Date Created: 12/8/24
* Date Last Modified: 5/9/24
* Description: Parent Class of the scheduling algorithms that records and runs the CPU Scheduling algorithms
*
*/
// PACKAGES //
import java.util.ArrayList;
abstract class Scheduler
{
// CLASS VARIABLES //
protected ArrayList<String> dispatchLogs = new ArrayList<String>(); //Stores logs of the time and Process object ID of when it was dispatched by a scheduler
// ABSTRACT METHODS //
abstract public Process dispatch();
abstract public void admit();
abstract public void run();
abstract public String getName();
abstract public ArrayList<Process> getEnterQueue();
abstract public ArrayList<String> getDispatchLogs();
// CONCRETE METHODS //
//PRE-CONDITION: No PRE-CONDITION
//POST-CONDITION: String dispatchLogs returned containing the logs of a Scheduler's dispatch
public String printDispatchLogs()
{
String dispatchlogs = getName() + ":\n";
for(String log: this.getDispatchLogs())
{
dispatchlogs += log + "\n";
}
dispatchlogs += "\n";
resetDispatchLogs(); //Avoids the next Scheduler's dispatch logs containing this Scheduler's dispatch logs
return dispatchlogs;
}
//PRE-CONDITION: No PRE-CONDITION
//POST-CONDITION: All items from dispatchLogs removed
public void resetDispatchLogs()
{
dispatchLogs.clear();
}
//PRE-CONDITION: No PRE-CONDITION
//POST-CONDITION: String returned containing the average turnTime or waitTime of a scheduler in 2 decimal place formatting
public String getAverage(String dataType)
{
int sum = 0;
for(Process process: this.getEnterQueue())
{
if(dataType == "turnTime") //IF we are finding average turnaround time
{
sum += process.getTurnTime();
}
else //Else we are calculating wait time.
{
sum += process.getWaitTime();
}
}
double average = (double)sum / this.getEnterQueue().size(); //Calculate average
return String.format("%.2f", average);
}
}