Question
We use Java and Eclpise IDE Step 2: Provide a circular array implementation of a queue. Your implementation must accommodate reallocation of the array as
We use Java and Eclpise IDE
Step 2:
Provide a circular array implementation of a queue. Your implementation must accommodate reallocation of the array as needed. Define your array to initially be a size of 4.
Make your queue implementation is thread safe.
Step 3:
Write a program to thoroughly test all of the methods of your ItemQueue class. Once you are confident is it working correctly, proceed to part III. Make sure to test the resizing of the array thoroughly test your implementation. Be sure to test the reallocation with tail < head and when tail > head.
Step 4:
Modify the main of Banner to use your implementation of a queue. (see the TODO comment)
Step 5:
Modify banner to read from the key board (use System.in) and enqueueItem the typed characters. Use "Enter text (or "//" to exit: as the prompt for the input. Repeat the keyboard input until // is entered. Do not enqueuer the // characters. Instead enqueuer the characters of Goodbye. (see the second TODO comment
Source codes:
Banner.java:
package edu.banner;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.JWindow;
import edu.util.DelegateLinkedItemQueue;
import edu.util.ItemQueue;
/**
* Banner provides a smooth scroll banner window in which to display messages.
* The characters to be displayed in the banner are accessed from a queue. A
* queue of Characters must be provided on construction of a Banner object
*
* @author
*
*/
@SuppressWarnings("serial")
public class Banner extends JWindow {
/**
* CharInfo is an internal class used to keep track of the display of a
* character while it is being displayed by Banner
*
* @author
*
*/
private class CharInfo {
String character;
int position;
int width;
public CharInfo(String character, int position, int width) {
this.character = character;
this.position = position;
this.width = width;
}
}
private ItemQueue
private LinkedList
private Font font = new Font("Default", 0, 50);
private Image buffer;
private Graphics bufferGraphics;
private boolean started = false;
/**
* Constructor - create a Banner
*
* Note: the created Banner will not be displayed until setVisible(true) is
* called.
*
* Note: Banner will not begin displaying characters until start() is
* called.
*
* Warning: the start method will begin a separate thread of execution. THUS
* THE QUEUE SUPPLIED MUST BE THREAD SAFE.
*
* @param queue
* a queue of Characters from which Banner will get its
* characters
*/
public Banner(ItemQueue
this.queue = queue;
getFontMetrics(font).getHeight();
setSize(500, getFontMetrics(font).getHeight());
setLocation(100, 100);
}
/**
* Paint the rendering of the banner
*
* @param g
* the Graphics to use to provide the rendering
*/
public void paint(Graphics g) {
if (bufferGraphics == null) {
buffer = createImage(getSize().width, getSize().height);
bufferGraphics = buffer.getGraphics();
}
bufferGraphics.setColor(Color.white);
bufferGraphics.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
bufferGraphics.setColor(Color.black);
bufferGraphics.setFont(font);
for (CharInfo charInfo : charsInFlight) {
bufferGraphics.drawString(charInfo.character, charInfo.position,
bufferGraphics.getFontMetrics().getAscent());
}
bufferGraphics.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
g.drawImage(buffer, 0, 0, this);
}
/**
* Start the banner scrolling
*/
public void start() {
started = true;
new Thread(new Runnable() {
public void run() {
while (started) {
CharInfo lastCharInfo = null;
for (Iterator
.hasNext();) {
lastCharInfo = iter.next();
lastCharInfo.position--;
if (lastCharInfo.position + lastCharInfo.width < 0) {
iter.remove();
}
}
if (lastCharInfo == null
|| lastCharInfo.position + lastCharInfo.width < Banner.this
.getWidth()) {
String character = "" + getNextCharacter();
charsInFlight.add(new CharInfo(character, Banner.this
.getWidth(), getFontMetrics(font).stringWidth(
character)));
}
Banner.this.repaint();
try {
Thread.sleep(12);
} catch (InterruptedException e) {
}
}
}
}).start();
}
/**
* Stop the banner from scrolling
*/
public void stop() {
started = false;
}
/**
* Get the next character to be displayed in the banner
*
* @return next character to be displayed
*/
private char getNextCharacter() {
if (!queue.isEmpty()) {
return ((Character) queue.dequeueItem()).charValue();
} else {
return ' ';
}
}
public static void main(String[] args) {
// TODO replace new with your array based circular queue
ItemQueue
// Create and display the banner. Start the banner thread to display
// any characters enqueued to the queue
// DO NOT CHANGE THIS CODE
Banner banner = new Banner(queue);
banner.setVisible(true);
banner.start();
// TODO replace all following code with code to read from keyboard and
// add characters into the queue. Use a "Enter text (or \"//\" to exit: " as a prompt
// to read a line of text at a time. Add each character in the line to the queue. Add
// a space character after each line of characters. Repeat until \"//\" is entered as
// the line of text.
queue.enqueueItem(new Character('H'));
queue.enqueueItem(new Character('e'));
queue.enqueueItem(new Character('l'));
queue.enqueueItem(new Character('l'));
queue.enqueueItem(new Character('o'));
queue.enqueueItem(new Character(' '));
queue.enqueueItem(new Character('W'));
queue.enqueueItem(new Character('o'));
queue.enqueueItem(new Character('r'));
queue.enqueueItem(new Character('l'));
queue.enqueueItem(new Character('d'));
// Wait until the queue is empty
while (!queue.isEmpty()) {
try {
// wait for one second
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
// Wait for 10 more seconds for any characters in flight to complete display
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
}
// Put \"Goodbye\" into the queue
queue.enqueueItem(new Character('G'));
queue.enqueueItem(new Character('o'));
queue.enqueueItem(new Character('o'));
queue.enqueueItem(new Character('d'));
queue.enqueueItem(new Character('b'));
queue.enqueueItem(new Character('y'));
queue.enqueueItem(new Character('e'));
// Wait for 10 more seconds for any characters in flight to complete display
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
}
System.exit(0);
}
}
DelegateLinkedItemQueue.java:
package edu.util;
import java.util.LinkedList;
/**
* An implementation of an ItemQueue using a LinkedList as a delegate. This
* implementation is thread safe.
*
* @author
*/
public class DelegateLinkedItemQueue
private LinkedList
/**
* Enqueue an object to the end of the queue
* @param item the object to be enqueued
*/
public synchronized void enqueueItem(E item) {
list.add(item);
}
/**
* Dequeue an object from the front of the queue
* @return the item removed from the front of the queue
* @throws NoSuchElementException if the queue is empty
*/
public synchronized E dequeueItem() {
return list.removeFirst();
}
/**
* Examine the object at the head of the queue, but do
* not remove it.
* @return the item at the head of the queue
* @throws NoSuchElementException if the queue is empty
*/
public synchronized E peekItem() {
return list.getFirst();
}
/**
* Test if the queue is empty
* @return true if the queue is empty, otherwise false
*/
public synchronized boolean isEmpty() {
return list.isEmpty();
}
/**
* Get the number of items in the queue
*
* @return the number of items in the queue
*/
public synchronized int noItems() {
return list.size();
}
}
ItemQueue.java:
package edu.util;
/**
* An abstract data type for a Queue. Specifies the 5
* main methods of a queue
*
* @author
*/
public interface ItemQueue
/**
* Enqueue an object to the end of the queue
* @param item the object to be enqueued
*/
public void enqueueItem(E item);
/**
* Dequeue an object from the front of the queue
* @return the item removed from the front of the queue
* @throws NoSuchElementException if the queue is empty
*/
public E dequeueItem();
/**
* Examine the object at the head of the queue, but do
* not remove it.
* @return the item at the head of the queue
* @throws NoSuchElementException if the queue is empty
*/
public E peekItem();
/**
* Test if the queue is empty
* @return true if the queue is empty, otherwise false
*/
public boolean isEmpty();
/**
* Get the number of items in the queue
*
* @return the number of items in the queue
*/
public int noItems();
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started