Question
Can you please complete it in java and add comments explaining the program so I can understand it.
PROBLEM: Evaluate a prefix expression. The operands in the expression are single digit whole numbers. The operators are binar
Test cases:

TEST INPUT -+*43-7*31 *28 @73 2@25 1 a-49*63+75 - * 3 7 28a-51 +34+896 -35-48+7*34+2 65 TEST OUTPUT: #2. -2 #3. 12 #4. 50 #5

Test case 2 input

*45 31 @ - 897+ 4 2 @ 35 - *2 410 *4 @ 57*32+19 * 4 6 9@- 3 817 2
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Java program

import java.util.*;
  
public class Prefix {
   public static void main(String[] args)
   {
   String expression = "@ - 8 9 7 + 4 2";
   System.out.println(evaluatePrefix(expression));
     
   expression = "* + @ 4 6 9 @ - 3 8 1 7 2";
   System.out.println(evaluatePrefix(expression));
   }
  


static double evaluatePrefix(String expression)
{
Stack<Double> Stack = new Stack<Double>();

for (int j = expression.length() - 1; j >= 0; j--) {

// Push operand to Stack
// To convert expression[j] to digit subtract
// '0' from expression[j].
if (isOperand(expression.charAt(j)))
Stack.push((double)(expression.charAt(j) - '0'));
else if(expression.charAt(j)==' ')continue;

else {

// Operator encountered
// Pop two elements from Stack
double operand1 = Stack.peek();
Stack.pop();
double operand2 = Stack.peek();
Stack.pop();

// Use switch case to operate on operand1
// and operand2 and perform operand1 Operator operand2.
switch (expression.charAt(j)) {
case '+':
Stack.push(operand1 + operand2);
break;
case '-':
Stack.push(operand1 - operand2);
break;
case '*':
Stack.push(operand1 * operand2);
break;
case '/':
Stack.push(operand1 / operand2);
break;
case '@':
   double operand3 = Stack.peek();
   Stack.pop();
   if(operand1>=0)Stack.push(operand2);
   else Stack.push(operand3);
}
}
}

return Stack.peek();
}
static Boolean isOperand(char c)
{
if(c >= '0' && c <= '9') return true;
return false;
}
  


}

//sample output

<terminated> Prefix [Java Application] C:Program Files\Javaljdk1.8.0 151 6.0 26.0 94-

Add a comment
Know the answer?
Add Answer to:
Can you please complete it in java and add comments explaining the program so I can understand it...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • EVALUATING GENERAL INFIX EXPRESSIONS INTRODUCTION The notation in which we usually write arithmetic expressions is called infix notation;...

    EVALUATING GENERAL INFIX EXPRESSIONS INTRODUCTION The notation in which we usually write arithmetic expressions is called infix notation; in it, operators are written between their operands: X + Y. Such expressions can be ambiguous; do we add or multiply first in the expression 5 + 3 * 2? Parentheses and rules of precedence and association clarify such ambiguities: multiplication and division take precedence over addition and subtraction, and operators associate from left to right. This project implements and exercises a stack-based algorithm that evaluates...

  • JAVA, please You must write a robust program meaning that your program should not crash with...

    JAVA, please You must write a robust program meaning that your program should not crash with any given data. Data validation must be done any time that user enters an input. Write a program that 1. Gets an infix expression form the user and evaluate the expression using stack ADT a. Finds the postfix equivalent of the given infix expression b. Evaluate the created postfix expression. c. Note: your program should not crash when you enter an invalid expression such...

  • I'm having trouble writing this code, can some help me? Step 1: Capturing the input The...

    I'm having trouble writing this code, can some help me? Step 1: Capturing the input The first step is to write a functionGetInput()that inputs the expression from the keyboard and returns the tokens---the operands and operators---in a queue. You must write this function. To make the input easier to process, the operands and operators will be separated by one or more spaces, and the expression will be followed by #. For example, here’s a valid input to your program: 6...

  • Total point: 15 Introduction: For this assignment you have to write a c program that will...

    Total point: 15 Introduction: For this assignment you have to write a c program that will take an infix expression as input and display the postfix expression of the input. After converting the postfix expression, the program should evaluate the expression from the postfix and display the result. What should you submit? Write all the code in a single file and upload the .c file. Problem We as humans write math expression in infix notation, e.g. 5 + 2 (the...

  • Python Issue Postfix notation (also known as Reverse Polish Notation or RPN in short) is a...

    Python Issue Postfix notation (also known as Reverse Polish Notation or RPN in short) is a mathematical notation in which operators follow all of its operands. It is different from infix notation in which operators are placed between its operands. The algorithm to evaluate any postfix expression is based on stack and is pretty simple: Initialize empty stack For every token in the postfix expression (scanned from left to right): If the token is an operand (number), push it on...

  • You are to write a program that implements a Reverse Polish Notation Calculator in C using...

    You are to write a program that implements a Reverse Polish Notation Calculator in C using BISON and FLEX, You only have to edit the BISON and FLEX files. Link to the files to start and have a general view of the program: https://www.dropbox.com/sh/83yzs66jhftqj5b/AABZcY9Qwl84JdUFnYpQaZk9a?dl=0 Reverse Polish Notation is a mathematical notation in which every operator follows all of its operands. It is sometimes called postfix notation, and does not require any parentheses as long as each operator has a fixed...

  • Write a java program for the following: Your program reads an infix expression represented by a...

    Write a java program for the following: Your program reads an infix expression represented by a string S from the standard input (the keyboard). Then your program converts the infix expression into a postfix expression P using the algorithm. Next, your program evaluates the postfix expression P to produce a single result R. At last, your program displays the original infix expression S, the corresponding postfix expression P and the final result R on the standard output ( the screen...

  • Here is the code I have so far. I'm trying to figure out how to implement...

    Here is the code I have so far. I'm trying to figure out how to implement a boolean and use precedence. The 2nd expression should be 14 but it comes out as 28 so I'm definitely not understanding. #include <stack> #include <iostream> #include <string> using namespace std; // Function to find precedence of // operators. int precedence(char op) {    if (op == '+' || op == '-')        return 1;    if (op == '*' || op ==...

  • Question: Write a Java program for Evaluating Postfix Expression 1. Input a postfix expression from user....

    Question: Write a Java program for Evaluating Postfix Expression 1. Input a postfix expression from user. 2. Evaluate expression using double stack made of your own linked list. 3. Show the result of expression or error if incorrect. Evaluating Postfix Expression Input an expression: 2 10 + 9 6 - / Evaluating Postfix Expression Input an expression: 20 10 + 9 6 - 1 Evaluating Postfix Expression Input an expression: 2 10 + 9 - / Result = 4.0 Result...

  • In C programming Language Write a version of the infix-to-postfix conversion algorithm. Write a program that converts an...

    In C programming Language Write a version of the infix-to-postfix conversion algorithm. Write a program that converts an ordinary infix arithmetic expression (assume a valid expression is entered) with single-digit integers For Example: Infix expression (6 + 2) * 5 - 8 / 4 to a postfix expression is  62+5*84/- The program should read the expression into character array infix and use the stack functions implemented in this chapter to help create the postfix expression in character array postfix. The...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT