Question: These are my 2 java classes Process and Schedule, can anyone fix these few errors? package scheduling; import java.util.*; public class Schedule{ private ArrayList que
These are my 2 java classes "Process" and "Schedule", can anyone fix these few errors?
package scheduling; import java.util.*; public class Schedule{ private ArrayList que = new ArrayList<>(); private static final String[][] processNames= {{"A","B","C","D","E"}, {"A","B","C","D","E"}, {"A","B","C","D"}}; private static final int[][] arrivalAt= {{0,2,4,6,8},{0,1,3,9,12},{0,1,2,3}}; private static final int[][] servTime= {{3,5,2,5,5},{3,5,2,5,5},{1,9,1,9}}; public Schedule(){getInput(0); } public void getInput(int row) { que.clear(); for (int j=0; j que.add(new Process (processNames[row][j], arrivalAt[row][j], servTime[row][j])); } sortQueByArrival(que); } public void init(){ Process.resetTimer(); for(int i=0; i que.get(i).init(); } } public Process isArrivingAt(int t){ for(int i=0; i if(t==que.get(i).arrival){ return que.get(i); } } return null; } public static void sortQueByService(ArrayList q){ Process p=null; for(int i=0; i p=q.get(i); for(int j=i+1; j if(p.service> q.get(j).service){ p=q.get(j); } } if(p!=q.get(i)){ q.remove(p); q.add(i,p); } } } public static Process getShortestRemainingTime(ArrayList q){ Process p=null; double timeLeft = Integer.MAX_VALUE; double left =0; int index = -1; for(int i=0; i p = q.get(i); if(p.arrival > currentTime || p.done){ continue; } left = (p.service - p.execute); if(timeLeft > left){ timeLeft = left; index = i; } } if(index >= 0){ return q.get(index); } return null; } public static Process getMaxResponseRatio(ArrayList q){ Process p=null; double responseRatio = -1; double r=0; int index = -1; for(int i=0; i p=q.get(i); if(p.arrival > currentTime || p.done){ continue; } r= (currentTime - p.arrival + p.service)/(double)p.service; if(responseRatio < r){ responseRatio = r; index = i; } } if(index >=0){ return q.get(index); } return null; } public static void sortQueByArrival (ArrayList q){ Process p=null; for(int i=0; i p=q.get(i); for(int j=i+1; j if(p.arrival>q.get(j).arrival){ p=q.get(j); } } if(p!=q.get(i)){ q.remove(p); q.add(i,p); } } } public String shortestProcessNext(){ StringBuffer buffer = new StringBuffer("SFN "); init(); ArrayList q = (ArrayList)(que.clone()); sortQueByService(q); int i=0; boolean idleProcessor = true; while (!q.isEmpty()){ idleProcessor=true; for(i=0; i if(Process.timer continue; } q.get(i).runNonpreempty(); idleProcessor = false; buffer.append(q.get(i)); q.remove(i); break; } if(idleProcessor){ Process.timer++; } } return buffer.toString(); } public String highestResponseRatioNext(){ StringBuffer buffer = new StringBuffer("HRRN "); init(); ArrayList q = (ArrayList)(que.clone()); Process p = null; while(! q.isEmpty()){ p=getMaxResponseRatio(q,Process.timer); if(p == null){ Process.timer++; continue; } p.runNonpreempty(); buffer.append(p); q.remove(p); } return buffer.toString(); } public String shortestRemainingServiceTimeNext(){ StringBuffer buffer = new StringBuffer("SRT "); init(); ArrayList q = (ArrayList)(que.clone()); Process p = null; while(! q.isEmpty()){ p=getShortestRemainingTime(q,Process.timer); if(p==null){ Process.timer++; continue; } q.remove(p); for(int t = p.execute; t p.run(1); if(p.done){ buffer.append(p); break; } Process newProcess = isArrivingAt(Process.timer); if(newProcess!=null && newProcess.service q.add(q.size()-1, p); break; } } } return buffer.toString(); } public String firstComeFirstServe(){ StringBuffer buffer = new StringBuffer("FCFS "); init(); for(int i = 0;i que.get(i).runNonpreempty(); buffer.append(que.get(i)); } return buffer.toString(); } public boolean allDone(){ for(int i=0; i if(!(que.get(i).done)){ return false; } } return true; } public String roundRobin(int g){ StringBuffer buffer = new StringBuffer("RR "); init(); int processorTimer =0; while(!allDone()){ for(int i =0; i Process.timer = processorTimer; if(que.get(i).done){ continue; } if(que.get(i).arrival > processorTimer){ break; } int timeSaved = que.get(i).run(g); if(timeSaved>0){ processorTimer -= timeSaved; } processorTimer += g; } } for(int i=0; i buffer.append(que.get(i)); } return buffer.toString(); } public static void main(String[] args){ Schedule s = new Schedule(); for(int i=0; i s.getInput(i); System.out.println("Input " + i ); System.out.println(s.firstComeFirstServe()+" "); System.out.println(s.shortestProcessNext()+" "); System.out.println(s.highestResponseRatioNext()+" "); System.out.println(s.shortestRemainingServiceTimeNext()+" "); System.out.println(s.roundRobin(i)); System.out.println(s.roundRobin(4)); } } }
Process class =
package scheduling;
public class Process { public String name; public int arrival; //arrival time public int service; //service time public int start=0; //start time public int finish=-1; //finish time public int execute=0; //execute time public boolean done = false; public boolean started = false; public static int timer = 0; //processor time in second
public Process(String n, int ariv, int serv){ name = n; arrival = ariv; service = serv; }
public void init(){ //undo execution start = 0; finish = -1; execute = 0; done = false; started = false; }
public static void resetTimer(){timer=0;} public int turnaround(){return finish - arrival;} public double ratio(){return turnaround()/(double)service;} public void runNonpreempty(){ start = timer; timer += service; finish = start + service; execute = service; started = done = true;
} public int waited(){ if(!done){ return timer - start - execute; } return finish - start - service; }
public int run(int time){//return prossor time saved if(done || arrival > timer){ return time; } if(!started){ start = timer; started = true; } int t = Math.min(time, service - execute); execute += t; timer += t; if(execute == service){ done = true; finish = timer; }
// System.out.println("timer="+timer+" "+this); return time - t; } @Override public String toString(){ return name +": Arrival "+arrival+";\tStart "+start+";\texecute " + execute +";\tFinishTime " + finish + ";\tTurnaround " + turnaround() + ";\tResponseRatio = " + ratio() + " "; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
