SSTF Draft
Draft pa lang pwede ko pa mapalitanuser_0616194
plain_text
7 months ago
4.5 kB
6
Indexable
Never
package os_final_project; import java.util.Arrays; import java.util.Scanner; public class SSTF { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the number of cylinders: "); int Cylinders = scanner.nextInt(); System.out.print("Enter the starting head position: "); int headStart = scanner.nextInt(); scanner.nextLine(); // consume newline character left in input stream int[] requests = null; boolean validInput = false; while (!validInput) { System.out.print("Enter the disk requests separated by spaces: "); String requestsInput = scanner.nextLine(); String[] requestStrings = requestsInput.split(" "); requests = new int[requestStrings.length]; validInput = true; for (int i = 0; i < requestStrings.length; i++) { try { int request = Integer.parseInt(requestStrings[i]); if (request < 0 || request >= Cylinders) { System.out.println("Error: Request " + request + " is out of range [0, " + (Cylinders-1) + "]"); validInput = false; break; } requests[i] = request; } catch (NumberFormatException e) { System.out.println("Error: Invalid request format: " + requestStrings[i]); validInput = false; break; } } } int[] traversalPath = calculateTraversalPath(headStart, requests); int seekTime = calculateShortestSeekTime(headStart, requests); printTraversalPath(traversalPath); System.out.println("Total head movement: " + seekTime); } public static int calculateShortestSeekTime(int headStart, int[] requests) { int totalSeekTime = 0; int currentPosition = headStart; int numRequests = requests.length; boolean[] visited = new boolean[numRequests]; for (int i = 0; i < numRequests; i++) { int shortestDistance = Integer.MAX_VALUE; int shortestIndex = -1; for (int j = 0; j < numRequests; j++) { if (!visited[j]) { int distance = Math.abs(requests[j] - currentPosition); if (distance < shortestDistance) { shortestDistance = distance; shortestIndex = j; } } } visited[shortestIndex] = true; totalSeekTime += shortestDistance; currentPosition = requests[shortestIndex]; } return totalSeekTime; } public static int[] calculateTraversalPath(int headStart, int[] requests) { int numRequests = requests.length; int[] traversalPath = new int[numRequests + 1]; traversalPath[0] = headStart; boolean[] visited = new boolean[numRequests]; for (int i = 0; i < numRequests; i++) { int shortestDistance = Integer.MAX_VALUE; int shortestIndex = -1; for (int j = 0; j < numRequests; j++) { if (!visited[j]) { int distance = Math.abs(requests[j] - traversalPath[i]); if (distance < shortestDistance || (distance == shortestDistance && j < shortestIndex)) { shortestDistance = distance; shortestIndex = j; } } } visited[shortestIndex] = true; traversalPath[i + 1] = requests[shortestIndex]; } return traversalPath; } public static void printTraversalPath(int[] traversalPath) { System.out.print("Traversal path: "); for (int i = 0; i < traversalPath.length - 1; i++) { int currentNode = traversalPath[i]; int nextNode = traversalPath[i+1]; int seekTime = Math.abs(nextNode - currentNode); System.out.println(currentNode + " -> " + nextNode + " [" + seekTime + "], "); } System.out.println(traversalPath[traversalPath.length-1]); } }