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.In this assignment, we want to implement data structure that is a

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 extends ArrayList implements Queue, Set { // ------------------------------------------------------------------------ // you need to fill out the following six methods // ------------------------------------------------------------------------ /** * this adds an element to the top of the stack,

* 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 stringSS = new StackSet(); stringSS.add("StackSet"); stringSS.add("the"); stringSS.add("Dodgers"); stringSS.remove(); // should remove "Dodgers" stringSS.offer("StackSet"); // should fail because it's a duplicate stringSS.add("Testing"); // should output "Testing: Testing the SetStack" System.out.println(stringSS.peek() + ": " + stringSS.remove() + " " + stringSS.remove() + " " + stringSS.remove()); StackSet intSS = new StackSet(); intSS.add(5); // autoboxing int --> Integer intSS.clear(); // still works - inherited from ArrayList intSS.add(7); intSS.add(8); intSS.add(9); intSS.offer(7); // should fail because it's a duplicate intSS.remove(); // should remove the 9 int total = 0; while (! intSS.isEmpty()) { // inherited from ArrayList total += intSS.remove().intValue(); } // should output 15 System.out.println("The total of the integer stackset is: " + total); } }

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

1 Expert Approved Answer
Step: 1 Unlock 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 Databases Questions!