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

Write a program to simulate the implementation of a printer interface applicatio

ID: 3629635 • Letter: W

Question

Write a program to simulate the implementation of a printer interface application. The application should consist of a print server object and document objects to be printed. The print server object should contain a queue to store the documents to be printed (therefore, the item type stored in your Queue class should be Document). Each document contains the name, length in bytes, time it was sent to the printer, and owner. Test your classes by deciding the next action randomly: a document leaves the queue (job completed), a new document arrives to the queue, or simply do nothing (the state of the system remains the same, printing or idle).

Explanation / Answer

Since you already have code for a Queue (from previous exercises) then it's just a matter of replacing the char objects with Document objects:

public class Queue {
    public Queue() {
        size = 100;
        list = new Document[size];
        front = 0;
        back = size - 1;
        count = 0;
    }

    public Queue(int s) {
        size = s;
        list = new Document[size];
        front = 0;
        back = size - 1;
        count = 0;
    }

    public void enqueue(Document d) {
        if(isFull())
            System.out.println("Cannot perform Enqueue: Document Queue is Full.");
        else
        {
            back = (back + 1) % size;
            list[back] = d;
            count++;
        }
    }

    public void dequeue() {
        if(isEmpty())
            System.out.println("Cannot perform Dequeue: Document Queue is Empty.");
        else
        {
            front = (front + 1) % size;
            count--;
        }
    }

    public Document getFront() {
        if(isEmpty())
        {
            System.out.println("Cannot perform getFront: Document Queue is Empty.");
            return null;
        }
        else
            return list[front];
    }

    public boolean isEmpty() {
        return count == 0;
    }
   
    public boolean isFull() {
        return count == size;
    }
    
    public int getCount() {
        return count;
    }
    
    private Document[] list;
    private int size;
    private int count;
    private int front, back;
}

The Document class - I added getters and setters just because - and I needed to display stuff so I couldn't call on the private fields from another class:

import java.util.GregorianCalendar;
import java.util.Calendar;

public class Document {
   
    public Document (String n, int l, String o)
    {
        name = n;
        length = l;
        printTime = new GregorianCalendar();
        owner = o;
    }
   
    public void setName(String n) {
       this.name = n;
    }

    public String getName() {
       return name;
    }
   
    public void setLength(int l) {
       this.length = l;
    }

    public int getLength() {
       return length;
    }
   
    public void setPrintTime(GregorianCalendar t) {
       this.printTime = t;
    }

    public GregorianCalendar getPrintTime() {
       return printTime;
    }
   
    public String displayPrintTime() {
       String date = "";
       date = printTime.get(Calendar.DATE) + "-" +
               printTime.get(Calendar.MONTH) + "-" +
               printTime.get(Calendar.YEAR) + " " +
               printTime.get(Calendar.HOUR) + ":" +
               printTime.get(Calendar.MINUTE) + ":" +
               printTime.get(Calendar.SECOND) + ":" +
               printTime.get(Calendar.MILLISECOND);
       return date;
    }
   
    public void setOwner(String o) {
       this.owner = o;
    }

    public String getOwner() {
       return owner;
    }
   
    private String name;
    private int length;
    private GregorianCalendar printTime;
    private String owner;
   
}

Next is the actual Printer Class - all it needs is a Queue of documents and the methods implemented are:

AddprintJob - basically enqueuing another document

printNext - dequeing the next in line

and isIdle - this does nothing except tell you how many print jobs are pending

public class PrintServer {
   
    public PrintServer(){
        documents = new Queue();
    }
   
    public PrintServer(int size){
        documents = new Queue(size);
    }
   
    public void addPrintJob(Document d) {
        // this is basically an enqeue + print that new document was received
        documents.enqueue(d);
        System.out.println("New print job in queue: " + d.getName());
    }
   
    public void printNext() {
        // this is basically a dequeue + print that documetn got printed
        System.out.println("Completing print job: " + documents.getFront().getName());
        documents.dequeue();
    }
   
    public void beIdle() {
        // this is basically idle mode, just print pending print jobs
        System.out.println("Printer is Idle. Pending print jobs: " + documents.getCount());
    }
   
    private Queue documents;
}

Now for the main test class. I didn't do anything fancy here. Just started by filling out 10 random documents. And then to randomize the behavior after that, I pick a rando number and depending on whether it's even or odd, I either print a document or just stay iddle. You can play with that some more and make it as random as you need.

public class Main {
    public static void main(String[] args) {
        PrintServer printer = new PrintServer();
       
        System.out.println("Sending 10 documents to the printer.");
        for (int i = 0; i < 10; i++) {
            Document d = new Document ("document"+i, (int)(Math.random()*1000), "owner"+i);
            System.out.println(d.getName() + " :: length " + d.getLength() + " :: sent at " + d.displayPrintTime() + " :: by " + d.getOwner());
            printer.addPrintJob(d);
        }
       
        for (int i = 0; i < 10; i++)
        {
            if( (int)(Math.random()*100) % 2 == 0 ) // if it's an even number print
            {
                printer.printNext();
            }
            else
                printer.beIdle(); // else just be idle     
        }
    }
}

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