Make a java Stack Interface (LIFO) class. Should use the methods outlines below
ID: 3757904 • Letter: M
Question
Make a java Stack Interface (LIFO) class. Should use the methods outlines below and work with the driver that is provided. If new methods are needed to complete the class than it's ok to add them!
Methods:
• void push( Object )
• Object pop()
• int size()
• String toString()
• boolean isEmpty()
• boolean equals( Object )
Driver:
public class StackDriver {
public static void main( String[] args ) {
Stack a = new Stack();
a.push( 'B' );
a.push( 'a' );
a.push( 't' );
System.out.println( a.toString() );
while( a.isEmpty() == false ) {
System.out.println( a.pop() );
}
}
}
Explanation / Answer
public class StackExample
{
private int maxSize;
private long[] stackArray;
private int top;
public StackExample(int s) {
maxSize = s;
stackArray = new long[maxSize];
top = -1;
}
public void push(Object j) {
stackArray[++top] = j;
}
public Object pop() {
return stackArray[top--];
}
public boolean isEmpty() {
return (top == -1);
}
public int size()
{
return maxSize;
}
public String toString()
{
String result = "";
for (int scan = 0; scan < top; scan++)
result = result + stackArray[scan].toString() + " ";
return result;
}
boolean equals( Object abc)
{
Object check=peek();
if(abc.equals(check))
return true;
else return false;
}
public Object peek() {
return stackArray[top];
}
public boolean isFull() {
return (top == maxSize - 1);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.