Write a segment of code to perform each of the following operations. Assume mySt
ID: 3531673 • Letter: W
Question
Write a segment of code to perform each of the following operations. Assume myStack is an object of class ArrayStack. You may call any of the public methods of ArrayStack. You may declare additional stack objects.
a) Set secondElement to the second element from the top of myStack, leaving myStack without its original top two elements
b) Set bottom equal to the bottom element in myStack, leaving myStack empty.
c)Set bottom equal to the bottom element in myStack, leaving myStack unchanged.
d) Print out the contents of myStack, leaving myStack unchanged.
Thanks in advance.
Explanation / Answer
/*Complete code*/
/*Array Stck class*/
class ArrayStack
{
private Object[] stack; // Array that holds stack elements
private int topIndex = -1; // index of top element in stack
// Constructors
public ArrayStack()
{
stack = new Object[100];
}
public ArrayStack(int maxSize)
{
stack = new Object[maxSize];
}
public void push(Object item)throws Exception
// Adds an element to the top of this stack
{
if (!isFull())
{
topIndex++;
stack[topIndex] = item;
}
else
throw new Exception("Push attempted on a full stack.");
}
public Object pop()throws Exception
// Removes an element from the top of this stack
{
if (!isEmpty())
{
Object x=stack[topIndex];
stack[topIndex] = null;
topIndex--;
return x;
}
else
throw new Exception("Pop attempted on an empty stack.");
}
public Object top()throws Exception
// Returns the element on top of this stack
{
Object topOfStack = null;
if (!isEmpty())
topOfStack = stack[topIndex];
else
throw new Exception("Top attempted on an empty stack.");
return topOfStack;
}
public void printStack(){
if(isEmpty()){
System.out.println("Stack is empty");
}
for(int i=topIndex;i>=0;i--){
System.out.println(stack[i].toString());
}
}
public boolean isEmpty()
// Checks if this stack is empty
{
if (topIndex == -1)
return true;
else
return false;
}
public boolean isFull()
// Checks if this stack is full
{
if (topIndex == (stack.length - 1))
return true;
else
return false;
}
}
/*Main Class to test program*/
public class StackTest {
public static void main(String args[])throws Exception{
ArrayStack a=new ArrayStack();
a.push("4");
a.push("3");
a.push("2");
a.push("1");
/* Answer to d part of question*/
a.printStack();//displayed
//part a)
String first=a.pop().toString();
String secondElement=a.pop().toString();
System.out.println("secondElement is:"+ secondElement);
//changing stack back to original
a.push(secondElement);
a.push(first);
System.out.println("a is:");
a.printStack();
//part a complete
//part b and c
ArrayStack b=new ArrayStack();//using another stack
while(!a.isEmpty()){
b.push(a.pop());//poping from a and pusing to b
}
String bottom=b.pop().toString();
System.out.println("bottom is:"+ bottom);
//part b complete a is empty and bottom element found
//for part c changing stack to original
a.push(bottom);
while(!b.isEmpty()){
a.push(b.pop());//poping from b and pusing to a
}
//just to make sure
System.out.println("a is:");
a.printStack();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.