Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Catch and handle the divide-by-zero situation that was assumed not to happen. Fo

ID: 3750773 • Letter: C

Question

Catch and handle the divide-by-zero situation that was assumed not to happen. For example, if the input expression is 9 7 7 - /, the result would be the exception message "illegal divide by zero".

Support a new operation indicated by "^" that returns the larger of its operands. for example, 3 9 ^ = 9


/**
* Represents an integer evaluator of postfix expressions. Assumes
* the operands are constants.
*
*
* Revise and test the postfix expression evaluator program
* 1. Catch and handle the divide-by-zero situation that was assumed not to happen. For example, if the input expression is 9 7 7 - /,
*    the result would be the exception message "illegal divide by zero".
* 2. Support a new operation indicated by "^" that returns the larger of its operands. for example, 3 9 ^ = 9
**/

public class PostfixEvaluator
{
    private final static char ADD = '+';
    private final static char SUBTRACT = '-';
    private final static char MULTIPLY = '*';
    private final static char DIVIDE = '/';
    private final static char LARGER = '^';

    private Stack<Integer> stack;

    /**
     * Sets up this evalutor by creating a new stack.
     */
    public PostfixEvaluator()
    {
        stack = new Stack<Integer>();
    }

    /**
     * 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)
    {
        int op1, op2, result = 0;
        String token;
        Scanner parser = new Scanner(expr);

        while (parser.hasNext())
        {
            token = parser.next();

            if (isOperator(token))
            {
                op2 = (stack.pop()).intValue();
                op1 = (stack.pop()).intValue();
                result = evaluateSingleOperator(token.charAt(0), op1, op2);
                stack.push(new Integer(result));
            }
            else
                stack.push(new Integer(Integer.parseInt(token)));
        }

        return result;
    }

    /**
     * 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 isOperator = token.equals("+") || token.equals("-") ||
                 token.equals("*") || token.equals("/");

        // TODO Support a new operation indicated by "^" that returns the larger of its operands. for example, 3 9 ^ = 9 , insert a statement that resign the isOperator variable by considering the "^"

        return isOperator;
    }

    /**
     * Peforms 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) throws RuntimeException
    {
        int result = 0;

        switch (operation)
        {
            case ADD:
                result = op1 + op2;
                break;
            case SUBTRACT:
                result = op1 - op2;
                break;
            case MULTIPLY:
                result = op1 * op2;
                break;
            case DIVIDE:
                // TODO Catch and handle the divide-by-zero situation once it happens. Insert a statement, where an instance of the IllegalDivisionException class will be thrown if a number is divided by zero
             

                result = op1 / op2;
                break;
            case LARGER:
                // TODO Support a new operation indicated by "^" that returns the larger of its operands. for example, 3 9 ^ = 9. Insert a statement where the larger one of two number will be calculated
                
        }

        return result;
    }
}

public class IllegalDivistionException extends RuntimeException
{
    /**
     * Sets up this exception with an appropriate message.
     * @param collection the name of the collection
     */
    public IllegalDivistionException()
    {
        // TODO Catch and handle the divide-by-zero situation. For example, if the input expression is 9 7 7 - /,
        //       the result would be the message "illegal divide by zero".
        //       Insert a statement invoking the constructor of the super class and prompting the error message "illegal divide by zero"

    }
}

Explanation / Answer

Screenshot

--------------------------------------------------------------------------------

Program

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

class PostfixEvaluator
{
    private final static char ADD = '+';
    private final static char SUBTRACT = '-';
    private final static char MULTIPLY = '*';
    private final static char DIVIDE = '/';
    private final static char LARGER = '^';

    private Stack<Integer> stack;

    /**
     * Sets up this evalutor by creating a new stack.
     */
    public PostfixEvaluator()
    {
        stack = new Stack<Integer>();
    }

    /**
     * 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)
    {
        int op1, op2, result = 0;
        String token;
        Scanner parser = new Scanner(expr);

        while (parser.hasNext())
        {
            token = parser.next();

            if (isOperator(token))
            {
                op2 = (stack.pop()).intValue();
                op1 = (stack.pop()).intValue();
                result = evaluateSingleOperator(token.charAt(0), op1, op2);
                stack.push(new Integer(result));
            }
            else
                stack.push(new Integer(Integer.parseInt(token)));
        }

        return result;
    }

    /**
     * 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 isOperator = token.equals("+") || token.equals("-") ||
                 token.equals("*") || token.equals("/")||token.equals("^");

        // TODO Support a new operation indicated by "^" that returns the larger of its operands. for example, 3 9 ^ = 9 , insert a statement that resign the isOperator variable by considering the "^"

        return isOperator;
    }

    /**
     * Peforms 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) throws RuntimeException
    {
        int result = 0;

        switch (operation)
        {
            case ADD:
                result = op1 + op2;
                break;
            case SUBTRACT:
                result = op1 - op2;
                break;
            case MULTIPLY:
                result = op1 * op2;
                break;
            case DIVIDE:
                // TODO Catch and handle the divide-by-zero situation once it happens. Insert a statement, where an instance of the IllegalDivisionException class will be thrown if a number is divided by zero
             if(op2<1) {
               IllegalDivistionException ill=new IllegalDivistionException();
               System.exit(0);
              
             }
             else {
               result = op1 / op2;
             }
              
                break;
            case LARGER:
                // TODO Support a new operation indicated by "^" that returns the larger of its operands. for example, 3 9 ^ = 9. Insert a statement where the larger one of two number will be calculated
               result=op1>op2?op1:op2;
        }

        return result;
    }
}

class IllegalDivistionException extends RuntimeException
{
    /**
     * Sets up this exception with an appropriate message.
     * @param collection the name of the collection
     */
    public IllegalDivistionException()
    {
        // TODO Catch and handle the divide-by-zero situation. For example, if the input expression is 9 7 7 - /,
        //       the result would be the message "illegal divide by zero".
        // Insert a statement invoking the constructor of the super class and prompting the error message "illegal divide by zero"
         System.out.println("illegal divide by zero");
    }
}
public class PostFixMain {

   public static void main(String[] args) {
       //First expression
       PostfixEvaluator pe=new PostfixEvaluator();
       System.out.println(pe.evaluate("3 9 ^"));
       //Second expression
       PostfixEvaluator pe1=new PostfixEvaluator();
       System.out.println(pe1.evaluate("9 7 7 - /"));
   }

}

-------------------------------------------------------------

output

9
illegal divide by zero

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote