Highlights
Programming Assignment
In this assignment, you will implement four versions of a priority queue, and two drivers to test them. The drivers both simulate putting “jobs” of specified priority onto the queue, and then taking them off and “execute” them. The first driver takes commands from the input, as the drivers in the first three programming assignments. The second driver will experimentally test the behavior of the queues by generated large numbers of random jobs and interspersing job creation and execution randomly.
Basic idea
The objective of this assignment is to compare the running time and quality of four different implementations of a priority queue. A driver will run the priority queue by putting “jobs” on the queue and requesting that they are “executed”. The quality of a queue is determined by the extent to which it executed high priority jobs quickly. The priority of a job is a number between 0.0 and 1.0. Large numbers correspond to high priority. If the queue is empty, then the request to “execute the next job” is a no-op.
Priority Queue Implementation
The four priority queue implementations are:
1. A stack implemented as an array. When a new job comes in, it is put at the top of the stack; and the job at the top of the stack is always chosen to execute next. That is, the priorities are ignored altogether. This will run very quickly but will have poor quality (defined below).
2. An array of four stacks, each stack implemented as an array. The first stack has the jobs for which 0.0 - priority - 0.25; the second stack has the jobs for which 0.25 < priority - 0.5; the third stack has the jobs for which 0.5 < priority 0.75; the fourth stack has the jobs for which 0.75 < priority - 1.0. When a job comes in, it is put on the head of the appropriate stack. When a job is to be executed, it is taken off the head of the highest priority non-empty stack. This will run a few times slower than (1) and give higher quality than (1), though not optimal.
3. A headed linked list. Add a new job at the front. To find the highest priority job do a linear search through the list. This is optimal quality, but very slow.
4. A-Max heap, implemented as an array. This should run a few times slower than (2) and will give optimal quality. The heap and the linked list will always execute jobs of equal quality, but if there are two jobs of equal priority, then they may make different choices.
Quality
The quality of a queue on a set of jobs is measured as follows: Define the “delay cost” of a particular job as its priority times the length of time that it waits in the queue. Define the quality of a queue on a collection as the average of the individual delay costs of the jobs. (Low numbers are considered good.) “Time” here is measured in terms of an imaginary clock that ticks once each time a job is created or executed.
Commands from input
The first driver, called TestQ, takes commands from standard input or an input text file, like the first three assignments. Again, there is one command per line, and you may assume that the input is correctly formatted. There are three commands: J <priority>: Create a job with the specified priority. The priority is a floating-point number
E: Pop a job off the queue and execute it
F: Repeatedly pop jobs off the queue and execute them
Class definition
Define the class Job as having two data fields: priority which is a double, and creationTime which is an int. Feel free, of course, to write getters, setters, and constructors. Define the abstract class MyQueue with three abstract methods: void add(Job j); boolean empty(); and Job pop(). Write four classes that extend MyQueue: MyStack, FourStacksMyLinkedList, and MyHeap. For each of these, implement the three methods appropriately.
Experimentation
The second driver, called ExperimentQ, will experimentally test the quality and running times of the four queues with large numbers of jobs. The command line should contain two arguments: The number of commands to execute, and the type of queue to run (1 for the stack, 2 for four stacks, 3 for a linked list, 4 for max heap). For example, if you execute the run line command java ExperimentQ 100 3 , that will generate 100 random commands, as described below, and run them on a linked list. The output is the overall quality and running time. (1 line of output for the entire execution). In doing these experiments, do not give the step-by-step output that you gave in running the first driver.
For queue implementations (1) and (4), use an array of size max(50, n/2), where n is the number of commands. For implementation (2), each stack can be implemented using an array of size max(50, n/8). It is immensely unlikely, though not absolutely impossible, that these sizes will be overrun.
Generating random jobs
The program will then go through three stages. In stage 1, you will generate n/2 random operations. With probability 0.8, this is to create a job of random priority, and with probability 0.2 it is to execute the next job. Stage 2 is the same but reversing the two probabilities. In stage 3, it simply executes the jobs remaining in the queue, without creating any. Thus, in stage 1, the queue fills up rapidly, as jobs are created much more rapidly than they are executed; in stage 2, the queue empties, as jobs are executed much more rapidly than they are created.
Execution time
To measure execution times in java, use the system call System.currentTimeMillis(). This returns the amount of CPU time used in a millisecond. Note that this is returns as along. If you call this before and after executing code and subtract, you will get the CPU time used by the code in milliseconds.
Experiments
For each of the four queue implementations, experiment to see how the running time varies with n. Start with n = 100 and try successively doubling until the running time is a minute, or until Java runs out of memory. Record the running time and quality at each value. Of course, while you are still writing and debugging the code, you should use very small values of n.
This IT and Computer Science Assignment has been solved by our IT and Computer Science experts at My Uni Paper. Our Assignment Writing Experts are efficient to provide a fresh solution to this question. We are serving more than 10000+ Students in Australia, UK & US by helping them to score HD in their academics. Our experts are well trained to follow all marking rubrics & referencing style.
Be it a used or new solution, the quality of the work submitted by our assignment experts remains unhampered. You may continue to expect the same or even better quality with the used and new assignment solution files respectively. There’s one thing to be noticed that you could choose one between the two and acquire an HD either way. You could choose a new assignment solution file to get yourself an exclusive, plagiarism (with free Turnitin file), expert quality assignment or order an old solution file that was considered worthy of the highest distinction.
© Copyright 2026 My Uni Papers – Student Hustle Made Hassle Free. All rights reserved.