Question: need java code to finish two classesSJF.java and RR.java SJF.java import java.util.*; public class SJF implements Algorithm { } RR.java import java.util.*; public class RR
need java code to finish two classesSJF.java and RR.java
SJF.java
import java.util.*;
public class SJF implements Algorithm {
}
RR.java
import java.util.*;
public class RR implements Algorithm {
}
And these two classes are related to other classes. Below are other classes that have been written
Driver.java
import java.util.*; import java.io.*; public class Driver { public static void main(String[] args) throws IOException { if (args.length != 2) { System.err.println("Usage: java Driver "); System.exit(0); } BufferedReader inFile = new BufferedReader(new FileReader(args[1])); String schedule; // create the queue of tasks List queue = new ArrayList<>(); // read in the tasks and populate the Ready queue while ( (schedule = inFile.readLine()) != null) { String[] params = schedule.split(",\\s*"); queue.add(new Task(params[0], Integer.parseInt(params[1]), Integer.parseInt(params[2]))); } inFile.close(); Algorithm scheduler = null; String choice = args[0].toUpperCase(); switch(choice) { case "FCFS": scheduler = new FCFS(queue); break; case "SJF": scheduler = new SJF(queue); break; case "RR": scheduler = new RR(queue); break; default: System.err.println("Invalid algorithm"); System.exit(0); } // start the scheduler scheduler.schedule(); } } FCFS.java
import java.util.*; public class FCFS implements Algorithm { private final List queue; //the total number of processes to be scheduled private final int numTasks; public FCFS(List queue) { this.queue = queue; numTasks = queue.size(); } public void schedule() { System.out.println("First-Come, First-Served Scheduling "); //to keep track of the total waiting time int totalWaitingTime = 0; Task currentTask; while (!queue.isEmpty()) { currentTask = pickNextTask(); //the waiting time for a process in FCFS is the time it is given the CPU minus its arrival time int wTime = 0; if (CPU.getCurrentTime() > currentTask.getArrivalTime()){ wTime = CPU.getCurrentTime() - currentTask.getArrivalTime(); } totalWaitingTime += wTime; CPU.run(currentTask, currentTask.getBurst()); System.out.println(currentTask.getName() + " finished at time "+CPU.getCurrentTime() + ". Its waiting time is: " + wTime); // remove the completed process queue.remove(currentTask); } //need to cast either the numerator or the denominator to the double type //otherwise, when both are integers, their division result will always be rounded to integer double averageWaitingTime = totalWaitingTime / (double) numTasks; //use printf for formatted out (only show two digits after decimal points) System.out.printf(" The average waiting time is: %.2f ", averageWaitingTime); } //select the next process to be executed by the CPU. public Task pickNextTask() { return queue.get(0); } } Algorithm.java
public interface Algorithm { /** * Invokes the scheduler */ void schedule();
/** * Selects the next task using the appropriate scheduling algorithm */ Task pickNextTask(); }
import java.util.*; public class FCFS implements Algorithm { private final List queue; //the total number of processes to be scheduled private final int numTasks; public FCFS(List queue) { this.queue = queue; numTasks = queue.size(); } public void schedule() { System.out.println("First-Come, First-Served Scheduling "); //to keep track of the total waiting time int totalWaitingTime = 0; Task currentTask; while (!queue.isEmpty()) { currentTask = pickNextTask(); //the waiting time for a process in FCFS is the time it is given the CPU minus its arrival time int wTime = 0; if (CPU.getCurrentTime() > currentTask.getArrivalTime()){ wTime = CPU.getCurrentTime() - currentTask.getArrivalTime(); } totalWaitingTime += wTime; CPU.run(currentTask, currentTask.getBurst()); System.out.println(currentTask.getName() + " finished at time "+CPU.getCurrentTime() + ". Its waiting time is: " + wTime); // remove the completed process queue.remove(currentTask); } //need to cast either the numerator or the denominator to the double type //otherwise, when both are integers, their division result will always be rounded to integer double averageWaitingTime = totalWaitingTime / (double) numTasks; //use printf for formatted out (only show two digits after decimal points) System.out.printf(" The average waiting time is: %.2f ", averageWaitingTime); } //select the next process to be executed by the CPU. public Task pickNextTask() { return queue.get(0); } } public interface Algorithm { /** * Invokes the scheduler */ void schedule();
/** * Selects the next task using the appropriate scheduling algorithm */ Task pickNextTask(); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
