Question: i have a java code that is about cpu priority with round robin , the code is complete and correct except for one part which

i have a java code that is about cpu priority with round robin , the code is complete and correct except for one part which is the gantt chart output.
i will provide you the code and the sample input and the sample output so please update what is neccesary in my code .
my input
Please enter Quantum time: 3
Please enter in this order: process ID, arrival time, burst time, and priority (to execute enter 0000):
1035
2186
3341
4252
0000
the gantt chart output i get : Gantt Chart: 0 P1-0 P3-3 P3-6 P4-7 P4-10 P2-12 P2-15 P2-
the correct gantt chart is this : Gantt Chart: 0-P1-3-P3-6-P3-7-P3-7-4-10-P4-12-P4-12-P2-15-P2-18-P2-20
my java code :
// begin of code
import java.util.*;
//define variables
class ProcessDetails
{
int pID; int bTime; int aTime; int pri; int remTime; int wTime; int tTime; int resTime;
boolean done; boolean start;
//start the variables
public ProcessDetails(int pid, int aTime, int bTime, int priority)
{
this.pID = pid;
this.aTime = aTime;
this.bTime = bTime;
this.pri = priority;
this.remTime = bTime;
//defaults values
this.wTime =0;
this.tTime =0;
this.resTime =-1;
this.done = false;
this.start = true;
}
//get methods for the id and priority
public int getPri(){
return this.pri;
}
public int getpID(){
return this.pID;
}
}
public class PRR {
public static void main(String[] args){
//start scanner for input
Scanner kbd = new Scanner(System.in);
System.out.print("Please enter Quantum time:");
int q = kbd.nextInt(); //quantum value saved here
//create new list for the process details (arrival time /...)
List processsos = new ArrayList<>();
//output and form
System.out.println("Please enter in this order: process ID, arrival time, burst time, and priority (to execute enter 0000):");
while (true){
int ID = kbd.nextInt();
int arrivalTime = kbd.nextInt();
int burstTime = kbd.nextInt();
int priority = kbd.nextInt();
// condition 0000 to execute/terminate
if (ID ==0 && arrivalTime ==0 && burstTime ==0 && priority ==0){
break;
}
// add the inputs to the new list
processsos.add(new ProcessDetails(ID, arrivalTime, burstTime, priority));
}
kbd.close(); // Close the scanner to avoid resource leaks
// organize the processes compared to thier arrival times
Collections.sort(processsos, Comparator.comparingInt(p -> p.aTime));
//new variables/defaults
int cTime =0; int processDone =0; int total_wTime =0; int total_tTime =0; int total_resTime =0;
//////////////////////////////////////////////////////////////////////////////////////
StringBuilder GanttChart = new StringBuilder();
int preTime =0;
while (processDone < processsos.size()){
ProcessDetails cur = pickNextProcess(processsos, cTime);
//calculate response time
if (cur != null){
if (cur.start){
cur.resTime = cTime - cur.aTime;
cur.start = false;
}
//draw the gantt chart
GanttChart.append(preTime).append(" P").append(cur.pID).append("-");
preTime=cTime;
int timeSlice = Math.min(q, cur.remTime);
cTime += timeSlice;
cur.remTime -= timeSlice;
// calculate turnaround,wait times / add them to the prev value [total]
if (cur.remTime ==0)
{
cur.done = true;
processDone++;
cur.tTime = cTime - cur.aTime;
cur.wTime = cur.tTime - cur.bTime;
total_wTime += cur.wTime;
total_tTime += cur.tTime;
total_resTime += cur.resTime;
}
} else {
cTime++; //because there is no process active right now so so add 1 to go next
preTime=cTime;
}
}
//helpful message for the user
System.out.println("
Schedule Successfully done.");
//print the gantt chart
System.out.println("Gantt Chart: "+ GanttChart.toString().trim());
//////////////////////////////////////////////////////////////////////////////////////
//print the process details
for (ProcessDetails p : processsos){
System.out.println("
-------------------------"+
"
Process ID: ["+ p.pID +"]"+"
The Turnaround Time: "+ p.tTime +"
The Waiting Time: "+ p.wTime +"
The Response Time: "+ p.resTime +"
");
}
//print the averages

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!