Question: In this assignment, we want to implement data structure that is a hybrid of two interfaces, Set and Queue. A Set is simply a collection
In this assignment, we want to implement data structure that is a hybrid of two interfaces, Set and Queue. A Set is simply a collection with the added promise that duplicates are not allowed. A stack is a type of Queue (pronounced like "Q") where the Last element In is the First element Out (LIFO Queue - think of a stack of papers where you always add and take from the top). To make things easier we will implement the StackSet by extending ArrayList. That is, all methods of ArrayList are available to implement StackSet, but in order to meet the specifications of the Queue and Set interfaces, additional methods must be defined.
The entire code framework for this project is below. Just copy and paste and fill in the blanks for the following six functions.
Broken down a bit more explicitly...
1.The class as written below compiles, but the functions are incomplete stubs. Your goal is to fill in all six functions with the appropriate behavior. Note that three of the functions are almost identical to the other three with only a change in behavior upon an error.
2.Two of those functions, add and remove, will override the ArrayList implementation. Mainly, the add method needs to make sure that a Set and Queue is implemented - specifically, duplicates should not be allowed by the add function, and you should be adding/removing from the top of the stack. (Probably the end of the ArrayList, though you may implement it differently).
3.There are also additional methods, element and peek, which returns the top value of the stack without removing it from the stack.
4.Note that three of the methods methods - offer, poll, and peek - which are almost identical to add, remove, and element, respectively, except for the behavior when an operation is not possible. See with JCF API specification for the specific expected behavior.
5.the main method contains code to minimally test a subset of these functions. It explains in comments the type of output you should get. If you get the expected output, it is likely that you implemented add/removing/peek correctly.
Start Code:
import java.util.ArrayList; import java.util.Queue; import java.util.Set; import java.util.NoSuchElementException; public class StackSet
* but checks to see if there is a duplicate. * If there is, it should return false and not add anything,
* otherwise returns true.
* the Set specification states exceptions are optional if duplicates
* are detected. The Queue specification states an exception should be thrown,
* therefore this function should throw an exception when there is a duplicate. */ public boolean add(E e){ // next line just here to be able to compile return false; } /** * remove and return the top of the stack * the remove() method throws NoSuchElementException if it is empty */ public E remove() { // next line just here to be able to compile throw new NoSuchElementException(); }
/** * Retrieves, but does not remove, the head of this queue. * This method differs from the peek method only in that it * throws the NoSuchElementException exception if this queue is empty. */ public E element() { // just a stub throw new NoSuchElementException(); }
/** * the offer method function like the add method but
* does not throw an exception (instead returning false if it fails) */ public boolean offer(E e) { // just a stub return false; } /** * the poll method is similar to the remove method
* it retrieves and removes the head of this queue,
* but returns null if this queue is empty. */ public E poll() { // just a stub return null; }
/** * The peek method is similar to the element method
* It retrieves, but does not remove, the head of this queue,
* but returns null if this queue is empty. */ public E peek() { // next line just here to be able to compile return null; } //------------------------------------------------------------------------- public static void main (String args[]) { StackSet
Queue Interface Structure Type of Operation Throws exception Returns special value Insert Remove Examine add (e) remove () element() offer (e) poll () peek ()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
