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 this question is as follows:
**********************************************************************************************************************************************
import java.util.Stack;
import java.util.Scanner;
public class Main
{
// Method to evaluate value of a postfix
expression(i.e. the static evaluate function given)
static int evaluate(String expression){
Stack<Integer>
stackforOperand = new Stack<>(); //create a stack class
object with Integer data type in it
for(int i=0 ;
i<expression.length() ; i++){ // Scan all characters one by
one
char c =
expression.charAt(i); //taking character at position i
if(Character.isDigit(c)) // If the scanned character is an operand
(number here),
stackforOperand.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 =
stackforOperand.pop(); //pop first element
int val2 = stackforOperand.pop(); //pop second
element
switch(c){ //checking whether the operator is
+,-,*,/
case '+':
stackforOperand.push(val2+val1); //when + , add both elements add
to stack
break;
case '-':
stackforOperand.push(val2-
val1); //when - , subtract both elements add to stack
break;
case '/':
stackforOperand.push(val2/val1); //when / , divide both elements
add to stack
break;
case '*':
stackforOperand.push(val2*val1); //when * , multiply both elements
add to stack
break;
}
}
}
return
stackforOperand.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
}
}
**********************************************************************************************************************************************Here is the screenshot of the code for further reference:
The output for the above program is as follows :