Question: 8.7. Re-implement the BoundedQueue class as a subtype of the Queue interface type in the standard library ch08/queue/BoundedQueue.java 1 import java.util.*; 2 3 /** 4

8.7. Re-implement the BoundedQueue class as a subtype of the Queue interface type in the standard library

ch08/queue/BoundedQueue.java 1 import java.util.*; 2 3 /** 4 A first-in, first-out bounded collection of objects. 5 */ 6 public class BoundedQueue extends AbstractCollection 7 { 8 /** 9 Constructs an empty queue. 10 @param capacity the maximum capacity of the queue 11 @precondition capacity > 0 12 */ 13 public BoundedQueue(int capacity) 14 { 15 elements = new Object[capacity]; 16 count = 0; 17 head = 0; 18 tail = 0; 19 } 20 21 public Iterator iterator() 22 { 23 return new 24 Iterator() 25 { 26 public boolean hasNext() 27 { 28 return visited < count; 29 } 30 31 public E next() 32 { 33 int index = (head + visited) % elements.length; 34 E r = (E) elements[index]; 35 visited++; 36 return r; 37 } 38 39 public void remove() 40 { 41 throw new UnsupportedOperationException(); 42 } 43 44 private int visited = 0; 45 }; 46 } 47 48 /** Fall 2017 Students of Suneuy Kim 11 49 Remove object at head. 50 @return the object that has been removed from the queue 51 @precondition size() > 0 52 */ 53 public E remove() 54 { 55 E r = (E) elements[head]; 56 head = (head + 1) % elements.length; 57 count--; 58 return r; 59 } 60 61 /** 62 Append an object at tail. 63 @param anObject the object to be appended 64 @return true since this operation modifies the queue. 65 (This is a requirement of the collections framework.) 66 @precondition !isFull() 67 */ 68 public boolean add(E anObject) 69 { 70 elements[tail] = anObject; 71 tail = (tail + 1) % elements.length; 72 count++; 73 return true; 74 } 75 76 public int size() 77 { 78 return count; 79 } 80 81 /** 82 Checks whether this queue is full. 83 @return true if the queue is full 84 */ 85 public boolean isFull() 86 { 87 return count == elements.length; 88 } 89 90 /** 91 Gets object at head. 92 @return the object that is at the head of the queue 93 @precondition size() > 0 94 */ 95 public E peek() 96 { 97 return (E) elements[head]; Fall 2017 Students of Suneuy Kim 12 98 } 99 100 private Object[] elements; 101 private int head; 102 private int tail; 103 private int count; 104 }

Below are files that are required for the question

JunitTestBoundedQueue.java

import static org.junit.Assert.*;

import org.junit.Test;

public class JunitTestBoundedQueue {

BoundedQueue iQueue = new BoundedQueue(10);

BoundedQueue sQueue = new BoundedQueue(10);

@Test

public void testAddSizePeek() {

iQueue.add(10);

sQueue.add("aa");

assertTrue(iQueue.size() == 1);

assertTrue(sQueue.size() == 1);

assertTrue(iQueue.peek() == 10);

assertTrue(sQueue.peek().equals("aa"));

}

@Test

public void testRemove() {

iQueue.add(10);

iQueue.add(20);

sQueue.add("aa");

assertTrue(iQueue.size() == 2);

assertTrue(sQueue.size() == 1);

assertTrue(iQueue.peek() == 10);

assertTrue(sQueue.peek().equals("aa"));

assertTrue(iQueue.remove() == 10);

assertTrue(iQueue.peek() == 20);

assertTrue(iQueue.size() == 1);

}

}

TestBoundedQueue.java

import java.util.Iterator;

public class TestBoundedQueue {

static BoundedQueue iQueue = new BoundedQueue(10);

static BoundedQueue sQueue = new BoundedQueue(10);

public static void main(String[] args) {

iQueue.add(10);

iQueue.add(20);

Iterator iteratorI = iQueue.iterator();

while(iteratorI.hasNext()) {

System.out.print(iteratorI.next() + " ");

}

System.out.println();

sQueue.add("aa");

sQueue.add("bb");

sQueue.add("cc");

Iterator iteratorS = sQueue.iterator();

while(iteratorS.hasNext()) {

System.out.print(iteratorS.next() + " ");

}

System.out.println();

sQueue.remove();

iteratorS = sQueue.iterator();

while(iteratorS.hasNext()) {

System.out.print(iteratorS.next() + " ");

}

System.out.println();

}

}

This question is from Ch.8 of A Crash Course in Java

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!