Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

package jsjf; import jsjf.exceptions.*; import java.util.Iterator; /** * Represe

ID: 3597870 • Letter: P

Question

package jsjf;

import jsjf.exceptions.*;

import java.util.Iterator;

/**

* Represents a linked implementation of a stack.

*

* @author Java Foundations

* @version 4.0

*/

public class LinkedStack<T> implements StackADT<T>

{

private int count;  

private LinearNode<T> top;

/**

* Creates an empty stack.

*/

public LinkedStack()

{

count = 0;

top = null;

}

/**

* Adds the specified element to the top of this stack.

* @param element element to be pushed on stack

*/

public void push(T element)

{

LinearNode<T> temp = new LinearNode<T>(element);

temp.setNext(top);

top = temp;

count++;

}

/**

* Removes the element at the top of this stack and returns a

* reference to it.

* @return element from top of stack

* @throws EmptyCollectionException if the stack is empty

*/

public T pop() throws EmptyCollectionException

{

if (isEmpty())

throw new EmptyCollectionException("stack");

T result = top.getElement();

top = top.getNext();

count--;

return result;

}

/**

* Returns a reference to the element at the top of this stack.

* The element is not removed from the stack.  

* @return element on top of stack

* @throws EmptyCollectionException if the stack is empty  

*/

public T peek() throws EmptyCollectionException

{

return this.top.getElement();

}

/**

* Returns true if this stack is empty and false otherwise.

* @return true if stack is empty

*/

public boolean isEmpty()

{

if (this.top.getNext() == null) {

return true;

}

  

return false;

  

}

/**

* Returns the number of elements in this stack.

* @return number of elements in the stack

*/

public int size()

{

return this.count;

}

/**

* Returns a string representation of this stack.

* @return string representation of the stack

*/

public String toString()

{

String ret = "top ";

LinearNode<T> current = top;

while (current!= null ) {

ret += current.getElement() + " ";

current = current.getNext();

}

return ret += "bottom";

}

}

Write a class called StackHand which implements the HandofCards interface using the LinkedStack class from the jsjf package. Include the following methods: ·play() which removes a card from the hand and returns the card ·size() which returns the number of cards in the hand

Explanation / Answer

package jsjf;
import jsjf.LinkedList.LinkedListIterator;
import jsjf.exceptions.*;

import java.util.ConcurrentModificationException;
import java.util.EmptyStackException;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* Represents a linked implementation of a stack.
*
* @author Java Foundations
* @version 4.0
*/
public class LinkedDropOutStack implements StackADT
{
private int count;
private LinearNode top;
private int limit = 5;

/**
* Creates an empty stack.
*/
public LinkedDropOutStack()
{
count = 0;
top = null;
}

/**
* Adds the specified element to the top of this stack.
* @param element element to be pushed on stack
*/
public void push(T element)
{
if (count < limit) {
LinearNode temp = new LinearNode(element);
temp.setNext(top);
top = temp;
}
else if (count == limit){
LinearNode temp = new LinearNode(element);
if (isEmpty())
throw new EmptyCollectionException("stack");
T result = top.getElement();
top = top.getNext();
temp.setNext(top);
top = temp;
}
count++;
}
/**
* Removes the element at the top of this stack and returns a
* reference to it.
* @return element from top of stack
* @throws EmptyCollectionException if the stack is empty
*/
public T pop() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException("stack");
T result = top.getElement();
top = top.getNext();
count--;
return result;
}
/**
* Returns a reference to the element at the top of this stack.
* The element is not removed from the stack.
* @return element on top of stack
* @throws EmptyCollectionException if the stack is empty
*/
public T peek() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException("Stack");
else
return top.getElement(); // temp
}
/**
* Returns true if this stack is empty and false otherwise.
* @return true if stack is empty
*/
public boolean isEmpty()
{
if (count == 0) {
return true; // temp
}
else
return false;
}
/**
* Returns the number of elements in this stack.
* @return number of elements in the stack
*/
public int size()
{
// To be completed as a Programming Project

return count - 1; // temp
}
/**
* Returns a string representation of this stack.
* @return string representation of the stack
*/
public String toString()
{
String result = "";
LinearNode values = top;
while (values != null){
result += values.getElement().toString() + " ";
values = values.getNext();
}

return result; // temp
}

public Iterator iterator()
{
return new StackIterator();
}
/**
* LinkedIterator represents an iterator for a linked list of linear nodes.
*/
private class StackIterator implements Iterator
{

private int iteratorModCount; // the number of elements in the collection
private LinearNode current; // the current position
/**
* Sets up this iterator using the specified items.
*
* @param collection the collection the iterator will move over
* @param size the integer size of the collection
*/
public StackIterator()
{
current = top;
iteratorModCount = count;
}
/**
* Returns true if this iterator has at least one more element
* to deliver in the iteration.
*
* @return true if this iterator has at least one more element to deliver
* in the iteration
* @throws ConcurrentModificationException if the collection has changed
* while the iterator is in use
*/
public boolean hasNext() throws ConcurrentModificationException
{
if (iteratorModCount != count)
throw new ConcurrentModificationException();
return (current != null);
}
/**
* Returns the next element in the iteration. If there are no
* more elements in this iteration, a NoSuchElementException is
* thrown.
*
* @return the next element in the iteration
* @throws NoSuchElementException if the iterator is empty
*/
public T next() throws ConcurrentModificationException
{
if (!hasNext())
throw new NoSuchElementException();
T result = current.getElement();
current = current.getNext();
return result;
}
/**
* The remove operation is not supported.
*
* @throws UnsupportedOperationException if the remove operation is called
*/
public void remove() throws UnsupportedOperationException
{
throw new UnsupportedOperationException();
}
}


}