Fixed & Variable Stacks and Queues Assignment

Assignment Task

A) Fixed-sized Stack

Your task for Part A is to fully implement the “BookFixedStack” class. The BookFixedStack class is a fixed stack that holds Book objects. This stack class is initialized to a fixed size – the “top” variable indicates the top of the stack.

(Note that if the stack is empty, top should be -1).

The fixed stack methods that need to be implemented are as follows:

public boolean push(Book book)

  • Push “book” onto the stack; return null of the stack is full.

public Book pop()

  • Remove the top Book from the stack and return it. Return null if the stack is empty.

public Book peek()

  • Return a reference to the top Book on the stack. Return null if the stack is empty.

public boolean isEmpty()

  • Return true of the stack is empty, false otherwise.

public boolean isFull()

  • Return true of the stack is full, false otherwise.

B) Variable-sized Stack

Your task for Part B is to fully implement the “BookVariableStack” class. The BookVariableStack class is a variable-sized (i.e., resizable) stack that holds Book objects. This stack class is initialized to a starting size – the “top” variable indicates the top of the stack. (Note that if the stack is empty, top should be -1).

The fixed stack methods that need to be implemented are as follows:

public void push(Book book)

  • If the stack is “full” (at current capacity), expand the stack. Then push “book” onto the stack.

private void expand()

  • Double the capacity of the stack. (Be sure to create the new array, copy the existing content to the new array, set the stack’s array reference to the new array, and update the capacity data field).

public Book pop()

  • Remove the top Book from the stack and return it. Return null if the stack is empty. If the resulting stack content size is less than half of the capacity, shrink the stack.

private void shrink()

  • Set the stack capacity to half the current capacity. If the resulting capacity is less than 1, set the capacity to 1. (Be sure to create the new array, copy the existing content to the new array, set the stack’s array reference to the new array, and update the capacity data field).

public Book peek()

  • Return a reference to the top Book on the stack. Return null if the stack is empty.

public boolean isEmpty()

  • Return true of the stack is empty, false otherwise.

public boolean isFull()

  • Return true of the stack is full, false otherwise.

public int size() 

C) Implement Queue

Your task for this assignment to fully implement the “PersonQueue” class. The PersonQueue class is a FIFO queue that holds Person objects. This queue does not have a fixed size – the “head” variable points to the first Person in the queue (next to be removed) and the “tail” variable points to the last Person in the queue (last to be removed). Note that if the stack is empty, head and tail should be null.

The queue methods that need to be implemented are as follows:

public void enqueue(Person person)

  • Add the “person” to the end of the queue (the new element becomes tail).

public Person dequeue()

  • Remove the Person object at the “head” of the queue and return it. The next element in the queue becomes “head”. Return null if the queue is empty.

public Person peek()

  • Return a reference to the Person object at the “head” of the queue. Return null if the queue is empty.

public boolean isEmpty()

  • Return true if the queue is empty, false otherwise.

public int size()

  • Return the number of elements in the queue. Return 0 if the queue is empty.

public void reverseInPlace()

  • Reverse the elements in the queue using the “reverse-in-place” technique.

public void clear()

  • Remove all existing elements from the queue, resetting the queue to its initial state (empty)

public int removeAgeBelow(int value)

  • Remove any elements from the queue whose Person object’s age is less than the “value” parameter passed in. Return the size of the resulting queue.