CSCI162 - Lab 10 1 Lab 10: Prefix Evaluator 1 Objective In this assignment you will read and figure out how to use several classes that I provide, write your own class from scratch, and implement a...

1 answer below »

View more »
Answered Same DayNov 19, 2021

Answer To: CSCI162 - Lab 10 1 Lab 10: Prefix Evaluator 1 Objective In this assignment you will read and figure...

Ayush answered on Nov 22 2021
153 Votes
72154/72154/desktop.ini
[ViewState]
Mode=
Vid=
FolderType=Generic
72154/72154/P02TestFunctionality.java
72154/72154/P02TestFunctionality.java
package com.company.STACK.INTERN;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
/*
 * You must include the Junit4 library to use this test unit.
 *
 * To add it, select the project and then choose Properties... from the File menu.
 * Click on "Java Build Path" in the panel at the left.
 * Select Libraries fr
om the list at the top.
 * Select Add Library... from the right side of the panel.
 * Select JUnit and click on Next.
 * Change the version to JUnit 4 and click on Finish.
 * Click on OK in the properties panel.
 * 
 * You can then run these tests using "Run As JUnit Test" instead of "Runs As Java Application".
 */
/**
 * JUnit tests for PrefixEvaluator.
 *
 * @author Chad Hogg
 */
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class P02TestFunctionality {

    /** The maximum allowable difference when comparing doubles. */
    private static final double EPSILON = 0.00001;
    /** The return value when the expression was empty. */
    private static final String NO_INPUT = "No input.";
    /** The return value when there were not enough operands on the stack. */
    private static final String INSUFFICIENT_OPERANDS = "Not enough operands.";
    /** The return value when the answer is infinity. */
    private static final String INFINITY = "Infinity";
    /** The return value when values are left on the stack. */
    private static final String OVERFULL_STACK = "Computed answer, but not all input used.";
    /** The return value when not all input is used. */
    private static final String MORE_INPUT = "Computed answer, but not all input used.";
    /** The return value when a left parenthesis is found. */
    private static final String LEFT_PAREN = "( has no meaning here.";
    /** The return value when a right parenthesis is found. */
    private static final String RIGHT_PAREN = ") has no meaning here.";
    /**
     * Attempts to evaluate a well-formed expression.
     * 
     * @param input The postfix expression to evaluate.
     * @param expected The correct answer.
     */
    private void runTest(String input, Double expected) {
        try {
            String result = PrefixEvaluator.evaluate(input);
            try {
                Double dResult = Double.parseDouble(String.valueOf(result));
                assertEquals("\"" + input + "\" produced wrong answer.", expected, dResult, EPSILON);
            }
            catch(NumberFormatException exception) {
                fail("\"" + input + "\" should have produced a number, not \"" + result + "\"");
            }
        }
        catch(Exception exception) {
            fail("\"" + input + "\" should not have caused a " + exception.getClass() + " to be thrown.");
        }
    }
    /**
     * Attempts to evaluate a mal-formed expression.
     * 
     * @param input The bad postfix expression to evaluate.
     * @param expected The expected error message.
     */
    private void runBadTest(String input, String expected) {
        try {
            String result = PrefixEvaluator.evaluate(input);
            assertEquals("\"" + input + "\" should have produced \"" + expected + "\", not \"" + result + "\"", expected, result);
        }
        catch(Exception exception) {
            fail("\"" + input + "\" should not have caused a " + exception.getClass() + " to be thrown.");
        }
    }

    /**
     * Tests that an addition of two doubles works.
     */
    @Test
    public void test01SimpleAddition() {
        runTest("+ 5.0 7.0", 12.0);
    }
    /**
     * Tests that a subtraction of two doubles works.
     */
    @Test
    public void test02SimpleSubtraction() {
        runTest("- 7.0 5.0", 2.0);
    }

    /**
     * Tests that a multiplication of two doubles works.
     */
    @Test
    public void test03SimpleMultiplication() {
        runTest("* 3.0 2.0", 6.0);
    }

    /**
     * Tests that a division of two doubles works.
     */
    @Test
    public void test04SimpleDivision() {
        runTest("/ 3.0 2.0", 1.5);
    }

    /**
     * Tests that combining two subexpressions works.
     */
    @Test
    public void test05CombiningSubexpressions() {
        runTest("- - 8.0 2.0 + 1.0 3.0", 2.0);
    }

    /**
     * Tests that multiple consecutive operations works.
     */
    @Test
    public void test06ConsecutiveOperations() {
        runTest("/ + - + 5.0 3.0 2.0 6.0 2.0", 6.0);
    }
    /**
     * Tests that more than two initial numbers works.
     */
    @Test
    public void test07ManyNumbersToStart() {
        runTest("+ 3 * 2 + 5 4", 21.0);
    }
    /**
     * Tests evaluating a long expression.
     */
    @Test
    public void test08LongExpression() {
        runTest("+ - + - + 5 * 7 4 * * 3 2 4 5 1 * 6 5", 43.0);
    }

    /**
     * Tests an expression whose value is negative.
     */
    @Test
    public void test09NegativeAnswer() {
        runTest("* - 5 7 3", -6.0);
    }

    /**
     * Tests an empty expression.
     */
    @Test
    public void test10EmptyInput() {
        runBadTest("", NO_INPUT);
    }

    /**
     * Tests an expression without enough numbers.
     */
    @Test
    public void test11Underflow() {
        runBadTest("- + 5 7", INSUFFICIENT_OPERANDS);
    }

    /**
     * Tests an expression involving division by 0.
     */
    @Test
    public void test12Infinity() {
        runBadTest("/ 7 0", INFINITY);
    }
    /**
     * Tests an expression with too few operands.
     */
    @Test
    public void test13NotEnoughOperands() {
        runBadTest("+ 5 3 8", ...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here
April
January
February
March
April
May
June
July
August
September
October
November
December
2025
2025
2026
2027
SunMonTueWedThuFriSat
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
00:00
00:30
01:00
01:30
02:00
02:30
03:00
03:30
04:00
04:30
05:00
05:30
06:00
06:30
07:00
07:30
08:00
08:30
09:00
09:30
10:00
10:30
11:00
11:30
12:00
12:30
13:00
13:30
14:00
14:30
15:00
15:30
16:00
16:30
17:00
17:30
18:00
18:30
19:00
19:30
20:00
20:30
21:00
21:30
22:00
22:30
23:00
23:30