C look Algorithm
unknown
plain_text
a year ago
5.0 kB
19
Indexable
Never
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; import javax.sound.midi.Soundbank; import javax.swing.plaf.ColorUIResource; public class Clook { public static ArrayList<String> order = new ArrayList<>(); public static ArrayList<Integer> fragments = new ArrayList<>(); public static int cylinder = 200; public static int headstart = 53; public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("\n===== C-Look Algorithm =====\n"); System.out.println(" [1] Predefined \n [2] User-Defined"); int status = in.nextInt(); in.nextLine(); switch(status){ case 1: fragments.add(98); fragments.add(183); fragments.add(37); fragments.add(122); fragments.add(14); fragments.add(124); fragments.add(65); fragments.add(67); fragments.add(headstart); goingUp(); goingDown(); break; case 2: System.out.println("Cylinder Size: "); cylinder = in.nextInt();in.nextLine(); System.out.println("Number of Fragments: "); int fragSize = in.nextInt();in.nextLine(); System.out.println("Headstart: "); headstart = in.nextInt();in.nextLine(); fragments.add(headstart); for(int i = 1; i <= fragSize; i++){ System.out.println("Enter fragment's address " + i + " : " ); int frag = in.nextInt();in.nextLine(); if(frag>cylinder){ System.out.println("Address exceeded the cylinder size, enter valid address."); i = i-1; } else fragments.add(frag); } goingUp(); goingDown(); break; } } static void header(){ Collections.sort(fragments); System.out.print("0" + " \t"); for(int i = 0; i < fragments.size(); i++){ System.out.print(fragments.get(i) + " \t"); order.add(" "); } System.out.print(cylinder-1 + " \t"); } static void goingDown(){ System.out.println("\n======= GOING DOWN ======="); header(); ArrayList<Integer> distances = new ArrayList<>(); int current = fragments.indexOf(headstart); for(int i = 0; i < fragments.size(); i++){ if(current < 0){ current = fragments.size()-1; } order.set(current, Integer.toString(i+1)); System.out.println("\n"); System.out.print(" " + "\t"); for (int j = 0; j < order.size(); j++) { System.out.print(order.get(j) + " \t"); } int distance; if(current < fragments.size()-1){ distance = fragments.get(current) - fragments.get(current+1); } else distance = fragments.get(current) - fragments.get(0); distances.add(i, Math.abs(distance)); order.set(current, " "); current--; } distances.remove(0); System.out.println("\n\n Distances: " + distances.toString()); int thm = 0; for(int i = 0; i < distances.size(); i++){ thm = thm + distances.get(i); } System.out.println("Total Head Movement: " + thm + "\n\n"); } static void goingUp(){ System.out.println("\n======= GOING UP ======="); header(); ArrayList<Integer> distances = new ArrayList<>(); int current = fragments.indexOf(headstart); for(int i = 0; i < fragments.size(); i++){ if(current >= fragments.size()){ current = 0; } order.set(current, Integer.toString(i+1)); System.out.println("\n"); System.out.print(" " + "\t"); for (int j = 0; j < order.size(); j++) { System.out.print(order.get(j) + " \t"); } int distance; if(current > 0){ distance = fragments.get(current) - fragments.get(current-1); } else distance = fragments.get(current) - fragments.get(fragments.size()-1); distances.add(i, Math.abs(distance)); order.set(current, " "); current++; } distances.remove(0); System.out.println("\n\n Distances: " + distances.toString()); int thm = 0; for(int i = 0; i < distances.size(); i++){ thm = thm + distances.get(i); } System.out.println("Total Head Movement: " + thm + "\n\n"); } }