Question

In: Computer Science

PostFixEvaluator.java import java.util.Stack; import java.util.Scanner; /** * Represents an integer evaluator of postfix expressions. Assumes *...

PostFixEvaluator.java

import java.util.Stack;
import java.util.Scanner;

/**
* Represents an integer evaluator of postfix expressions. Assumes
* the operands are constants.
*
* @author Java Foundations
* @version 4.0
*/
public class PostfixEvaluator
{
   private final static char ADD = '+';
   private final static char SUBTRACT = '-';
   private final static char MULTIPLY = '*';
   private final static char DIVIDE = '/';

   private Stack stack;

   /**
   * Sets up this evaluator by creating a new stack.
   */
   public PostfixEvaluator()
   {
       // complete this method
   }

   /**
   * Evaluates the specified postfix expression. If an operand is
   * encountered, it is pushed onto the stack. If an operator is
   * encountered, two operands are popped, the operation is
   * evaluated, and the result is pushed onto the stack.
   * @param expr string representation of a postfix expression
   * @return value of the given expression
   */
   public int evaluate(String expr)
   {
// Step 3- complete this method
   }

   /**
   * Determines if the specified token is an operator.
   * @param token the token to be evaluated
   * @return true if token is operator
   */
   private boolean isOperator(String token)
   {
       // Step 1 - complete this method
   }

   /**
   * Performs integer evaluation on a single expression consisting of
   * the specified operator and operands.
   * @param operation operation to be performed
   * @param op1 the first operand
   * @param op2 the second operand
   * @return value of the expression
   */
   private int evaluateSingleOperator(char operation, int op1, int op2)
   {
// Step 2 - complete this method
   }
}

PostFixTester.java

import java.util.Scanner;

/**
* Demonstrates the use of a stack to evaluate postfix expressions.
*
* @author Java Foundations
* @version 4.0
*/
public class PostfixTester
{
   /**
   * Reads and evaluates multiple postfix expressions.
   */
   public static void main(String[] args)
   {
       String expression, again;
       int result;

       Scanner in = new Scanner(System.in);

       do
       {
           PostfixEvaluator evaluator = new PostfixEvaluator();
           System.out.println("Enter a valid post-fix expression one token " +
                   "at a time with a space between each token (e.g. 5 4 + 3 2 1 - + *)");
           System.out.println("Each token must be an integer or an operator (+,-,*,/)");
           expression = in.nextLine();

           result = evaluator.evaluate(expression);
           System.out.println();
           System.out.println("That expression equals " + result);

           System.out.print("Evaluate another expression [Y/N]? ");
           again = in.nextLine();
           System.out.println();
       }
       while (again.equalsIgnoreCase("y"));
   }
}

Could you help, please?

Solutions

Expert Solution

/**
* Represents an integer evaluator of postfix expressions. Assumes the operands
* are constants.
*
* @author Java Foundations
* @version 4.0
*/
class PostfixEvaluator {
   private final static char ADD = '+';
   private final static char SUBTRACT = '-';
   private final static char MULTIPLY = '*';
   private final static char DIVIDE = '/';

   private Stack stack;

   /**
   * Sets up this evaluator by creating a new stack.
   */
   public PostfixEvaluator() {
       // complete this method
   }

   /**
   * Evaluates the specified postfix expression. If an operand is encountered,
   * it is pushed onto the stack. If an operator is encountered, two operands
   * are popped, the operation is evaluated, and the result is pushed onto the
   * stack.
   *
   * @param expr
   * string representation of a postfix expression
   * @return value of the given expression
   */
   public int evaluate(String expr) {
       // create a stack
       Stack<Integer> stack = new Stack<>();

       // iterate all characters one by one
       for (int i = 0; i < expr.length(); i++) {
           char c = expr.charAt(i);
           if(c==' ')
               continue;
           // If the scanned character is an operand (number here),
           // push it to the stack.
           if (isOperator(c + "")) {
               int val1 = stack.pop();
               int val2 = stack.pop();
               stack.push(evaluateSingleOperator(c, val1, val2));
           } else {
               stack.push(c - '0');
           }

       }
       return stack.pop();
       // Step 3- complete this method
   }

   /**
   * Determines if the specified token is an operator.
   *
   * @param token
   * the token to be evaluated
   * @return true if token is operator
   */
   private boolean isOperator(String token) {

       boolean res = false;
       switch (token.charAt(0)) {
       case ADD:
       case SUBTRACT:
       case DIVIDE:
       case MULTIPLY:
           res = true;
           break;
       default:
           res = false;
       }
       return res;
       // Step 1 - complete this method
   }

   /**
   * Performs integer evaluation on a single expression consisting of the
   * specified operator and operands.
   *
   * @param operation
   * operation to be performed
   * @param op1
   * the first operand
   * @param op2
   * the second operand
   * @return value of the expression
   */
   private int evaluateSingleOperator(char operation, int val1, int val2) {
       int res = 0;
       switch (operation) {
       case ADD:
           res = (val1 + val2);
           break;

       case SUBTRACT:
           res = (val2 - val1);
           break;

       case DIVIDE:
           res = (val2 / val1);
           break;

       case MULTIPLY:
           res = (val2 * val1);
           break;
       }
       // Step 2 - complete this method
       return res;
   }
}

/**
* Demonstrates the use of a stack to evaluate postfix expressions.
*
* @author Java Foundations
* @version 4.0
*/
public class PostfixTester {
   /**
   * Reads and evaluates multiple postfix expressions.
   */
   public static void main(String[] args) {
       String expression, again;
       int result;

       Scanner in = new Scanner(System.in);

       do {
           PostfixEvaluator evaluator = new PostfixEvaluator();
           System.out.println("Enter a valid post-fix expression one token "
                   + "at a time with a space between each token (e.g. 5 4 + 3 2 1 - + *)");
           System.out.println("Each token must be an integer or an operator (+,-,*,/)");
           expression = in.nextLine();

           result = evaluator.evaluate(expression);
           System.out.println();
           System.out.println("That expression equals " + result);

           System.out.print("Evaluate another expression [Y/N]? ");
           again = in.nextLine();
           System.out.println();
       } while (again.equalsIgnoreCase("y"));
   }
}


Related Solutions

import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        } System.out.println(""+getAvg(new_stack));    }     public static int getAvg(Stack s) {        //TODO: Find the average of the elements in the...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
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...
2.) Postfix to infix translator (NOTE: This is NOT the evaluator you have already coded!) •...
2.) Postfix to infix translator (NOTE: This is NOT the evaluator you have already coded!) • Create a java class called PostfixToInfixTranslator that includes a main method. • The code you write should prompt the user for an expression in postfix notation and use ArrayStack to output the equivalent expression in infix. • See the following session: Enter a postfix expression: 3 4 + 2 * In infix notation that is: ((3+4)*2) Translate another expression [y/n]? y Enter a postfix...
In Java please Cipher.java: /* * Fix me */ import java.util.Scanner; import java.io.PrintWriter; import java.io.File; import...
In Java please Cipher.java: /* * Fix me */ import java.util.Scanner; import java.io.PrintWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; public class Cipher { public static final int NUM_LETTERS = 26; public static final int ENCODE = 1; public static final int DECODE = 2; public static void main(String[] args) /* FIX ME */ throws Exception { // letters String alphabet = "abcdefghijklmnopqrstuvwxyz"; // Check args length, if error, print usage message and exit if (args.length != 3) { System.out.println("Usage:\n"); System.out.println("java...
C++ Data Structure Write a program to change following infix expressions to postfix expressions using a...
C++ Data Structure Write a program to change following infix expressions to postfix expressions using a stack a) D-B+C b) C*D+A*B c) (A*B)*C+D*F-C d) (A-4*(B-C)-D/E)*F
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT