package os_project;
import java.text.DecimalFormat;
import java.util.InputMismatchException;
import java.util.Scanner;
public class sample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
DecimalFormat format = new DecimalFormat ("##.##");
System.out.println("====================================================================================");
System.out.println("\t\tP R I O R I T Y S C H E D U L I N G A L G O R I T H M");
System.out.println("====================================================================================");
int job = 0;
boolean processInput = false;
//ask for number of processes
while (!processInput) {
//try catch to repeat while loop until user enters a valid number of processes
try {
System.out.print("\nEnter the number of processes: ");
job = sc.nextInt();
processInput = true;
} catch (InputMismatchException e) {
System.out.println("Please enter whole numbers only.");
sc.nextLine();
}
}
double Burst[] = new double[job];
int Priority[] = new int[job];
//ask for burst time and priority number of each process
for (int i = 0; i < job; i++) {
boolean burstInput = false;
boolean priorityInput = false;
System.out.println("\n------------------------------------- Process " + (i+1) + " ------------------------------------");
while (!burstInput) {
try {
System.out.print("\nBurst Time:\t");
Burst[i] = sc.nextDouble();
burstInput = true;
} catch (InputMismatchException e) {
System.out.println("\nPlease enter NUMBERS only!\n");
sc.nextLine(); //read invalid burst to not have an infinite loop
}
}
while (!priorityInput) {
try {
System.out.print("Priority:\t");
Priority[i] = sc.nextInt();
priorityInput = true; //continue the iteration
} catch (InputMismatchException e) {
System.out.println("\nPlease enter WHOLE NUMBERS only!\n");
sc.nextLine(); //read invalid burst to not have an infinite loop
}
}
}
//temporary array to store sorted priority Numbers and compare it to the unsorted priority array
int P1[] = new int[job];
//copy Priority elements to P1
for ( int i = 0; i < job; i++) {
P1[i] = Priority[i];
}
//bubble sort, sorts elements in P1 in ascending order
for (int i = 0; i < job; i++) {
for (int j = 0; j < job-1; j++) {
if(Priority[j] > Priority[j+1]) {
int temp = Priority[j];
Priority[j] = Priority[j+1];
Priority[j+1] = temp;
}
}
}
double wait = 0;
double turnTable = 0;
double waitingTime[] = new double[job];
double turnaround[] = new double[job];
float totalWait = 0;
float totalTurn = 0;
int k = 0;
int l = 0;
System.out.format("%63s","\n====================================================================================\n");
System.out.format("%15s %15s %15s %15s %18s", "Process", "Priority No.", "Burst Time", "Waiting Time", "Turnaround Time\n");
System.out.format("%63s","====================================================================================\n");
//Iterate on each of the priorities
for (int i = 0; i < job; i++) {
// Skip duplicate
if (i > 0 && Priority[i] == Priority[i-1]) {
continue;
}
// Iterate on all job
for (int j = 0; j < job; j++) {
if (Priority[i] == P1[j]) {
turnTable += Burst[j];
System.out.format("%12d %13d %17.2f %15.2f %15.2f\n", (j+1), P1[j], Burst[j], wait, turnTable);
waitingTime[k] = wait;
totalWait += wait;
wait += Burst[j];
turnaround[k] = waitingTime[k] + Burst[j];
totalTurn += turnaround[l];
}
}
}
System.out.println("\n\tAVERAGE WAITING TIME: " + format.format(totalWait/job) + "\t\tAVERAGE TURNAROUND TIME: " + format.format(totalTurn/job));
System.out.format("%63s","====================================================================================\n");
System.out.print("\nWould you like to restart the program?\nPress [Y] to restart or ANY KEY to terminate:\t ");
String restart = sc.next();
if (restart.equals('Y')) {
main(null);
} else {
System.out.println("\nProgram terminated. Thanks for using the program!");
}
}
}