I use Eclipse IDE and Java. I provided ItemQueue.java (at bottom of post), also
ID: 3735823 • Letter: I
Question
I use Eclipse IDE and Java. I provided ItemQueue.java (at bottom of post), also DelegateLinkedItemQueue is irrelevant because we are replacing its implimentation with CircularArrayQueue, but i provided it anyway.
Step 1:
Modify the main of Banner to use the Circular Array Implementation of queue. (see the TODO comment)
Step 2:
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)
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 queue;
private LinkedList charsInFlight = new 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 queue) {
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 iter = charsInFlight.iterator(); iter
.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 queue = new DelegateLinkedItemQueue();
// 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);
}
}
CircularArrayQueue.java:
import java.util.NoSuchElementException;
public class CircularArrayQueue implements ItemQueue {
private static final int capacity = 5;
private Object[] Q;
private int N; // capacity
private int f = 0;
private int r = 0;
public CircularArrayQueue() {
this(capacity);
}
public CircularArrayQueue(int capacity) {
N = capacity;
Q = new Object[N];
}
@Override
public void enqueueItem(E item) {
if (isFull()) {
throw new NoSuchElementException("Queue is Full.");
} else {
Q[r] = item;
r = (r + 1) % N;
}
}
@Override
public E dequeueItem() {
E item;
if (isEmpty()) {
throw new NoSuchElementException("Queue is Empty.");
} else {
item = (E) Q[f];
Q[f] = null;
f = (f + 1) % N;
}
return (E) item;
}
@Override
public E peekItem() {
if (isEmpty()) {
throw new NoSuchElementException("Queue is Empty.");
} else {
return (E) Q[r];
}
}
@Override
public boolean isEmpty() {
return (r == f) ? true : false;
}
public boolean isFull() {
int diff = r - f;
if (diff == -1 || diff == (N - 1))
return true;
return false;
}
@Override
public int noItems() {
if (r > f)
return r - f;
return N - f + r;
}
}
DelegateLinkedItemQueue.java:
import java.util.LinkedList;
/**
* An implementation of an ItemQueue using a LinkedList as a delegate. This
* implementation is thread safe.
*
* @author
*/
public class DelegateLinkedItemQueue<E> implements ItemQueue<E> {
private LinkedList<E> list = new LinkedList<E>();
/**
* 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
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();
}
Explanation / Answer
here is your modified code for Banner.java : ---------->>>>>>>>>>
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 java.util.Scanner;
//import edu.util.CircularArrayQueue;
//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 queue;
private LinkedList<CharInfo> charsInFlight = new 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 queue) {
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 iter = charsInFlight.iterator(); iter
.hasNext();) {
lastCharInfo = (CharInfo)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 queue = new CircularArrayQueue(200);
// 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.
Scanner sc = new Scanner(System.in);
String com;
while(true){
System.out.println("Enter text (or "//" to exit: ");
com = sc.nextLine();
if(com.equals("//")){
break;
}else{
for(int i = 0;i<com.length();i++){
queue.enqueueItem(new Character(com.charAt(i)));
}
}
queue.enqueueItem(new Character(' '));
}
// 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);
}
}