Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

i have this java code and i need to paraphrase it ( make it look like a new / unique code since i used it

i have this java code and i need to paraphrase it (make it look like a new/unique code since i used it last year ) without changing the context and output results and please make sure it works after the changes (for example change every variable name and function and any changable thing to make it look unique / make each process id in a table for example of something ):
import java.util.*;
class Process {
int id;
int burstTime;
int arrivalTime;
int priority;
int remainingTime;
int waitingTime;
int turnaroundTime;
int responseTime;
boolean isComplete;
boolean isFirstCycle;
public Process(int id, int arrivalTime, int burstTime, int priority){
this.id = id;
this.arrivalTime = arrivalTime;
this.burstTime = burstTime;
this.priority = priority;
this.remainingTime = burstTime;
this.waitingTime =0;
this.turnaroundTime =0;
this.responseTime =-1;
this.isComplete = false;
this.isFirstCycle = true;
}
// Getter for priority
public int getPriority(){
return this.priority;
}
// Getter for ID
public int getId(){
return this.id;
}
}
public class PriorityRoundRobinScheduling {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter quantum time:");
int quantum = scanner.nextInt();
List processes = new ArrayList<>();
System.out.println("Enter process ID, arrival time, burst time, and priority (terminate with 0000):");
while (true){
int id = scanner.nextInt();
int arrivalTime = scanner.nextInt();
int burstTime = scanner.nextInt();
int priority = scanner.nextInt();
if (id ==0 && arrivalTime ==0 && burstTime ==0 && priority ==0){
break;
}
processes.add(new Process(id, arrivalTime, burstTime, priority));
}
scanner.close(); // Close the scanner to avoid resource leaks
// Sort processes by arrival time
Collections.sort(processes, Comparator.comparingInt(p -> p.arrivalTime));
int currentTime =0;
int completedProcesses =0;
int totalWaitingTime =0;
int totalTurnaroundTime =0;
int totalResponseTime =0;
StringBuilder ganttChart = new StringBuilder();
while (completedProcesses < processes.size()){
Process current = pickNextProcess(processes, currentTime);
if (current != null){
if (current.isFirstCycle){
current.responseTime = currentTime - current.arrivalTime;
current.isFirstCycle = false;
}
ganttChart.append(" p").append(current.id).append("-");
int timeSlice = Math.min(quantum, current.remainingTime);
currentTime += timeSlice;
current.remainingTime -= timeSlice;
if (current.remainingTime ==0){
current.isComplete = true;
completedProcesses++;
current.turnaroundTime = currentTime - current.arrivalTime;
current.waitingTime = current.turnaroundTime - current.burstTime;
totalWaitingTime += current.waitingTime;
totalTurnaroundTime += current.turnaroundTime;
totalResponseTime += current.responseTime;
}
} else {
currentTime++; // Increment the current time if no process is running
ganttChart.append(".");
}
}
System.out.println("Scheduling complete.");
System.out.println("Gantt Chart (RR with Priority): "+ ganttChart.toString().trim());
for (Process p : processes){
System.out.println("Process ID: "+ p.id +
", Turnaround Time: "+ p.turnaroundTime +
", Waiting Time: "+ p.waitingTime +
", Response Time: "+ p.responseTime);
}
System.out.println("Average Turnaround Time: "+(double) totalTurnaroundTime / processes.size());
System.out.println("Average Waiting Time: "+(double) totalWaitingTime / processes.size());
System.out.println("Average Response Time: "+(double) totalResponseTime / processes.size());
}
private static Process pickNextProcess(List processes, int currentTime){
return processes.stream()
.filter(p ->!p.isComplete && p.arrivalTime <= currentTime)
.min(Comparator.comparingInt(Process::getPriority).thenComparingInt(Process::getId))
.orElse(null);
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing Fundamentals, Design, and Implementation

Authors: David M. Kroenke, David J. Auer

14th edition

133876705, 9781292107639, 1292107634, 978-0133876703

More Books

Students also viewed these Databases questions