Related questions
Concept explainers
ERROR IN THIS CODE!!
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
class Task {
String name;
int priority;
int burstTime;
int arrivalTime;
Task(String name, int priority, int burstTime) {
this.name = name;
this.priority = priority;
this.burstTime = burstTime;
}
}
public class SchedulingAlgorithms {
private static List<Task> taskSet;
public static void main(String[] args) {
initializeTaskSet();
// FCFS
Thread fcfsThread = new Thread(() -> {
System.out.println("FCFS Schedule:");
executeFCFS();
});
// SJF
Thread sjfThread = new Thread(() -> {
System.out.println("\nSJF Schedule:");
executeSJF();
});
// Priority Scheduling
Thread priorityThread = new Thread(() -> {
System.out.println("\nPriority Schedule:");
executePriority();
});
// Round-Robin
Thread rrThread = new Thread(() -> {
System.out.println("\nRound-Robin Schedule:");
executeRoundRobin();
});
fcfsThread.start();
sjfThread.start();
priorityThread.start();
rrThread.start();
try {
fcfsThread.join();
sjfThread.join();
priorityThread.join();
rrThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static void initializeTaskSet() {
taskSet = new ArrayList<>();
taskSet.add(new Task("T1", 2, 20));
taskSet.add(new Task("T2", 4, 25));
taskSet.add(new Task("T3", 3, 25));
taskSet.add(new Task("T4", 3, 15));
taskSet.add(new Task("T5", 1, 10));
// Assign random arrival times in the range [0, 100]
Random random = new Random();
for (Task task : taskSet) {
task.arrivalTime = random.nextInt(101);
}
// Sort tasks based on arrival time
Collections.sort(taskSet, (t1, t2) -> Integer.compare(t1.arrivalTime, t2.arrivalTime));
}
private static void executeFCFS() {
int currentTime = 0;
int totalWaitTime = 0;
int totalTurnaroundTime = 0;
for (Task task : taskSet) {
int waitTime = currentTime - task.arrivalTime;
totalWaitTime += waitTime;
int turnaroundTime = waitTime + task.burstTime;
totalTurnaroundTime += turnaroundTime;
System.out.println(task.name + ": " + currentTime + " - " + (currentTime + task.burstTime));
currentTime += task.burstTime;
}
printAverages(totalWaitTime, totalTurnaroundTime);
}
private static void executeSJF() {
// Sort tasks based on burst time
Collections.sort(taskSet, (t1, t2) -> Integer.compare(t1.burstTime, t2.burstTime));
executeFCFS(); // SJF is essentially FCFS after sorting by burst time
}
private static void executePriority() {
int currentTime = 0;
int totalWaitTime = 0;
int totalTurnaroundTime = 0;
for (Task task : taskSet) {
int waitTime = currentTime - task.arrivalTime;
totalWaitTime += waitTime;
int turnaroundTime = waitTime + task.burstTime;
totalTurnaroundTime += turnaroundTime;
System.out.println(task.name + ": " + currentTime + " - " + (currentTime + task.burstTime));
currentTime += task.burstTime;
}
printAverages(totalWaitTime, totalTurnaroundTime);
}
private static void executeRoundRobin() {
int currentTime = 0;
int totalWaitTime = 0;
int totalTurnaroundTime = 0;
int timeQuantum = 10;
List<Task> remainingTasks = new ArrayList<>(taskSet);
while (!remainingTasks.isEmpty()) {
for (Task task : new ArrayList<>(remainingTasks)) {
int remainingBurstTime = Math.min(task.burstTime, timeQuantum);
int waitTime = currentTime - task.arrivalTime;
totalWaitTime += waitTime;
int turnaroundTime = waitTime + remainingBurstTime;
totalTurnaroundTime += turnaroundTime;
System.out.println(task.name + ": " + currentTime + " - " + (currentTime + remainingBurstTime));
currentTime += remainingBurstTime;
task.burstTime -= remainingBurstTime;
if (task.burstTime == 0) {
remainingTasks.remove(task);
}
}
}
printAverages(totalWaitTime, totalTurnaroundTime);
}
private static void printAverages(int totalWaitTime, int totalTurnaroundTime) {
double averageWaitTime = (double) totalWaitTime / taskSet.size();
double averageTurnaroundTime = (double) totalTurnaroundTime / taskSet.size();
System.out.println("\nAverage Waiting Time: " + averageWaitTime);
System.out.println("Average Turnaround Time: " + averageTurnaroundTime);
}
}
Step by stepSolved in 4 steps with 1 images
- StringFun.java import java.util.Scanner; // Needed for the Scanner class 2 3 /** Add a class comment and @tags 4 5 */ 6 7 public class StringFun { /** * @param args not used 8 9 10 11 12 public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Please enter your first name: "); 13 14 15 16 17 18 System.out.print("Please enter your last name: "); 19 20 21 //Output the welcome message with name 22 23 24 //Output the length of the name 25 26 27 //Output the username 28 29 30 //Output the initials 31 32 33 //Find and output the first name with switched characters 34 //All Done! } } 35 36 37arrow_forwardusing System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LibraryManagment { class Program {// private static int userInput;// need varaible to capture user input = int public static string Title { get; private set; } public static bool IsChecked { get; private set; } //Implement a menu system that allows users to add books, check out books, //and check in books using the console. Use Console.ReadLine() ///to capture user input and Console.WriteLine() for output. static void Main(string[] args) {// provides a cosole interaction for a user while (IsChecked) { Console.WriteLine("\nMenu\n" + "1)Add book\n" + "2)Check Out Book\n" + "3)Check In Book\n" + "4)Exit\n"); Console.Write("Choose your...arrow_forwardimport java.util.Scanner; public class Playlist { // TODO: Write method to ouptut list of songs public static void main (String[] args) { Scanner scnr = new Scanner(System.in); SongNode headNode; SongNode currNode; SongNode lastNode; String songTitle; int songLength; String songArtist; // Front of nodes list headNode = new SongNode(); lastNode = headNode; // Read user input until -1 entered songTitle = scnr.nextLine(); while (!songTitle.equals("-1")) { songLength = scnr.nextInt(); scnr.nextLine(); songArtist = scnr.nextLine(); currNode = new SongNode(songTitle, songLength, songArtist); lastNode.insertAfter(currNode); lastNode = currNode; songTitle = scnr.nextLine(); } // Print linked list...arrow_forward
- JavaTimer.java: import java.util.Arrays;import java.util.Random; public class JavaTimer { // Please expand method main() to meet the requirements.// You have the following sorting methods available:// insertionSort(int[] a);// selectionSort(int[] a);// mergeSort(int[] a);// quickSort(int[] a);// The array will be in sorted order after the routines are called!// Be sure to re-randomize the array after each sort.public static void main(String[] args) {// Create and initialize arraysint[] a = {1, 3, 5}, b, c, d;// Check the time to sort array along startTime = System.nanoTime();quickSort(a);long endTime = System.nanoTime();long duration = (endTime - startTime) / 1000l;// Output resultsSystem.out.println("Working on an array of length " + a.length + ".");System.out.println("Quick sort: " + duration + "us.");}// Thanks to https://www.javatpoint.com/insertion-sort-in-javapublic static void insertionSort(int array[]) {int n = array.length;for (int j = 1; j < n; j++) {int key = array[j];int...arrow_forwardimport java.util.Scanner; public class LabProgram { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); int n = scnr.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = scnr.nextInt(); } for (int i = n - 1; i >= 0; i--) { System.out.print(arr[i]); if (i > 0) { System.out.print(","); } } }}arrow_forwardimport java.util.* ; public class Averager { public static void main(String[] args) { //a two dimensional array int[][] a = {{5, 9, 3, 2, 14}, {77, 44, 22, 15, 99}, {14, 2, 3, 9, 5}, {88, 15, 17, 121, 33}} ; System.out.printf("Average = %.2f\n", average(a)) ; System.out.println("EXPECTED:") ; System.out.println("Average = 29.85") ; System.out.printf("Average of evens = %.2f\n", averageEvens(a)) ; System.out.println("EXPECTED:") ; System.out.println("Average of evens = 26.57") ; Random random = new Random(1) ; a = new int[100][1] ; for (int i = 0 ; i < 100 ; i++) a[i][0] = random.nextInt(1000) ; System.out.println("For an array of random values in range 0 to 999:") ; System.out.printf("Average = %.2f\n", average(a)) ; System.out.printf("Average of evens = %.2f\n", averageEvens(a)) ; } /** Find the average of all elements of a two-dimensional array @param aa the two...arrow_forward
- Text book imageDatabase System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationText book imageStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONText book imageDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- Text book imageC How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONText book imageDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningText book imageProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education