-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiskCalculator.java
More file actions
143 lines (118 loc) · 4.19 KB
/
DiskCalculator.java
File metadata and controls
143 lines (118 loc) · 4.19 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
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
142
143
/**
Author: Rajin Santos Gajadhar
Student ID: 239479650
Assignment 5, Question 7
Any and all work in this file is my own.
*/
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
public class DiskCalculator {
//FCFS Algorithm
public static int fcfs(List<Integer> queue, int head) {
int totalDistance = 0;
int currentPosition = head;
for (int request : queue) {
totalDistance += Math.abs(currentPosition - request);
currentPosition = request;
}
return totalDistance;
}
//SCAN Algorithm
public static int scan(List<Integer> queue, int head, int limit) {
int totalDistance = 0;
queue.sort(Integer::compareTo);
List<Integer> left = new ArrayList<>();
List<Integer> right = new ArrayList<>();
for (int request : queue) {
if (request < head) {
left.add(request);
} else {
right.add(request);
}
}
//Move towards 0, then to limit
for (int i = left.size() - 1; i >= 0; i--) {
totalDistance += Math.abs(head - left.get(i));
head = left.get(i);
}
totalDistance += head; //Head moves to 0
head = 0;
for (int request : right) {
totalDistance += Math.abs(head - request);
head = request;
}
return totalDistance;
}
//SSTF Algorithm
public static int sstf(List<Integer> queue, int head) {
int totalDistance = 0;
List<Integer> requests = new ArrayList<>(queue);
int size = requests.size();
for (int i = 0; i < size; i++) {
int closest = findClosest(requests, head);
totalDistance += Math.abs(head - closest);
head = closest;
requests.remove(Integer.valueOf(closest));
}
return totalDistance;
}
//Helper to find closest request for SSTF
private static int findClosest(List<Integer> requests, int head) {
int minDistance = Integer.MAX_VALUE;
int closest = head;
for (int request : requests) {
int distance = Math.abs(request - head);
if (distance < minDistance) {
minDistance = distance;
closest = request;
}
}
return closest;
}
//C-SCAN Algorithm
public static int cscan(List<Integer> queue, int head, int limit) {
int totalDistance = 0;
queue.sort(Integer::compareTo);
List<Integer> left = new ArrayList<>();
List<Integer> right = new ArrayList<>();
for (int request : queue) {
if (request < head) {
left.add(request);
} else {
right.add(request);
}
}
//Move towards limit, then jump to 0
for (int request : right) {
totalDistance += Math.abs(head - request);
head = request;
}
totalDistance += limit - head + limit; //Jump to 0
head = 0;
for (int request : left) {
totalDistance += Math.abs(head - request);
head = request;
}
return totalDistance;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter current position: ");
int head = scanner.nextInt();
System.out.print("Enter the disk size cylinders (e.g.3000): ");
int limit = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter the disk queue (separate by 1 space): ");
String[] queueInput = scanner.nextLine().split(" ");
List<Integer> queue = new ArrayList<>();
for (String s : queueInput) {
queue.add(Integer.parseInt(s.trim()));
}
System.out.println("Total distance travelled for FCFS: " + fcfs(queue, head));
System.out.println("Total distance travelled for SCAN: " + scan(queue, head, limit));
System.out.println("Total distance travelled for SSTF: " + sstf(queue, head));
System.out.println("Total distance travelled for C-SCAN: " + cscan(queue, head, limit));
scanner.close();
}
}