Sunday, November 29, 2009

shortest seek time first (SSTF) java source code

import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.Math;



public class Main {


public static void main(String[] args) {


ArrayList list = new ArrayList();


BufferedReader dataIn = new BufferedReader(new
InputStreamReader( System.in) );


//read current head position
String w = "";
System.out.println("Position of head currently :");
try{
w = dataIn.readLine();
}catch( IOException e ){
System.out.println("Error!");
}
int head = Integer.parseInt(w);
int initialhead = head;


// Read service requests
while(true){
try{
System.out.println("Enter next service request.Enter z to end the requests.");
w = dataIn.readLine();

if(w.equals("z"))
break;
int temp = Integer.parseInt(w);
list.add(temp);
}catch( IOException e ){
System.out.println("Error!");
}

}


ArrayList service_order = new ArrayList();//adds requests in order of the service

int number_of_orders = list.size();
for(int i=0; i &ltnumber_of_orders;i++){
Iterator it = list.iterator();
int min = 0;
int temp=0;
int min_distance=200;

while(it.hasNext()){
temp = (Integer)it.next();
int distance = Math.abs(head-temp);
System.out.println(head +" "+ temp+" " + distance );

if(distance < min_distance){
min = temp;
min_distance = distance;
}

}

list.remove((Integer)min);
head = min;
System.out.println(" Next request "+ min);
service_order.add(min);


}

Iterator min_it = service_order.iterator();

System.out.print("Service Order: ");

while(min_it.hasNext()){
System.out.print(min_it.next()+" ");
}
int total_head_movement=0;
min_it = service_order.iterator();
total_head_movement = Math.abs(initialhead - (Integer)service_order.get(0));
for(int i =1;i < service_order.size();i++){
total_head_movement += Math.abs((Integer)service_order.get(i) - (Integer)service_order.get(i-1));
}
System.out.println();

System.out.println("Total head movement: "+ total_head_movement);
}

}




Deepcopy of a string in c

2 comments:

  1. can we get the package sstf as it is not given in this code. ?

    ReplyDelete
  2. excellent code, I used it for an assignment thanks man

    ReplyDelete