CSC 212: Programming with Data Structures
Lab 6: Queues
Due: Thursday, Mar. 3, 11:59pm
Credit for this Lab: Nick Howe.
In this lab we're going back to graphics, since it's a really nice
way to visualize what is going on in a queue and why queues are
essential for many applications. We'll also get more practice with
generics and (optionally) with implementing data structures. This lab is
individual and everyone should submit their own code and screenshot,
but you are welcome to discuss and compare code with your classmates.
To start, make a new lab6 project (not package!) and add the following files:
- JQueue.java, which is a graphics
wrapper around a queue. It's only purpose is to display a queue in a
window. We won't be changing this file at all.
- JKeyProcess.java, which you can
think of like viewing a printer. It handles/processes printing requests one
character at a time. We won't be changing this file at all and will
have to work around its limitations.
- NoQueueGUI.java, which manages
the controller part of model-view-controller pattern. It listens for
keys (characters) to be pressed, and then tells the printer how to
handle these requests. Right now it doesn't use a queue, but we'll
change this part. This is the only file you need to modify.
In computer systems, queues are commonly used anywhere there is a mismatch in speeds
between the production of some type of data and its consumption.
The queue allows each process to proceed at its own pace, and ensures that each item will
be handled in the order in which it was created. This lab simulates such a situation.
Without Queues
Take a minute to go through the starter code and make sure you
understand roughly what each method does. Note the imported Java
queue interface. When you run
NoQueueGUI, you should see a simple GUI window consisting of a box with a label under
it. The box is the visual representation of
JKeyProcess, which is intended to simulate a (slow) processing/printing module for keystrokes. It can only handle
one key stroke at a time, and any
additional strokes pressed during handling of the first will be ignored. Try typing
a word quickly, and note how inconvenient the delay is.
Everything would work much more smoothly if extra keystrokes could be buffered in a
queue, so that the processing module could handle them in order when it is ready. Your
task in this lab is to make this change. To make the queue behavior visible, the class
JQueue is designed to display the current
contents of your queue at any given time.
Specifics
To help you complete this task during the lab period, here are some suggestions:
- Change the name of NoQueueGUI.java to
QueueGUI.java (or make a copy). This is the file you will be
modifying. Start by changing the name of the window to "Example with
Queue".
- Add a queue field to QueueGUI. This can be any class that implements the
Queue<Character>
interface. I would suggest LinkedList, since in addition to
implementing the List interface, it also implements the Queue
interface.
You will use this field to store extra keystrokes, and feed them to
the processor when it is ready. Also add a JQueue
field, so that the contents of your queue can be displayed
visually.
- Inside createComponents(), you should initialize
your two new fields (or you could add a constructor and initialize
them there). Note that you must pass the queue you create as an
argument to the JQueue constructor, so that it knows
what to display. Once your JQueue is set up, add it
to the content pane in the BorderLayout.EAST position.
- Rewrite the keyTyped() handler method. Instead of always trying to pass the keystroke to the processor,
it should check first whether the processor is busy using the isBusy method. If it is, the keystroke should be
added to the queue. Also, whenever the queue is updated you should
call repaint() on the JQueue.
- We also need a way to get the keystrokes off the queue and into the key processor when it is ready. The key processor
can notify our GUI manager when it is free if we register an action listener with it. So go ahead and create a nested
action listener class. When triggered, it should move one keystroke
off the queue and into the key processor. Don't forget to
repaint()!
- Then add code
in createComponents() to initialize an instance of
your new listener and register it with the keystroke processor.
- That's it! Test out your program and make sure it works, then
take a screenshot.
- If you have time, it would be great practice to implement your own
generic Queue<E> to replace the one from
the standard Collections classes.
Vocabulary Practice
For the last part of this lab, each student should find their class
account and write a definition for the corresponding vocabulary
word. As you define your term, include
any relevant Java
keywords (i.e.
new,
void,
throws). Here is a list:
List of
Java keywords
Then I will collect all the definitions and put them on the
website for everyone to study from. For this part, send your word to
me over EMAIL.
Class Account |
Vocab Word |
aa | argument |
ac | array (discuss allocate) |
ad | casting |
ae | class |
af | client-server model |
ag | command line and terminal |
ah | complexity and Big O |
ai | constructor |
aj | controller (model-view-controller pattern) |
ak | data structure |
al | exception |
am | field |
an | final |
ao | generic |
ap | inherit, augment, override (w.r.t. methods) |
aq | inheritance (discuss parent/child, super) |
ar | instance |
as | interface |
at | iterator |
au | Java standard library |
av | Javadocs |
aw | linked list |
ay | method (discuss signature) |
az | model (model-view-controller pattern) |
bb | object |
bc | object-oriented |
bd | pointer/reference |
be | primitive type (include a list) |
bf | private |
bg | protected |
bh | public |
bi | queue |
bk | sorting (discuss in-place, runtime of insertion and bubble sort) |
bm | stack |
bp | static |
bq | type (include return types) |
br | UI/GUI |
bs | user |
by | variable (discuss declare/assign) |
bz | view (model-view-controller pattern) |
To Submit
- QueueGUI.java
- JKeyProcess.java
- JQueue.java
- screenshot.png (no typescript required this time, but make sure it
compiles on the server)
- your vocab word by EMAIL
- Queue.java (optional)
If you have more time, you can either implement a queue to replace the
built-in one, or you can start studying for the midterm (previous
midterms are linked from the calendar).