I\'m a beginner in JAVA: The Process Scheduler relies on a scheduling algorithm,
ID: 3718940 • Letter: I
Question
I'm a beginner in JAVA: The Process Scheduler relies on a scheduling algorithm, based on a specific scheduling policy, to allocate the CPU in the best way to move jobs through the system efficiently. Write a Java program that will simulate the following process scheduling algorithm: 2. Shortest Job Next (SJN) Your Java program should produce tables to presents computed results for each scheduling algorithm as shown in the following sample data for the validation your program’s computed results for each scheduling algorithm.
B) Timeline for Shortest Job Next (SJN) algorithm: 12 15 20 Turnaround Time ArrivalCPU Start Finish Time Cycle TimeTime Finish Time- Arrival Time 15 9 12 7 7-0-7 20 20-1=19 12 12-2- 10 15 115-3=12 3 2 9 9-4 5 Average Turnaround Time (7+19 1012 +5)/5-53/5 10.6Explanation / Answer
PROGRAM
import java.util.Scanner;
// Create class SJN
class SJN
{
// create and implement readData() method
public static void readData(int a[],int size)
{
Scanner s=new Scanner(System.in); // Create Scanner Object s
for(int i=0;i<size;i++) // create for loop until size of array elements
a[i]=s.nextInt(); // read array elements
}
public static void main(String args[])
{
// declare integer array variable and number of process n
int aTime[],cycle[],sTime[],fTime[],tTime[],n;
double tat; // delcare double variable
Scanner scr=new Scanner(System.in); // create Scanner object scr
System.out.print("Enter Number of Process: ");
n=scr.nextInt(); // read number of process
// create array elements
aTime=new int[n];
cycle=new int[n];
sTime=new int[n];
fTime=new int[n];
tTime=new int[n];
System.out.println(" Enter Arrival Time ");
readData(aTime,n); // read arrival time
System.out.println(" Enter CPU Cycle Time ");
readData(cycle,n); // read cpu cycle time
System.out.println(" Enter Start Time ");
readData(sTime,n); // read start time
for(int i=0;i<n;i++)
fTime[i]=sTime[i]+cycle[i]; // calculate finish Time
System.out.println(" Calculating Turnaround Time");
for(int i=0;i<n;i++)
{
tTime[i]=fTime[i]-aTime[i]; // calculate turnaround time
}
int sum=0; // declare and initialize integer variable sum=0
for(int i=0;i<n;i++)
sum+=tTime[i]; // calculate sum of turnaround time
tat=(double)sum/n; // calculate turnaround time
int asc=65; // declare constant integer variable asc=65
// display table format
System.out.println(" Job Arrival CPU Start Finish Turnaround Time = Time Cycle Time Time Finish Time - Arrival Time ");
System.out.print("===================================================================== ");
for(int i=0;i<n;i++) // create for loop until number of process
{
// display actual values of an array elements of each process
System.out.println((char)asc+" "+aTime[i]+" "+cycle[i]+" "+sTime[i]+" "+fTime[i]+" "+fTime[i]+" - "+aTime[i]+" = "+tTime[i]);
asc++; // increment process names
}
System.out.print("===================================================================== ");
// display table information and turnaround time
System.out.println();
System.out.print("Average Turnaround Time: (");
for(int i=0;i<n;i++)
System.out.print(tTime[i]+" + ");
System.out.print(") / "+n+" = "+sum+" / "+n+" = "+tat);
System.out.print(" ===================================================================== ");
}//main
}
OUTPUT
F:PHANIJava>javac SJN.java
F:PHANIJava>java SJN Enter Number of Process: 5
Enter Arrival Time
0
1
2
3
4
Enter CPU Cycle Time
7
5
3
3
2
Enter Start Time
0
15
9
12
7
Calculating Turnaround Time
Job Arrival CPU Start Finish Turnaround Time =
Time Cycle Time Time Finish Time - Arrival Time
=====================================================================
A 0 7 0 7 7 - 0 = 7
B 1 5 15 20 20 - 1 = 19
C 2 3 9 12 12 - 2 = 10
D 3 3 12 15 15 - 3 = 12
E 4 2 7 9 9 - 4 = 5
=====================================================================
Average Turnaround Time: (7 + 19 + 10 + 12 + 5 + ) / 5 = 53 / 5 = 10.6 =====================================================================
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.