Question

In: Computer Science

Write a program that will recognize and evaluate prefix expressions. First design and implement a class...

Write a program that will recognize and evaluate prefix expressions. First design and implement a class of prefix expressions. This class should contain methods to recognize and evaluate prefix expressions. This chapter discusses the algorithms you will need to implement these methods. Walls and Mirrors 3rd edition Chapter 6 Programming Practice 8. Programming Language: Java.

Solutions

Expert Solution

//Create a class in java Name as PrefixExpressionEvaluator.java

import java.util.Stack;

public class PrefixExpressionEvaluator{

public static void main(String[] args) {
String expression = "+8*68";
//check for valid expression first and then perform
evaluatePrefixExpression(expression);

}

static Boolean isNumber(char ch) {
// check for character is operand or not
if (ch >= 48 && ch <= 57) {
return true;
} else {
return false;
}
}

public static void evaluatePrefixExpression(String expression) {

try {
//import stack from java.util
Stack<Double> stk = new Stack<Double>();
for (int j = expression.length() - 1; j >= 0; j--) {

// Push operand to Stack
if (isNumber(expression.charAt(j))) {
stk.push((double) (expression.charAt(j) - 48));
} else {

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

//perform operation
switch (expression.charAt(j)) {
case '+':
stk.push(operand1 + operand2);
break;
case '-':
stk.push(operand1 - operand2);
break;
case '*':
stk.push(operand1 * operand2);
break;
case '/':
stk.push(operand1 / operand2);
break;
}
}
}
System.out.println(expression + "= " + stk.peek());
} catch (Exception e) {
System.out.println("Not valid Prefix Expression");
}
}


}

//output

+8*68= 56.0

//invalid expression

-+8*68 is Not valid Prefix Expression


Related Solutions

Write a program that converts prefix expressions to postfix and postfix expressions to prefix. (java) The...
Write a program that converts prefix expressions to postfix and postfix expressions to prefix. (java) The program for this project should consist of three classes. -The main class should create a GUI that allows the user to input an expression in either prefix or postfix and convert it to postfix or prefix, respectively. -The other class should contain the code to perform the conversions. --->The pseudocode for prefix to postfix, which requires two stacks, is shown below: tokenize the string...
C++ PROGRAMING Implement a program to evaluate simple mathematical expressions. Assume that the original expression is...
C++ PROGRAMING Implement a program to evaluate simple mathematical expressions. Assume that the original expression is provided to the program as a text string. Allowed expression tokens: brackets “(” and “)”, integers like “4”, “15”, addition “+”, subtraction “-”, multiplication “*”, division “/”. Output can be float. Trim spaces from an expression. Brackets should contain at least one operation. Make sure that an expression is validated before it is calculated; reject the invalid expressions with appropriate message. The program must...
Design and implement the class Day that implements the day of the week in a program....
Design and implement the class Day that implements the day of the week in a program. The class Day should store the day, such as Sun for Sunday. The program should be able to perform the following operations on an object of type Day: Set the day. Print the day. Return the day. Return the next day. Return the previous day. Calculate and return the day by adding certain days to the current day. For example, if the current day...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
1) Design an implement a program that created and exception class called StringTooLongException, designed to be...
1) Design an implement a program that created and exception class called StringTooLongException, designed to be thrown when a string is discovered that has too many characters in it. Create a driver that reads in strings from the user until the user enters DONE. If a string that has more then 5 characters is entered, throw the exception. Allow the thrown exception to terminate the program 2) Create a class called TestScore that has a constructor that accepts an array...
Use C++ write a "Design and implement a class of infix calculators" ,simply write a function...
Use C++ write a "Design and implement a class of infix calculators" ,simply write a function named "evaluateInfix()" that evaluates infix expressions. It should have one string parameter and should return an int result. It should call a separate function named "infixToPostfix()" to convert the infix expression into a postfix expression, and then it should do the work of evaluating the resulting postfix expression. Then write a main() function to thoroughly test the function. Use the pseudocode algorithm that evaluates...
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project...
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project Details: Your program should use 2D arrays to implement simple matrix operations. Your program should do the following: • Read the number of rows and columns of a matrix M1 from the user. Use an input validation loop to make sure the values are greater than 0. • Read the elements of M1 in row major order • Print M1 to the console; make...
Write a C++ program (The Account Class) Design a class named Account that contains (keep the...
Write a C++ program (The Account Class) Design a class named Account that contains (keep the data fields private): a) An int data field named id for the account. b) A double data field named balance for the account. c) A double data field named annualInterestRate that stores the current interest rate. d) A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. e) The accessor and mutator functions for id, balance, and annualInterestRate....
Write in drjava Problem Design and implement these 4 files: A parent class called Plant with...
Write in drjava Problem Design and implement these 4 files: A parent class called Plant with name (eg Rose, Douglas Fir) and lifespan (could be in days, weeks, months or years) attributes Tree inherits from Plant and adds a height attribute Flower inherits from Plant and adds a color attribute A driver file to test the 3 classes above. The classes described in #1, 2 and 3 above should have the usual constructors (default and parameterized), get (accessor) and set...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT