Question: The Commands class includes a main method that will run the calculator as a command-line application if you wish to test specific inputs different from

The Commands class includes a main method that will run the calculator as a command-line application if you wish to test specific inputs different from the test case.Your task in completing the calculator only requires completing a single method (you can create more as helpers to your method!). The calculator user interface framework already exists, but you must complete the processCommand method within the Calculator class. The only other code in the Calculator class is a helper method to determine if a string input is a Double or not. The processCommand method takes a list of individual strings guaranteed to be only numbers and valid operators. For example, the list might contain the strings 2, 2, and +. Your could should process the strings and return the answer as a string.

package calculator;

import java.util.List; import java.util.Stack;

public class Calculator { public static final String INVALID_MESSAGE = "Invalid Command"; /** * Process the calculator command assuming post-fix notation * commands - a vector that is assured to be numbers (doubles) or valid operators * returns a String that is either the computed number or the INVALID_MESSAGE constant */ public String processCommand(List commands) { // Use a stack to process the incoming user command // The user entry has been broken into a vector of Strings. Each String is assured to be // 1.) a number // 2.) a valid operator from the operators above // The inputs will NOT BE invalid themselves, but they may not amount to a valid command. // For instance, 2+2 would be blocked, but 2 + 2 could come through and would be invalid // since it is not in post-fix notation. // // Your task is to process the command into a single numerical response and return that response // as a String (no formatting required). If the command is invalid, return the constant INVALID_MESSAGE return INVALID_MESSAGE; } /** * Check if a String value is indeed a double * value - the value to check * returns true if the number is a double, false otherwise */ public boolean isDouble(String value) { try { Double.parseDouble(value); return true; } catch (Exception e) {} return false; } }

// Junit test

package calculator.test;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import calculator.Calculator; import calculator.Commands;

public class CalculatorTest { Commands commands = new Commands(); private void runTests(Calculator uut, String[][] testCases) { for (String[] testCase : testCases) { String input = testCase[0]; String expected = testCase[1]; String actual = uut.processCommand(commands.parseInput(input)); assertEquals(expected, actual); } } @Test @GradedTest(name="Test simple statements", max_score=5) public void testSimple() { Calculator uut = new Calculator(); String[][] testCases = { {"2 2 +","4.0"}, {"2 2 -","0.0"}, {"2 2 *","4.0"}, {"2 2 /","1.0"} }; runTests(uut, testCases); }

@Test @GradedTest(name="Test compund statements", max_score=10) public void testCompound() { Calculator uut = new Calculator(); String[][] testCases = { {"4 8 3 * +","28.0"}, {"4 8 + 3 *","36.0"}, {"78 30 0.5 28 8 + * - 6 / +","80.0"}, {"2.1 2 ^ 5.2 + 7.2 - 7.1 *","17.110999999999994"}, {"2 20 * 2 / 3 4 + 3 2 ^ * + 6 - 15 +","92.0"} }; runTests(uut, testCases); }

@Test @GradedTest(name="Test invalid statements", max_score=5) public void testInvalid() { Calculator uut = new Calculator(); String[][] testCases = { {"2 2 +","4.0"}, // To avoid all invalid {"2 + 2", Calculator.INVALID_MESSAGE}, {"2 2 2 +", Calculator.INVALID_MESSAGE} }; runTests(uut, testCases); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!