2 points it is possible to create a class that

Info icon This preview shows pages 3–8. Sign up to view the full content.

View Full Document Right Arrow Icon
(2 points) It is possible to create a class that implements both the Stack<A> and Set<Integer> interfaces. (true or false) 3
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
In the rest of this problem, you will implement the Stack<A> interface without using the java.util libraries. The class is called LinkedStack<A> , and it will use a singly-linked, mutable data struc- ture similar to the queue and deque types from homework 5. Step 3: Write test cases. A stack is supposed to exhibit “last in, first out” (LIFO) behavior: the most recent element pushed is the next one that will be popped. Complete the following test cases that demonstrate this behavior. We have also provided two complete example tests. Rubric: 1 point per blank (6 points total) import ...; import java.util.NoSuchElementException; public class LinkedStackTest { @Test public void testIsEmpty() { LinkedStack<Integer> s = new LinkedStack<Integer>(); assertEquals( true , s.isEmpty()); } @Test public void testPopEmptyException() { LinkedStack<Integer> s = new LinkedStack<Integer>(); try { s.pop(); Assert.fail(); } catch (NoSuchElementException e) { // expected outcome } } // ----------------- FILL IN HOLES BELOW ----------------------- @Test public void testPushIsNotEmpty() { LinkedStack<Integer> s = new LinkedStack<Integer>(); s.push(3); assertEquals( false , s.isEmpty()); } @Test public void testPushPushPopPop() { LinkedStack<Integer> s = new LinkedStack<Integer>(); s.push(3); s.push(4); assertEquals((Integer) 4, s.pop()); assertEquals((Integer) 3, s.pop()); assertEquals( true , s.isEmpty()); } } 4
Image of page 4
Step 4: Implement it. Implement the LinkedStack<A> class. Use an auxiliary inner class called Node to store the (singly-linked, mutable) stack structure—it doesn’t need any methods. Do not use any library classes ; do not use arrays; you should not need to catch any exceptions. The LinkedStack<A> default constructor is sufficient. Your code should pass the tests given above. import java.util.NoSuchElementException; public class LinkedStack<A> implements Stack<A> { // field (s) of the LinkedStack < A > class private Node top; // private class of Node data private class Node { // field (s) of the Node class A elt; Node next; // constructor of the Node class Node(A elt, Node next) { this .elt = elt; this .next = next; } } // returns true if the Stack is empty public boolean isEmpty() { return (top == null ); } // pushes an element onto the top of the stack public void push(A elt) { Node newTop = new Node(elt, top); top = newTop; } // pops the top element off the stack , updating // the stack in place // throws a NoSuchElementException if the stack is empty public A pop() { if ( this .isEmpty()) throw new NoSuchElementException(); Node t = top; top = t.next; return t.elt; } } Rubric: 2 points for private field (1 pt for private, 1 pt for field) 4 points for node class (1 pt for each field, 2 pts for correct constructor) 2 points for isEmpty 5
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
4 points for push (2 pts for new node, 2 pts for reassign) 6 points for pop (2 pts for exception, 2 pts for top/t.next , 2 pts for returning t.elt not t ) 6
Image of page 6
3. Java Programming (20 points) In this problem, you will write a Java program that uses the Stack<A> abstraction from Problem 2.
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern