Using the algorithm evaluatePostfix, given in the photo below, Write a class in
ID: 3678190 • Letter: U
Question
Using the algorithm evaluatePostfix, given in the photo below, Write a class in java that evaluate each of thefollowing postfix expressions. Assume that a = 2, b = 3, c = 4, d = 5, and e = 6.
a.) a b + c * d -
5.18 The evaluation algorithm follows direci Algorithm evaluatePostfix(postfix) siO valueStack a new empty stack while (postfix has characters left to parse) nextCharacter- nex nonblank character of postfix switch (nextCharacter) case variable: valueStack.push(value of the variable nextCharacter) break + case operandTwo - valueStack.pop(O operandOne - valueStack.popO case case case ' the result ofthe operation in nextchar acter and its operands operandone and operandTwo result valueStack.push(result) break default: break // Ignore unexpected characters return valueStack.peekOExplanation / Answer
Program to Evalute the postfix expression
PostfixEvaluation.java
package org.students;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.Scanner;
class PostfixEvaluation
{
public static void main(String[] args)throws IOException
{
String input;
//Always this loop will execute.
while(true)
{
System.out.print("Enter the postfix expresion (Without spaces):");
input=getString();
// if the user didn't entered any string,program will exit.
if(input.equals(""))
{ System.out.println(":: Program Exit::");
break;
}
// Create the Evaluation class object.
Evaluation e=new Evaluation();
// call the calculate method by passing the input string as
// argument.
System.out.println("Result:: "+e.calculate(input));
System.out.print("Do you want to continue(Y/N):");
Scanner sc1 = new Scanner(System.in);
// read the character entered by the user as input.
char c = sc1.next(".").charAt(0);
// check whether the character is not equal to 'Y' or 'y'
if (c == 'y' || c == 'Y')
{
continue;
}
else
{
System.out.println("* Program Exit *");
break;
}
}
}
public static String getString()throws IOException
{
DataInputStream inp=new DataInputStream(System.in);
String s=inp.readLine();
return s;
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Evaluation.java
package org.students;
class Evaluation{
public int calculate(String s)
{
int n,r=0;
//find the length of the string.
n=s.length();
//create a stack
Stack a=new Stack(n);
//take each character from string using charAt() method .
for(int i=0;i<n;i++)
{
char ch=s.charAt(i);
//whether the character is number between 0-9(inclusive).
if(ch>='0'&&ch<='9')
//if it is number store it into the stack.
a.push((int)(ch-'0'));
else
{
//if the character is an operator symbol then get the operands from the stack.
//pop() method will delete the top most operand from the stack and return it.
//delete the top most operand from the stack and the returned value stored it in a variable x.
int x=a.pop();
//delete the top most operand from the stack
//and the returned value stored it in a variable y.
int y=a.pop();
//execute the switch block if the character is operator.
switch(ch)
{
case '+':r=x+y;
break;
case '-':r=y-x;
break;
case '*':r=x*y;
break;
case '/':r=y/x;
break;
default:r=0;
}
a.push(r);
}
}
r=a.pop();
return(r);
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Stack.java
package org.students;
import java.io.*;
class Stack
{
private int[] a;
private int top,m;
public Stack(int max)
{
m=max;
a=new int[m];
top=-1;
}
public void push(int key)
{
a[++top]=key;
}
public int pop()
{
return(a[top--]);
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Input: 23+4*5- (this is the input)
Enter the postfix expresion :23+4*5-
Result:: 15
Do you want to continue(Y/N):n
* Program Exit *
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.