final-fa12-solutions

2 points it is possible to create a class that

Info iconThis 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<A> interfaces. (true or false) c. T F (2 points) It is possible to create a class that implements both the Stack<A> and Set<Integer> interfaces. (true or false) 3
Background image of page 3

Info iconThis 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
Background 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
Background image of page 5

Info iconThis 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
Background image of page 6
3. Java Programming
Background image of page 7

Info iconThis 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 ]}

Page3 / 17

2 points It is possible to create a class that implements...

This preview shows document pages 3 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online