Create an appropriate Java Postfix calculator class using the algorithm below. Y
ID: 3893157 • Letter: C
Question
Create an appropriate Java Postfix calculator class using the algorithm below. Your program should accept at least five elements (data1, data2, data3, data4, data5) and perform an appropriate postfix operation given the following scenarios:
data1 data2 + data3 * data4 –
data1 data2 * data3 * data1 – / data4 data5* +
Use the following values for each input:
data1 = 1;
data2 = 2;
data3 = 4
data4 = 5
data5 = 3
ALGORITHM
Algorithm evaluatePostfix(postfix)
// Evaluates a postfix expression.
valueStack = a new empty stack
while (postfix has characters left to parse)
{
nextCharacter = next nonblank character of postfix
switch (nextCharacter)
{
case variable:
valueStack.push(value of the variable nextCharacter)
break
case '+' : case '-' : case '*' : case '/' : case '^' :
operandTwo = valueStack.pop()
operandOne = valueStack.pop()
result = the result of the operation in nextCharacter and its operands
operandOne and operandTwo
valueStack.push(result)
break
default: break
}
}
return valueStack.peek()
Explanation / Answer
Below is your code. Please note that out of the two expressions provided by you for testing one is invalid(Not a proper prostfix expression). My code is handling that condition and printing "Invalid Expression".
Please let me know in comments if you have any issues
public class Postfix {
public static void main(String[] args) {
int data1 = 1, data2 = 2, data3 = 4, data4 = 5, data5 = 3;
String expression = String.valueOf(data1) + String.valueOf(data2) + "+" + String.valueOf(data3) + "*"
+ String.valueOf(data4) + "-";
if (postfix(expression) != -1)
System.out.println("data1 data2 + data3 * data4 -(" + (expression) + ") = " + postfix(expression));
else
System.out.println("data1 data2 + data3 * data4 -(" + (expression) + ") = Invalid Expression");
String expression3 = String.valueOf(data1) + String.valueOf(data2) + "*" + String.valueOf(data3) + "*"
+ String.valueOf(data1) + "-" + "/" + String.valueOf(data4) + String.valueOf(data5) + "*" + "+";
if (postfix(expression3) != -1)
System.out.println(
"data1 data2 * data3 * data1-/ data4 data5*+(" + (expression3) + ")= " + postfix(expression3));
else
System.out
.println("data1 data2 * data3 * data1-/ data4 data5*+(" + (expression3) + ")= Invalid Expression");
String expression2 = String.valueOf(data4) + String.valueOf(data5) + "+" + String.valueOf(data3)
+ String.valueOf(data1) + "*" + "/" + String.valueOf(data1) + "+";
if (postfix(expression2) != -1)
System.out
.println("data4 data5 + data3 data1 * / data1 +(" + (expression2) + ")= " + postfix(expression2));
else
System.out.println("data4 data5 + data3 data1 * / data1 +(" + (expression2) + ")= Invalid Expression");
}
public static int postfix(String postfix) {
try {
Stack<Integer> valuestack = new Stack<Integer>();
int characterCount = postfix.length();
int index = 0;
char nextChar = ' ';
Integer operandTwo, operandOne;
Integer result;
for (; index < characterCount; index++)
{
nextChar = postfix.charAt(index);
switch (nextChar)
{
case '+':
operandTwo = valuestack.pop();
operandOne = valuestack.pop();
result = operandOne + operandTwo;
valuestack.push((Integer) result);
break;
case '-':
operandTwo = valuestack.pop();
operandOne = valuestack.pop();
result = operandOne - operandTwo;
valuestack.push((Integer) result);
break;
case '*':
operandTwo = valuestack.pop();
operandOne = valuestack.pop();
result = operandOne * operandTwo;
valuestack.push((Integer) result);
break;
case '/':
operandTwo = valuestack.pop();
operandOne = valuestack.pop();
result = operandOne / operandTwo;
valuestack.push((Integer) result);
break;
default:
String nextch = postfix.substring(index, index + 1);
valuestack.push(Integer.parseInt(nextch));
break;
}
}
return valuestack.peek();
} catch (EmptyStackException e) {
return -1;
}
}
}
Output
data1 data2 + data3 * data4 -(12+4*5-) = 7
data1 data2 * data3 * data1-/ data4 data5*+(12*4*1-/53*+)= Invalid Expression
data4 data5 + data3 data1 * / data1 +(53+41*/1+)= 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.