package PJ2; public class MyStack implements StackInterface { // Data fields pri
ID: 3760896 • Letter: P
Question
package PJ2;
public class MyStack implements StackInterface
{
// Data fields
private Node topNode; // references the first node in the chain
private int numberOfEntries;
public MyStack()
{
topNode = null;
} // end default constructor
public void push(T newEntry)
{
// add stataments
Node newNode = new Node(newEntry, topNode);
topNode = newNode;
} // end push
public T peek()
{
// add stataments
T top = null;
if (topNode != null)
top = topNode.getData();
return top;
} // end peek
public T pop()
{
T top = peek();
if (topNode != null)
topNode = topNode.getNextNode();
return top;
} // end pop
public boolean empty()
{
// add stataments
return (topNode == null);
} // end empty
public int size()
{
// add stataments
int count = 0;
for (Node node = topNode; node != null; node = topNode.getNextNode()) {
count++;
}
return count;
} // end isEmpty
public void clear()
{
// add stataments
topNode = null;
} // end clear
public String toString()
{
// add stataments
return "stack: " + topNode.toString();
}
/****************************************************
Do not modify: Stack test
Expected output:
OK: stack is empty
Push 3 data: 10, 30, 50
Print stack [50,30,10,]
OK: stack size is 3
OK: peek stack top is 50
OK: stack is not empty
Pop 2 data: 50, 30
Print stack [30,10,]
Print stack [10,]
OK: stack pop data is 30
Clear stack
Print stack []
******************************************************/
public static void main (String args[])
{
MyStack s = new MyStack();
if (s.empty())
System.out.println("OK: stack is empty");
else
System.out.println("Error: stack is not empty");
s.push(10);
s.push(30);
s.push(50);
System.out.println("Push 3 data: 10, 30, 50");
System.out.println("Print stack " + s);
if (s.size() == 3)
System.out.println("OK: stack size is 3");
else
System.out.println("Error: stack size is " + s.size());
if (s.peek() == 50)
System.out.println("OK: peek stack top is 50");
else
System.out.println("Error: peek stack top is " + s.size());
if (!s.empty())
System.out.println("OK: stack is not empty");
else
System.out.println("Error: stack is empty");
System.out.println("Pop 2 data: 50, 30");
s.pop();
System.out.println("Print stack " + s);
int data=s.pop();
System.out.println("Print stack " + s);
if (data == 30)
System.out.println("OK: stack pop data is 30");
else
System.out.println("Error: stack pop data is " + data);
System.out.println("Clear stack");
s.clear();
System.out.println("Print stack " + s);
}
} // end Stack
Hi,
I want to make sure this works.
Explanation / Answer
package javafiles;
Program code:
import java.util.NoSuchElementException;
public class MyStack implements StackInterface<Object>
{
// Data fields
private Object[] stackArray1;
private int size = 0;
public MyStack(int sizeOfArray){
stackArray1=new Object[sizeOfArray];
}
public void push(Object item) {
// TODO Auto-generated method stub
if(size==stackArray1.length)
{
throw new IllegalStateException("cannot push onto a full stack");
}
stackArray1[size++] = item;
}
public Object pop() {
if (size == 0) {
throw new NoSuchElementException("Cannot pop from empty stack");
}
Object result = stackArray1[size-1];
stackArray1[--size] = null;
return result;
// TODO Auto-generated method stub
}
public Object peek() {
if (size == 0) {
throw new NoSuchElementException("Cannot peek into empty stack");
}
return stackArray1[size - 1];
}
public boolean isEmpty() {
// TODO Auto-generated method stub
return size == 0;
}
public void clear() {
for(int i=0;i<stackArray1.length;i++)
{
pop();
}
// TODO Auto-generated method stub
}
public static void main (String args[])
{
StackInterface<Object> s = new MyStack(10);
if (s.isEmpty())
System.out.println("OK: stack is empty");
else
System.out.println("Error: stack is not empty");
s.push(10);
s.push(30);
s.push(50);
System.out.println("Push 3 data: 10, 30, 50");
System.out.println("Print stack" + s.size());
if (s.size() == 3)
System.out.println("OK: stack size is 3");
else
System.out.println("Error: stack size is " + s.size());
if (s.peek()==(Object)50)
System.out.println("OK: peek stack top is 50");
else
System.out.println("Error: peek stack top is " + s.size());
if (!s.isEmpty())
System.out.println("OK: stack is not empty");
else
System.out.println("Error: stack is empty");
System.out.println("Pop 2 data: 50, 30");
s.pop();
System.out.println("Print stack " + s);
int data=(int) s.pop();
System.out.println("Print stack " + s);
if (data == 30)
System.out.println("OK: stack pop data is 30");
else
System.out.println("Error: stack pop data is " + data);
System.out.println("Clear stack");
s.clear();
System.out.println("Print stack " + s);
}
@Override
public int size() {
// TODO Auto-generated method stub
return size;
}
}
Sample output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.