Question

I'm trying to create a program that will process the print queue of 100 prints for 1-3 printers. When trying to display the queue for the printers in the case of 2 working printers, instead of getting a string of many numbers, I get a single number for printer 1 and nothing for printer 2. I suspect the first number is the sum of all the numbers in that queue, but I'm not sure how it got that. Can someone help me out?

import java.util.*;

public class PrintQueue
{
/** The total number of print jobs */
    final static int PRINTS = 100;
    /** The number of pages printed per minute */
    final static int PPM = 10;

/**
 * Main method
 * @param args optional file name for simulation parameters */
public static void main(String args[]) 
{
    // Data Fields
    /** Simulated clock */
    int clock = 0;
    /** Time to print jobs */
    double processTime1 = 0, processTime2 = 0, processTime3 = 0;
    /** The order that the print requests were printed */
    Integer[] orderPrinted = new Integer[PRINTS];
    /** The order that the print requests were recieved */
    Integer[] orderRecieved = new Integer[PRINTS];
    /** All the print jobs submited to printer 1*/
    Queue<Integer> p1 = new LinkedList<Integer>();
    /** All the print jobs submited to printer 2*/
    Queue<Integer> p2 = new LinkedList<Integer>();
    /** All the print jobs submited to printer 3*/
    Queue<Integer> p3 = new LinkedList<Integer>();
    /** The hours require to process a queue*/
    int hours1, hours2, hours3;
    /** The minutes require to process a queue*/
    double minutes1, minutes2, minutes3;

    Random rand = new Random();

    for (; clock < PRINTS; clock++)
    {
        orderRecieved[clock] = rand.nextInt(50) + 1;
    }

    orderPrinted = orderRecieved;
    Arrays.sort(orderPrinted);

    Scanner input = new Scanner(System.in);
    System.out.println("How many of the 3 printers are available for use?");
    int choice = input.nextInt();

    switch (choice)
    {
        case 1:
            for (int i = 0; i < orderPrinted.length; i++)
            {
                processTime1 += orderPrinted[i];
            }
            processTime1 /= PPM;
            hours1 = (int) processTime1 / 24;
            minutes1 = processTime1 % 60;
            System.out.println("The order in which print requests (measured in pages) were recieved is: \n" + displayArray(orderRecieved));
            System.out.println("The order in which print requests (measured in pages) were printed is: \n" + displayArray(orderPrinted));
            System.out.println("The time to print on one printer was: \n" + hours1 + " hours and " + Math.round(minutes1) + " minutes.");
            break; 
        case 2:
            p1.add(orderPrinted[0]);
            p2.add(orderPrinted[1]);
            for (int i = 2; i < orderPrinted.length; i++)
            {
                if (sumTime(p1) <= sumTime(p2))
                {
                    p1.add(orderPrinted[i]);
                    processTime1 += orderPrinted[i];
                }
                else
                {
                    p2.add(orderPrinted[i]);
                    processTime2 += orderPrinted[i];
                }
            }
            processTime1 /= PPM;
            hours1 = (int) processTime1 / 24;
            minutes1 = processTime1 % 60;
            processTime2 /= PPM;
            hours2 = (int) processTime2 / 24;
            minutes2 = processTime2 % 60;

            System.out.println("The order in which print requests (measured in pages) were recieved is: \n" + displayArray(orderRecieved));
            System.out.println("The order in which print requests (measured in pages) were printed is: ");
            System.out.println("Printer 1: " + displayQ(p1));
            System.out.println("Printer 2: " + displayQ(p2));
            System.out.println("The time to print on printer 1 was: \n" + hours1 + " hours and " + Math.round(minutes1) + " minutes.");
            System.out.println("The time to print on printer 2 was: \n" + hours2 + " hours and " + Math.round(minutes2) + " minutes.");
            break;
        case 3: 
            p1.add(orderPrinted[0]);
            p2.add(orderPrinted[1]);
            p3.add(orderPrinted[2]);
            for (int i = 3; i < orderPrinted.length; i++)
            {
                if (sumTime(p1) < sumTime(p2) && sumTime(p1) < sumTime(p3))
                {
                    p1.add(orderPrinted[i]);
                    processTime1 += orderPrinted[i];
                }
                else if (sumTime(p2) < sumTime(p1) && sumTime(p2) < sumTime(p3))
                {
                    p2.add(orderPrinted[i]);
                    processTime2 += orderPrinted[i];
                }
                else
                {
                    p3.add(orderPrinted[i]);
                    processTime3 += orderPrinted[i];
                }
            }

            processTime1 /= PPM;
            hours1 = (int) processTime1 / 24;
            minutes1 = processTime1 % 60;
            processTime2 /= PPM;
            hours2 = (int) processTime2 / 24;
            minutes2 = processTime2 % 60;
            processTime3 /= PPM;
            hours3 = (int) processTime3 / 24;
            minutes3 = processTime3 % 60;

            System.out.println("The order in which print requests (measured in pages) were recieved is: \n" + displayArray(orderRecieved));
            System.out.println("The order in which print requests (measured in pages) were printed is: ");
            System.out.println("Printer 1: " + displayQ(p1));
            System.out.println("Printer 2: " + displayQ(p2));
            System.out.println("Printer 3: " + displayQ(p3));
            System.out.println("The time to print on printer 1 was: \n" + hours1 + " hours and " + Math.round(minutes1) + " minutes.");
            System.out.println("The time to print on printer 2 was: \n" + hours2 + " hours and " + Math.round(minutes2) + " minutes.");
            System.out.println("The time to print on printer 3 was: \n" + hours3 + " hours and " + Math.round(minutes3) + " minutes.");
            break;
     default: 
            System.out.println("You may only choose 1-3 printers.");
            break
    default: 
            System.out.println("You may only choose 1-3 printers.");
            break;
    }
}

/** Displays the contents of an array 
        @param array The array of data to be displayed
        @returns a stringArray The data as a StringBuilder object 
 */
public static StringBuilder displayArray(Integer[] array)
{
    StringBuilder stringArray = new StringBuilder(); 
    for (int i = 0; i < array.length; i++)
    {
        stringArray.append(array[i] + "     ");
    }
    return stringArray;
}

/** Displays the contents of a queue 
        @param Q The queue of data to be displayed
        @returns a stringQ The data as a StringBuilder object 
 */
public static StringBuilder displayQ(Queue<Integer> Q)
{
    StringBuilder stringQ = new StringBuilder(); 
    for (int i = 0; i < Q.size(); i++)
    {
        stringQ.append(Q.poll() + "     ");
    }
    return stringQ;
}

/** Sums the total time of the print requests on a printer queue
        @param printer The printer's queue of print requests
        @returns sum The total time required to print all pages on the printer's queue
 */
public static double sumTime(Queue<Integer> printer)
{
    double sum = 0;
    for (int i = 0; i < printer.size(); i++)
    {
        sum += printer.poll();
    }
    sum /= PPM;
    return sum;
}
}
Was it helpful?

Solution

In method sumTime change

printer.poll();

to

printer.peek();

and try.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top