Question: In JAVA I am trying to place the objects that are marked Running at the end of the list in the output. import java.util.Random
In JAVA I am trying to place the objects that are marked "Running " at the end of the list in the output.
import java.util.Random ;
public class PCB { private String PCB_state; private int PCB_ID ; private String pgmCounter; private int CPU_used; private int CPU_max; private int timeWaiting;
private static int PCB__K; private Random random__X ;
// constructor methods
public PCB () { random__X = new Random();
PCB__K += 1 ; //=====> Increment the static variable for Process ID PCB_state = "Ready" ; PCB_ID = PCB__K ; pgmCounter = "" ; CPU_used = 0 ; CPU_max = random__X.nextInt(50) + 1 ; // Assign max to be between 1 and 50 timeWaiting = 0 ; }
// default constructor
public String showPCB() { return "state: " + PCB_state + "\tID: " + Integer.toString(PCB_ID) + "\tK: " + pgmCounter + "\tCPU used: " + Integer.toString(CPU_used) + "\tCPU max: " + Integer.toString(CPU_max) + "\tWait: " + Integer.toString(timeWaiting) ; }
// set methods
public String get_state() { return PCB_state; }
public int get_ID() { return PCB_ID; }
public int get_CPU_used() { return CPU_used; }
public int get_CPU_max() { return CPU_max; }
public String get_pgmCounter() { return pgmCounter; }
public int get_timeWaiting() { return timeWaiting; }
// Set methods , void returns no value
public void set_state(String state0) { PCB_state = state0 ; }
public void set_CPU_used(int CPU0) { CPU_used = CPU0 ; }
public void add_CPU_used(int c0) { CPU_used += c0 ; }
public void set_CPU_max(int CPU0) { CPU_max = CPU0 ; }
public void set_pgmCounter(String pgmCounter0) { pgmCounter = pgmCounter0 ; }
public void set_timeWaiting(int timeWaiting0) { timeWaiting = timeWaiting0 ; }
public void add_timeWaiting(int t0) { timeWaiting += t0 ; }
public String toString() { return showPCB(); } }
//Client Code
import java.util.LinkedList; import java.util.Iterator; import java.util.Stack; import java.util.PriorityQueue; import java.util.Random ;
public class project2PartB { public static void main (String [] args) { int randomNumber = 0; int counter =0; LinkedList Plist= new LinkedList();
for(int i =0; i <20; i++) { Plist.add(new PCB()); }
Iterator iter = Plist.iterator();
while (iter.hasNext()) { System.out.printf ("%s " ,(PCB)iter.next()); } System.out.println("");
Random num = new Random();
PCB pcb = new PCB(); int Size = Plist.size(); for(int i =0; i < Size ; i ++) {
pcb = Plist.get(i); pcb.set_state("Running"); randomNumber = num.nextInt(20 - 10) + 10; pcb.set_CPU_used(randomNumber);
if(pcb.get_CPU_used() > pcb.get_CPU_max()) {
pcb.set_state("Terminated");
} else if(pcb.get_CPU_used() < pcb.get_CPU_max()) {
}
System.out.println(pcb);
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
