View the step-by-step solution to:

Question

Can someone help me with this question?

/*

 * To change this license header, choose License Headers

in Project Properties.

 * To change this template file, choose Tools | Templates

 * and open the template in the editor.

 */

package dsassignment3;


import DataStructures.EmptyCollectionException;

import DataStructures.InvalidArgumentException;

import DataStructures.LinearNode;

import java.util.ArrayList;


/**

 * 

 * @author author  

 * @version Fall 2019

 * @param <T> 

 */

public class WorkAheadQueue<T> implements WorkAheadQueueADT<T>

{

  protected LinearNode<T> front;

  protected LinearNode<T> back;

  protected ArrayList<LinearNode<T>> frontThreeNodes;

  protected ArrayList<T> frontThreeElements;

  protected int numNodes = 0;

   

  /**

   * Default Constructor 

   */

  public WorkAheadQueue() {

    numNodes = 0;

    front = null;

    back = null;

  }


  /**

   * Removes and returns the element that is at place x in the queue.

   * Precondition: x must be less than 5, x must be less than size

   * Note: indexing from 0: 0 == front element, 1 == second element, etc.

   * 

   * @param x the passed in index of the element to be removed

   * @return the element removed from the queue

   * @throws EmptyCollectionException if the queue is empty

   * @throws InvalidArgumentException if x > 4, or x > size of collection

   * 

   */

  @Override

  public T dequeue(int x) throws EmptyCollectionException,

      InvalidArgumentException {

     

  }

   

  /**

   * Returns (without removing) the element that is at place x in the queue.

   * Precondition: x must be less than 5, x must be less than size

   * Note: indexing from 0: 0 == front element, 1 == second element, etc.

   * 

   * @return the element at the front of the queue

   * @throws EmptyCollectionException if the queue is empty

   * @throws InvalidArgumentException if x > 4, or x > size of collection

   * @param x the specified index of the element to return

   */

  @Override

  public T first(int x) throws EmptyCollectionException, 

      InvalidArgumentException {

   

  }

   

  /**

   * Returns an ArrayList of the first three nodes in the queue

   * 

   * @return ArrayList<LinearNode<T>> array list of nodes

   * @throws EmptyCollectionException if the queue is empty

   */

  @Override

  public ArrayList<LinearNode<T>> firstThreeNodes() throws 

      EmptyCollectionException {

     

  }


  /**

   * Returns an ArrayList of the first three elements in the queue

   * 

   * @return ArrayList<T> array list of elements 

   * @throws DataStructures.EmptyCollectionException 

   */

  @Override

  public ArrayList<T> firstThreeElements() throws EmptyCollectionException {

     

    if (isEmpty()) {

      throw new EmptyCollectionException ("firstThreeElements(): empty " 

          + "queue");

    }

     

    frontThreeElements.clear();

    LinearNode<T> curr = front;

     

    for (int i = 0; i < 3 && i < size(); i++) {

      frontThreeElements.add(i, curr.getElement());

      curr = curr.getNext();

    }

     

    return frontThreeElements;

  }

   

  /**

   * Adding a specific element to the end of the queue

   * 

   * @param element 

   */

  @Override

  public void enqueue(T element) {

    

  }

   

  /**

   * Removes the element at the front of queue and returns a reference to it

   * 

   * @return 

   * @throws EmptyCollectionException 

   */

  @Override

  public T dequeue() throws EmptyCollectionException {

     

  }

   

  /**

   * Returns a reference to the element at the front of the queue

   * 

   * @return

   * @throws EmptyCollectionException 

   */

  @Override

  public T first() throws EmptyCollectionException {

     

  }


  /**

   * Returns true if the collection contains no elements

   * 

   * @return true if the collection is empty 

   */

  @Override

  public boolean isEmpty() {

    return numNodes == 0;

     

  }

  /**

   * Returns the number of elements in the collection

   * 

   * @return the number of elements as an int  

   */

  @Override

  public int size() {

    return numNodes;

     

  }

   

  /**

   * Returns a string representation of the collection 

   * 

   * @return a string representation of the collection

   */

  @Override 

  public String toString() {

    StringBuilder sb = new StringBuilder("");

    LinearNode curr = front;

     

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

      sb.append(curr.getElement().toString());

      if (i < size() - 1) {

        sb.append (", ");

      }

      curr = curr.getNext();

    }

    return sb.toString();

  }

   

}

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes