Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Having trouble with this java lab. If anyone can help with the code i have below

ID: 665038 • Letter: H

Question

Having trouble with this java lab. If anyone can help with the code i have below. Any changes made please comment so i can understand what you did.

For this subject we're dealing with queue. This is the output needs to have a Event, System time, Process identifer PID, Total time in the system, identify of the lowest-level queue in which the process resided. I don't how to do simulation on event, total time in system, and Lowest Level queue.

Here is what the output should display

Here is the code i have so far...any changes made please leave comments so i can understand what you did. As for the mfq.txt. As for the what i put in the mfq.txt

try these numbers insert in your mfq.txt

//ObjectQueue class

//Job Class

//CPU Class

Explanation / Answer

CPU.java


import java.io.*;
public class CPU {
ObjectQueue q1 = new ObjectQueue();
ObjectQueue q2 = new ObjectQueue();
ObjectQueue q3 = new ObjectQueue();
ObjectQueue q4 = new ObjectQueue();
int cpuTime = 0,
  quantum = 0;
private PrintWriter writer;
Job job = new Job(writer);
int jobID = job.jobID();
int enQue = job.enQue();
int workTime = 0;
public CPU(PrintWriter writer)throws IOException{
  this.writer = writer;
}
public void doWork(Object queueTop){
  job.cutter((String)queueTop);
  System.out.println(cpuTime + "**" + job.enQue() + "**" + job.workTime());
  if(job.enQue() == cpuTime){
   q1.insert(queueTop);
   workTime = job.workTime();
   queueOne();
  }
  else if(q1.isEmpty() && q2.isEmpty() && q3.isEmpty() && q4.isEmpty())
   {cpuTime++;
   doWork(queueTop);}
  else if(!q1.isEmpty())
   {queueOne();
   doWork(queueTop);}
  else if(!q2.isEmpty())
   {queueTwo();
   doWork(queueTop);}
  else if(!q3.isEmpty())
   {queueThree();
   doWork(queueTop);}
  else if(!q4.isEmpty())
   {queueFour();
   doWork(queueTop);}
}
public int getTime(){
  return cpuTime;
}
public void queueOne(){
  if(quantum < 2 && workTime > 0)
   {quantum++;
   workTime--;
   cpuTime++;}
  else if(workTime == 0)
   {System.out.println("1: " + cpuTime + " " + quantum);
   System.out.println(cpuTime + " REMOVED: " + q1.remove());
   workTime = job.workTime();
   quantum = 0;}
  else
   {q2.insert(q1.remove());
   System.out.println("1: " + cpuTime + " " + quantum);
   quantum = 0;
   queueTwo();}
}
public void queueTwo(){
  if(quantum < 4 && workTime > 0)
   {quantum++;
   workTime--;
   cpuTime++;}
  else if(workTime == 0)
   {System.out.println("2: " + cpuTime + " " + quantum);
   System.out.println(cpuTime + " REMOVED: " + q2.remove());
   workTime = job.workTime();
   quantum = 0;}
  else
   {q3.insert(q2.remove());
   System.out.println("2: " + cpuTime + " " + quantum);
   quantum = 0;
   queueThree();}
}
public void queueThree(){
  if(quantum < 8 && workTime > 0)
   {quantum++;
   workTime--;
   cpuTime++;}
  else if(workTime == 0)
   {System.out.println("3: " + cpuTime + " " + quantum);
   System.out.println(cpuTime + " REMOVED: " + q3.remove());
   workTime = job.workTime();
   quantum = 0;}
  else
  {q4.insert(q3.remove());
  System.out.println("3: " + cpuTime + " " + quantum);
  quantum = 0;
  queueFour();}
}
public void queueFour(){
  if(quantum < 16 && workTime > 0)
   {quantum++;
   workTime--;
   cpuTime++;}
  else if(workTime == 0)
   {System.out.println("4: " + cpuTime + " " + quantum);
   System.out.println(cpuTime + " REMOVED: " + q4.remove());
   quantum = 0;
   workTime = job.workTime();}
  else
  {q4.insert(q4.remove());
  System.out.println("4: " + cpuTime + " " + quantum);
  System.out.println(workTime);
  System.out.println(q4.query());}
}
}

Driver.java

import java.io.*;
public class Driver {
public static void main(String[] args) throws IOException{
  PrintWriter pw = new PrintWriter(
    new FileWriter("csis.txt"));
  MFQ mfq = new MFQ(pw);
  mfq.getJobs();
  mfq.outputHeader();
  mfq.runSimulation();
  mfq.outStats();
  pw.close();}}
Job.java

import java.io.*;
import java.util.Scanner;
public class Job {
private Scanner read = new Scanner(new File("mfq.txt"));
String s = read.nextLine();
String[] st = s.split("\s+");
public Job(PrintWriter write)throws IOException{
}
public Job(PrintWriter write, Scanner read) throws IOException{
  this.read = read;}
public void formatS(){
  if(s.startsWith(" "))
    s = s.replaceFirst(" ", "");
  else;
}
public void job(Scanner read)throws IOException{
   s = read.nextLine();
   formatS();
   st = s.split("\s+");}
public Object jobList(){
  return enQue() + " " + jobID() + " " + workTime();
  }
public void cutter(String queue){
  st = queue.split("\s+");
}
public int enQue(){
  return Integer.parseInt(String.valueOf(st[0]));
}
public int jobID(){
  return Integer.parseInt(String.valueOf(st[1]));}
public int workTime(){
  return Integer.parseInt(String.valueOf(st[2]));}}

MFQ.java

import java.io.*;
import java.util.Scanner;
public class MFQ extends Driver{
private PrintWriter writer;
ObjectQueue que = new ObjectQueue();
CPU cpu = new CPU(writer);
Scanner read = new Scanner(new File("mfq.txt"));
Job job = new Job(writer, read);
public MFQ(PrintWriter writer)throws IOException{
  this.writer = writer;}
public void getJobs()throws IOException{
  Job job = new Job(writer, read);
  if(!read.hasNextLine()){}
  else{
   job.job(read);
   que.insert(job.jobList()); //Inserts jobList() which is cast up to an Object. This is the Queue that houses all jobs,
          //confirmed by my outStats() method.
  getJobs();}}     //Recursive call.
public void outputHeader(){
System.out.println("Event     " + "System Time        " + "PID      " + "CPU Time Needed       " + "Total Time in System      " + "Lowest Level Queue      ");
  }
//System.out.println(" Add Process Work Time: ID: Time:");}
public void runSimulation()throws IOException{
  if(!que.isEmpty())
  {cpu.doWork(que.remove());
  System.out.println(cpu.getTime());
  if(!que.isEmpty())
   System.out.println(que.query());
  else;
  runSimulation();}
}

public void outStats(){
  /*if(!que.isEmpty())
   {System.out.println(que.query());
   writer.println(que.remove());
   outStats();}*/}}

ObjectQueue.java

public class ObjectQueue implements ObjectQueueInterface{
private Object[] item;
private int front;
private int rear;
private int size;
public ObjectQueue() {
size = 100;
item = new Object[size];
front = size-1;
rear = size-1;
}
public ObjectQueue(int max) {
size = max;
item = new Object[size];
front = size-1;
rear = size-1;
}
public boolean isEmpty() {
return front == rear;
}
public boolean isFull() {
return rear == size-1 ? front == 0 : front == rear+1;
}
public void clear() {
item = new Object[size];
front = size-1;
rear = size-1;
}
public void insert(Object x) {
if (isFull()) {
System.out.println("Insert Runtime Error: Queue Overflow");
System.exit(1);
}
if (rear == size-1) // or rear = (rear+1) % size;
rear = 0;
else
rear++;
item[rear] = x;
}
public Object remove() {
if (isEmpty()) {
System.out.println("Remove Runtime Error: Queue Underflow");
System.exit(1);
}
if (front == size-1) // or front = (front+1) % size;
front = 0;
else
front++;
Object temp = item[front];
item[front] = null;
return temp;
}
public Object query() {
if (isEmpty()) {
System.out.println("Query Runtime Error: Queue Underflow");
System.exit(1);
}
if (front == size-1)
return item[0];
else
return item[front+1];
}
}

Ob

public interface ObjectQueueInterface {
public boolean isEmpty();
public boolean isFull();
public void clear();
public void insert(Object x);
public Object remove();
public Object query();
}

mfq.txt

2 101 3
7 102 1
9 103 7
12 104 5
14 105 1
17 106 1
18 107 50
24 108 2
34 109 12
37 110 3
44 111 10
45 112 48
50 113 1
56 114 1
71 115 3
81 116 2

jectQueueInterface.java

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote