Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class BoundedQueue2 { // Overview: a BoundedQueue2 is a mutable, bounded FIFO data structure // of fixed size , with size being fixed at

public class BoundedQueue2

{

// Overview: a BoundedQueue2 is a mutable, bounded FIFO data structure

// of fixed size , with size being fixed at 2.

// A typical Queue is [], [o1], or [o1, o2], where neither o1 nor o2

// are ever null. Older elements are listed before newer ones.

private final Object[] elements;

private int size, front, back;

private final int CAPACITY = 2;

public BoundedQueue2 ()

{

elements = new Object [CAPACITY];

size = 0; front = 0; back = 0;

}

public void enQueue (Object o)

throws NullPointerException, IllegalStateException

{ // Modifies: this

// Effects: If argument is null throw NullPointerException

// else if this is full, throw IllegalStateException,

// else make o the newest element of this

if (o == null)

throw new NullPointerException ("BoundedQueue2.enQueue");

else if (size == CAPACITY)

throw new IllegalStateException ("BoundedQueue2.enQueue");

else

{

size++;

elements [back] = o;

back = (back+1) % CAPACITY;

}

}

public Object deQueue () throws IllegalStateException

{ // Modifies: this

// Effects: If queue is empty, throw IllegalStateException,

// else remove and return oldest element of this

if (size == 0)

throw new IllegalStateException ("BoundedQueue2.deQueue");

else

{

size--;

Object o = elements [ (front % CAPACITY) ];

elements [front] = null;

front = (front+1) % CAPACITY;

return o;

}

}

public boolean isEmpty()

{

return (size == 0);

}

public boolean isFull()

{

return (size == CAPACITY);

}

public String toString()

{

String result = "[";

for (int i = 0; i < size; i++)

{

result += elements[ (front + i) % CAPACITY ] . toString();

if (i < size -1) {

result += ", ";

}

}

result += "]";

return result;

}

}

Question 4 (16 points) Use the class BoundedQueue2 for questions a-f below. A compilable version is available on Canvas in the file BoundedQueue2 java. The queue is managed in the usual circular fashion. Suppose we build a FSM where states are defined by the representation variables of BoundedQueue2. That is, a state is a 4-tuple defined by the values for [elements, size, front, back. For example, the initial state has the value [[null null, 0, 0, 0], and the state that results from pushing an object obj onto the queue in its initial state is [[obj, nuly, l, 0, 1] a) We do not actually care which specific objects are in the queue.

Consequently, there are really just four useful values for the variable elements. What are they? (2 points)

b) How many states are there? (2 points)

c) How many of these states are reachable? (2 points) (you can answer this in part d)

d) Show the reachable states in a drawing. (4 points)

e) Add edges for the enQueue() and deQueue() methods. (For this assignment, ignore the exceptional returns, although you should observe that when exceptional returns are taken, none of the instance variables are modified.) you can answer this in part d) (3 points)

f) Define a small test set that achieves Edge Coverage Implement and execute this test set. You might find it helpful to write a method that shows the internal variables at eachcall. (3 points)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Heikki Topi, Jeffrey A Hoffer, Ramesh Venkataraman

13th Edition

0134773659, 978-0134773650

More Books

Students also viewed these Databases questions

Question

Write a summary of "The Chemical Transfer of Memory".

Answered: 1 week ago