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
// 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
Get step-by-step solutions from verified subject matter experts
