By the way, I\'ve already checked the answers on the net so...Please don\'t just
ID: 3687653 • Letter: B
Question
By the way, I've already checked the answers on the net so...Please don't just copy & paste from somewhere else.
One of the main applications of priority queues is in operating systems - for scheduling jobs on a CPU. In this assignment you are to build a program that schedules simulated CPU jobs. Your program should run in a loop, each iteration of which corresponds to a time slice for the CPU. Each job is assigned a priority, which is an integer between -20 (highest priority) and 19 (lowest priority), inclusive. From among all jobs waiting to be processed in a time slice, the CPU must work on a job with highest priority. In this simulation, each job will also come with a length value, which is an integer between 1 and 10, inclusive, indicating the number of time slices that are needed to process this job. When not interrupted, a job, when scheduled on the CPU, runs for a number of time slices equal to its length. Jobs, however, may be preempted. For example, when a new job with a higher priority arrives, execution of the current job will be suspended, with the unfinished time-slices as the new job length. For simplicity, overhead from context switches will not be accounted for. Also, jobs of the same priority are scheduled on a first-come-first-served (FCFS) basis. Your simulator must output the name of the job running on the CPU in each time slice and must process a sequence of commands, one per time slice, each of which is of the form add job name with length n and priority p or no new job this slice. Name your main class PQScheduler
Explanation / Answer
Sample Input and Output
Your program (PQScheduler) should read inputs from stdin (System.in in Java) and write outputs to
stdout (System.out in Java). See Table 1 for an example. Note the following:
Closely follow the I/O format. Contact the TA for questions about the format.
Input/Output lines are case-sensitive.
Your program should read a line from stdin at a time until the end of file is reached.
Your program should print out as many output lines as input lines.
You may assume that a job name contains no spaces.
You may assume that no invalid input lines will be given.
At any time slice, output an empty line if no jobs are in the queue. See time slice 7 in Table 1 for an
example.
Your program should ignore the remaining jobs in the queue and terminate after all the input lines are
read and processed.
Table 1: A Sequence of Input Jobs and Output Job Names
Time stdin stdout Notes
1 add job job1 with length 3 and priority 0nn job1nn nn: newline
2 no new job this slicenn job1nn
3 add job job2 with length 2 and priority -3nn job2nn preemption happens
4 add job job3 with length 1 and priority 3nn job2nn
5 no new job this slicenn job1nn
6 no new job this slicenn job3nn
7 no new job this sliceEOF nn EOF: end of file.
To generate the end of file in a terminal (command prompt), press Ctrl + D on *nix or Ctrl + Z
followed by Enter on Windows.
Submitting Your Code
When you complete each assignment, you must submit it to us electronically via the HuskyCT site1. Submit
your Java classes as a single zipped file to facilitate grading. Note that you may submit your program as
many times before due. Your final submission will be graded. Note that neither printed copies nor email
attachments will be accepted.
Late Policies
Late submissions will not be graded.
Coding Style and Naming Conventions
The Java code that you write for this course should follow the coding style and naming conventions described
below, which are used by Java courses taught by many institutes.
Matching opening and closing curly braces should be aligned in the same column. This means that
the opening curly brace which follows an if statement should appear directly below the letter i in if,
and not on the same line as the conditional expression.
All statements within curly braces should be indented four spaces (or one TAB character) relative to
the brace. A statement should not appear on the same line as the opening curly brace.
The names that you use for variables, parameters, methods, and classes should be meaningful, except
counters or other loop control variables that may have simple names such as i or p.
All constants in your program should be defined and named meaningfully. For example, if you were
writing an array implementation of a stack with a hard-coded maximum size of 256 elements, define
a static final field such as MAXIMUM ELEMENTS, and use that field in your code, rather than the
literal integer 256.
Class names should be capitalized. Class names with multiple words should have each subsequent
word capitalized, with no underscore separating the words. (e.g. Song, ReggaeSong)
Names of class members (methods and fields) should begin with a lowercase letter. Subsequent words
should be capitalized, with no underscore separating the words. (e.g. playSong(), songTitle)
1http://huskyct.uconn.edu
2
Named constants should be named using all capital letters, with underscores separating the words.
(e.g. EULER, MAXIMUM ELEMENTS)
Every member should be declared using an access-control modifier (e.g. public, private, or protected).
Whitespace should appear between each method argument and around each binary operator. For example,
createFile(”alex.out”, WRITE) instead of createFile(”alex.out”,WRITE), or a + b instead
of a+b.
One single line of code should not be inordinately long. What constitutes ”inordinately long” is
largely a matter of taste, but a good rule of thumb is not to write lines of code that are longer than
80 characters. Don’t be afraid to break up long lines into multiple lines. For example, if you have a
method call with fifteen parameters, put some on one line, some on the next, etc., such that each line
is no longer than 80 characters.
Every class, method, and field should have a comment which briefly explains its purpose. In the case
of methods, some explanation of the meaning of the method’s parameters is expected.
Within the body of your methods, code whose purpose is not readily obvious should be commented.
It is not necessary to include a comment on every line.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.