Question: Consider the implementation of an array based Queue in handout D, the peek operation allows you to view the value in the front element of

Consider the implementation of an array based Queue in handout D, the peek operation allows you to view the value in the front element of the queue before dequeueing it, sometimes it might also be valuable to see the second element as well. Write an instance method “peek2” that returns the value in the second element in the queue, without removing it

public S peek2()

Handout D: Generic Array-based Queue Specification

The following represents the set of methods for a queue class.

Public class Queue implements QueueInterface{

private S[] elements;

private int currentSize;

private int maxSize;

private int front;

private int rear;

public Queue(int max);

// allocates a new queue, capable of containing maxsize values,

//Preconditions: MaxSize > 0,

// throws QueueException if maxSize <=0

public boolean isFull();

// determines whether the queue is full.

// returns true if currentSize == maxSize;

public boolean isEmpty();

// determines whether the queue is empty.

// returns true if currentSize ==0

public void enqueue( S newValue);

// adds a new value to the end of the queue

// Preconditions: Queue must not be full, QueueException is thrown

// if the queue is full

// Postconditions: NewValue is inserted at the end of the queue

public S dequeue();

// retrieves the value at the front of the queue

// Precondition: The queue must not be empty.

// Postconditon: If the queue is not empty, the value at

// the front of the queue is removed and returned.

//_Errors: throws QueueException if the queue is empty

public S peek();

// returns the value at the front of the queue without removing it.

// Preconditions: The queue must not be empty.

// Postconditon: If the queue is not empty, the value at

// the front of the queue is returned.

//_Errors: throws QueueException if the queue is empty

public int size();

// returns the number of elements in the queue.

public int getMaxSize();

// returns the maximum number of elements the queue can hold.

public void clear ( int position)

//Description: deletes all elements from the Queue

public String toString()

//Description: displays the contents of the Queue as a comma delimited string

Step by Step Solution

3.52 Rating (159 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To implement the peek2 method in the Queue class you can add the f... View full answer

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 Operating System Questions!