2 Replies - 1534 Views - Last Post: 13 October 2012 - 05:44 AM Rate Topic: -----

#1 lilVaratep  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 207
  • Joined: 09-October 11

Priority Queue: Printing

Posted 12 October 2012 - 08:22 PM

What's up with private static PrinterQueue queue;? I don't get what this is supposed to represent. Same as getQueue(). I had the first impression that it was FRONT (returns highest priority in queue), but I don't get how this would happen as it's not the same type as PrintJob.


Description.
In this assignment you have to use the following PrinterQueue class. In this class, you have to implement the parts marked as //TODO. You are not allowed to change the other parts and you are not allowed to add new public methods. You can create another class or use existing structures of Java (see java.util. package) for implementing a priority queue. Do not forget to add comments in your code and to create a documentation using javadoc.

public class PrinterQueue{
private static PrinterQueue queue;
//TODO –add private fields to imlement a priority queue
private PrinterQueue(){
// TODO –initialize the priority queue
}
public static PrinterQueue getQueue(){
if (queue==null) queue=new PrinterQueue();
return queue;} 
public void addJob (PrintJob job) {
//TODO –add the job to the printer queue based the priority of the job. 
// Use the job’s getPriority method to check its priority
System.out.println(“Job ”+ job.getName()+ “is added to the printer queue”);
}
public boolean isEmpty(){
//TODO
}
public PrintJob removeJob(){
//TODO –remove the highest priority job from the queue
System.out.println(“Job ”+ job.getName()+ “has been printed”);

}
public boolean isFull(){
//TODO
}
//TODO –you may add private methods as helper methods. 
}

Part b. 
After implementing the PrinterQueue an the required classes to compile (such as PrintJob), you need to implement a TestPrinterQueue class. TestPrinterQueue class should have a main method.

When the program runs, it takes the following inputs for each job in this order from user until the user enters -1 as priority.
-	Priority (int)
-	 Jobname (String)
-	number of pages in this printjob (int)
There are 4 levels of priority, 0 being the lowest priority.

TestPrinterQueue class, either in its main method or in a helper method invoked by the main method, should use an instance of the PrinterQueue class. I.e. you need to get access to a PrinterQueue object.

The program first takes the jobs from the user, creates job objects, adds them to the queue, then remove the items from the queue (it is encouraged to have these functionality in separate methods of the TestPriotiyQueue class)

Part c.

Now you have to extend the program you have implemented.In this part, you are required to have specific print jobs. A printjob can be a textjob, a binary job ( such as a ps file), or an imagejob . Implement these specific printJobs.

One change caused by this extension is user entries. This time a user enters
-	Priority (int)
-	 Jobname (String)
-	number of pages in this printjob (int)
-	job type (one of these Strings: “textjob” “binaryjob” or “imagejob”) 

While extending your program, which methods have changed? Please indicate the classes and the methods that are affected by this extension in their source code. I.e. write comments in the code saying which ones are affected.


Submission: Put all your code in a java package and zip that folder. Submit the zip file to METU online. If you have used an IDE such as eclipse, do not forget to create a buld.xml for deployment. Make sure your code gets compiled.



PrinterQueue

public class PrinterQueue {
    private static PrinterQueue queue;
    private in size, cap;
    private PrintJob[] jobs;
    
    //private static OrderedVectorOfPrintJob jobs;
    
    private PrinterQueue() {
        size = 0;
        cap = 10;
        jobs = new PrintJob[cap];
    }
    
    public static PrinterQueue getQueue() {
        if (queue == null)
            queue = new PrinterQueue();
        return queue;
    }
    
    public void addJob(PrintJob job) {
        if (size == cap)
            makeBigger();
        for (int pos = size-1; pos >= 0; pos--)
            if (jobs[pos].getPriority() > b.getPriority())
                jobs[pos+1] = jobs[pos];
            else {
                jobs[pos+1] = b;
                size++;
                return;
            }
        jobs[0] = b;
        size++;
    }
    
    public boolean isEmpty() {
        if (size == 0)
            return true;
        return false;
    }
    
    public PrintJob removeJob() {
        PrintJob temp = PrintJob[size-1];
        size--;
        return temp;
    }
    
    public boolean isFull() {
        if (size == cap)
            return true;
        return false;
    }
}


PrintJob

public class PrintJob {

    private int Priority;
    private String JobName;
    private int No_Of_Pages; 


    public PrintJob(int Prio, String Name, int Number ){
        setPriority(Prio);
        setName(Name);
        setNo_Of_Pages(Number);
    }

    public void setPriority(int Prio){
        Priority = Prio;
    }

    public void setName(String Name){
        JobName = Name;
    }

    public void setNo_Of_Pages ( int Number){
        No_Of_Pages = Number;
    }


    public String getName(){
        return JobName;
    }

    public int getPriority(){
        return Priority;
    }

    public int getNumberOfPages(){
        return No_Of_Pages;
    }
}

This post has been edited by lilVaratep: 12 October 2012 - 08:24 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Priority Queue: Printing

#2 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Priority Queue: Printing

Posted 13 October 2012 - 12:11 AM

The private static PrinterQueue queue looks like it is a coding trick to implement what is called the Singleton pattern. Basically the code has been designed so there will always be only one PrinterQueue only. The static keyword will make that variable the same across all copies of the given object. You can also see its private, so is the constructor. So the only way you can get a PrinterQueue initiated is by calling the getQueue() method, which returns the one and only reference to queue. If its the first time getQueue is called, it will create a new copy by using the private constructor, otherwise its will just return the object that already exists.

http://en.wikipedia....ngleton_pattern

This post has been edited by SwiftStriker00: 13 October 2012 - 12:29 AM

Was This Post Helpful? 1
  • +
  • -

#3 lilVaratep  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 207
  • Joined: 09-October 11

Re: Priority Queue: Printing

Posted 13 October 2012 - 05:44 AM

View PostSwiftStriker00, on 13 October 2012 - 12:11 AM, said:

The private static PrinterQueue queue looks like it is a coding trick to implement what is called the Singleton pattern. Basically the code has been designed so there will always be only one PrinterQueue only. The static keyword will make that variable the same across all copies of the given object. You can also see its private, so is the constructor. So the only way you can get a PrinterQueue initiated is by calling the getQueue() method, which returns the one and only reference to queue. If its the first time getQueue is called, it will create a new copy by using the private constructor, otherwise its will just return the object that already exists.

http://en.wikipedia....ngleton_pattern



Thanks so much. Knowing this, is there anything I need to change in my Priority Queues class? such as the constructor? Not sure how to implement it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1