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

An operating system assigns jobs to print queues based on the number of pages to

ID: 3677511 • Letter: A

Question

An operating system assigns jobs to print queues based on the number of pages to be printed (less than 10 pages, less than 20 pages, or more than 20 pages). You may assume that the system printers are able to print 10 pages per minute. Smaller print jobs are printed before larger print jobs, and print jobs of the same priority are queued up in the order in which they are received. The system administrator would like to compare the time required to process a set of print jobs using one, two, or three system printers.

Write a program to simulate processing 100 print jobs of varying lengths using one, two, or three printers. Assume that a print request is made every minute and that the number of pages to print varies from 1 to 50 pages.

The output from your program should indicate the order in which the jobs were received, the order in which they were printed, and the time required to process the set of print jobs. If more than one printer is being used, indicate which printer each job was printed on.

Explanation / Answer

public class PrinterSim
{
private static final int NUM_JOBS = 100;
private static PrintJob[] jobsArray;
private static int time;
public PrinterSim(int numJobs)
{
jobsArray = new PrintJob[numJobs];
for (int i = 0; i < numJobs; i++)
{
jobsArray[i] = new PrintJob(i + 1, (int) (Math.random() * 50 + 1));
}
}
public void runOnePrinterSim()
{
Printer printerA = new Printer("Printer");
time = 0;
int nextJob = 0;
while (nextJob < NUM_JOBS)
{
if (time % 60 == 0) {
printerA.addJob(jobsArray[nextJob]);
System.out.println("(" + getFormattedTime() + ") "+ jobsArray[nextJob] + " sent to Printer.");
nextJob++;
}
time++;
printerA.update();
}
while (printerA.hasMoreJobs())
{
time++;
printerA.update();
}
System.out.println(" Total Simulation Time (One System Printer): "+ getFormattedTime() + " ");
}
public void runTwoPrinterSim()
{
Printer printerA = new Printer("Printer A");
Printer printerB = new Printer("Printer B");
time = 0;
int nextJob = 0;
while (nextJob < NUM_JOBS)
{
if (time % 60 == 0)
{
if (printerA.compareTo(printerB) <= 0)
{
printerA.addJob(jobsArray[nextJob]);
System.out.println("(" + getFormattedTime() + ") "+ jobsArray[nextJob] + " sent to Printer A.");
}
else
{
printerB.addJob(jobsArray[nextJob]);
System.out.println("(" + getFormattedTime() + ") "+ jobsArray[nextJob] + " sent to Printer B.");
}
nextJob++;
}
time++;
printerA.update();
printerB.update();
}
while (printerA.hasMoreJobs() || printerB.hasMoreJobs())
{
time++;
printerA.update();
printerB.update();
}
System.out.println(" Total Simulation Time (Two System Printers): "+ getFormattedTime() + " ");
}
public void runThreePrinterSim()
{
Printer printerA = new Printer("Printer A");
Printer printerB = new Printer("Printer B");
Printer printerC = new Printer("Printer C");
time = 0;
int nextJob = 0;
while (nextJob < NUM_JOBS)
{
if (time % 60 == 0)
{
if (printerA.compareTo(printerB) <= 0&& printerA.compareTo(printerC) <= 0)
{
printerA.addJob(jobsArray[nextJob]);
System.out.println("(" + getFormattedTime() + ") "+ jobsArray[nextJob] + " sent to Printer A.");
}
else if (printerB.compareTo(printerC) <= 0&& printerB.compareTo(printerA) <= 0)
{
printerB.addJob(jobsArray[nextJob]);
System.out.println("(" + getFormattedTime() + ") "+ jobsArray[nextJob] + " sent to Printer B.");
}
else
{
printerC.addJob(jobsArray[nextJob]);
System.out.println("(" + getFormattedTime() + ") "+ jobsArray[nextJob] + " sent to Printer C.");
}
nextJob++;
}
time++;
printerA.update();
printerB.update();
printerC.update();
}
while (printerA.hasMoreJobs() || printerB.hasMoreJobs()|| printerC.hasMoreJobs())
{
time++;
printerA.update();
printerB.update();
printerC.update();
}
System.out.println(" Total Simulation Time (Three System Printers): "+ getFormattedTime() + " ");
}
public static String formatTime(int seconds)
{
int h = seconds / 3600;
seconds %= 3600;
int m = seconds / 60;
int s = seconds % 60;
if (h > 0)
{
return String.format("%d:%02d:%02d", h, m, s);
}
else
{
return String.format("%d:%02d", m, s);
}
}
public static String getFormattedTime()
{
return formatTime(time);
}
public static void main(String[] args)
{
PrinterSim sim = new PrinterSim(NUM_JOBS);
System.out.println(" Simulation using One System Printer: ");
sim.runOnePrinterSim();
System.out.println(" Simulation using Two System Printers (A, B): ");
sim.runTwoPrinterSim();System.out.println(" Simulation using Three System Printers (A, B, C): ");
sim.runThreePrinterSim();
}
}

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