Write a Java class (MyStackApp) to emulate a stack structure using an array. Pro
ID: 3881669 • Letter: W
Question
Write a Java class (MyStackApp) to emulate a stack structure using an array. Provide methods for push(), pop(), isEmpty(), peek(), and size(). Use the postfix expression in the text (given below) to test your logic for evaluating the postfix form of the expression.. postfix: 3 4 * 2 5 + - 4 * 2 / Be sure to avoid error situations such as popping an empty stack, and the stack holding more than one item at the completion of the program. (Note: you should pop() he last value on the stack and display it as the final result, This should result in an empty stack). please solve using array
Explanation / Answer
1. MyStackApp class: To be defined in MyStackApp.java
----------------------------
package stackdemo;
/**
* Class emulating stack
*/
public class MyStackApp {
private int top; // will point to the topmost element of the stack
private Object[] storage; //array to hold elements of stack
private int size; //maximum size of stack
/**
* Constructor
*/
public MyStackApp(int n) {
// TODO Auto-generated constructor stub
size = n;
storage = new Object[size];
top = -1;
}
/**
* Clears the stack
*/
public void clear()
{
top = -1;
}
/**
* @return true is stack is full.
*/
public boolean isFull()
{
return (top >= storage.length-1);
}
/**
* @return true if stack is empty.
*/
public boolean isEmpty()
{
return (top == -1);
}
/**
* Pushes a new element into stack.
* @param el - element to push
* @throws Exception
*/
public void push(Object el) throws Exception
{
if (!isFull())
{
top++;
storage[top] = el;
}
else
throw new Exception("Overflow!");
}
/**
* Pops out top element from stack.
* @throws Exception
*/
public Object pop() throws Exception
{
if(!isEmpty())
{
Object tmp = storage[top];
top--;
return tmp;
}
else
throw new Exception("Underflow");
}
/**
* @return top element of stack without popping.
*/
public Object peek()
{
if(!isEmpty())
{
return storage[top];
}
else
return null;
}
/**
* @return size of stack.
*/
public int size()
{
return size;
}
}
-----------------------------
2. MyStackAppDemo class: To be defined in MyStackAppDemo.java
-----------------------------------------------
package stackdemo;
/**
* Class to show the use of stack through postfix expression evaluation
*
*/
public class MyStackAppDemo {
//check whether a character is operand
private static boolean isOperand(char c)
{
int operand=c-'0';
if(operand >= 0 && operand <= 9)
return true;
else
return false;
}
//checks whether a character is operator
private static boolean isOperator(char c)
{
if(c=='+' || c=='-' || c=='*' || c=='/')
return true;
else
return false;
}
/**
* Evaluates a postfix expression
* @param postfix - expression in postfix notation
* @return the result of postfix expression evaluation
* @throws Exception
*/
public static int evaluatePostfixExpression(String postfix) throws Exception
{
int result=0;
MyStackApp stack = new MyStackApp(50); //stack as a buffer for elements of postfic expression
//loop to parse postfix expression
for(int i=0;i<postfix.length();i++)
{
char el=postfix.charAt(i);
if (isOperand(el)) //if is operand, push to stack
{
stack.push(el - '0'); // convert char to int
}
else if (isOperator(el)) //if is operator, pops two top operands from stack and evaluate. Push result back to stack
{
int opr1 = (int) stack.pop();
int opr2 = (int) stack.pop();
int value;
switch (el)
{
case '+':
value = opr2 + opr1;
stack.push(value);
break;
case '-':
value = opr2 - opr1;
stack.push(value);
break;
case '*':
value = opr2 * opr1;
stack.push(value);
break;
case '/':
value = opr2 / opr1;
stack.push(value);
break;
}
}
}
result = (int)stack.pop();
return result;
}
/**
* main() function
*/
public static void main(String[] args) {
String postfixExpression="34*25+-4*2/";
System.out.println("Postfix expression is "+postfixExpression);
try
{
System.out.println("Evaluating postfix expression...");
int value=evaluatePostfixExpression(postfixExpression);
System.out.println("Result:"+value);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.