In: Computer Science
Please use Java language in an easy way with comments! Thanks!
Write a static method called "evaluate" that takes a string as a parameter. The string will contain a postfix expression, consisting only of integer operands and the arithmetic operators +, -, *, and / (representing addition, subtraction, multiplication, and division respectively). All operations should be performed as integer operations. You may assume that the input string contains a properly-formed postfix expression. The method should return the integer that the expression evaluates to. The method MUST use a stack to perform the evaluation. Specifically, you should use the Stack class from the java.util package (For this problem, you must write a static "evaluate" method).
Also, in the same file, write a program that prompts the user to enter a postfix expression. Your program should evaluate the expression and output the result. Your program should call the "evaluate" method described above. Here are some examples:
Postfix expression: 1 2 + Result: 3 Postfix expression: 1 2 + 3 4 - * Result: -3 Postfix expression: 3 4 5 + + 1 - Result: 11
The java program for your query is given below:
**********************************************************************************************************************************************
import java.util.Stack;
import java.util.Scanner;
public class Main
{
// Method to evaluate value of a postfix
expression
static int evaluate(String exp){
Stack<Integer> stack = new
Stack<>(); //create a stack class object with Integer data
type in it
for(int i=0 ; i<exp.length() ;
i++){ // Scan all characters one by one
char c =
exp.charAt(i); //taking character at position i
if(Character.isDigit(c)) // If the scanned character is an operand
(number here),
stack.push(c -
'0'); // push it to the stack.
// If the
scanned character is an operator, pop two
// elements from
stack apply the operator
else{
int val1 =
stack.pop(); //pop first element
int val2 = stack.pop(); //pop second
element
switch(c){ //checking whether the operator is
+,-,*,/
case '+':
stack.push(val2+val1); //when
+ , add both elements add to stack
break;
case '-':
stack.push(val2- val1);
//when - , subtract both elements add to stack
break;
case '/':
stack.push(val2/val1); //when
/ , divide both elements add to stack
break;
case '*':
stack.push(val2*val1); //when
* , multiply both elements add to stack
break;
}
}
}
return stack.pop();
//pop from the stack and return
}
// Driver program to test above functions
public static void main(String[] args)
{
System.out.print("Postfix
Expression:"); //prompt the user for Postfix Expression
Scanner input = new
Scanner(System.in); //defining scanner class object
String expression =
input.nextLine(); //taking input of expression with scanner
object
System.out.println("Result:
"+evaluate(expression)); //Print Result
}
}
**********************************************************************************************************************************************
Providing screenshot of the code for further reference:
Output of the program :