-1

I am working on building a simple calculator in Java. Currently, I am testing the subtraction functionality using tests. It works fine for the first subtraction (5-5-5) but doesn't work with the (-2-2) due to Java stack order - how can I go about solving this problem.

Calculator class:

public class Calculator{

public double evaluate(String expression){

    if (expression.contains("+")){
        int index = expression.indexOf("+");
        String str1 = expression.substring(0, index);
        // String str2 = expression.substring(index + 1);
        return evaluate(str1) + evaluate(expression.substring(index + 1));
    }

    else if (expression.contains("-")){
        int index = expression.indexOf("-");
        String str1 = expression.substring(0, index);
        // String str2 = expression.substring(index + 1);
        return evaluate(expression.substring(index + 1)) - evaluate(str1);
    }
    return Double.parseDouble(expression);
}

Test class:

public class CalculatorTest{

Calculator cal;

@Before
public void setUp(){
    cal = new Calculator();
}

@Test
public void  testEvaluateReturnsDoubleMinusZeroWhenStringFiveMinusFiveMinusFiveIsPassedIn(){
    assertEquals(-5.0,cal.evaluate("5-5-5"),0.0);
}

@Test
public void testEvaluateReturnsDoubleMinusFourWhenStringMinusTwoMinusTwoIsPassedIn(){
    assertEquals(-4.0,cal.evaluate("-2-2"),0.0);
}
  • 1
    debug your code while testing this: `assertEquals(-2.0,cal.evaluate("-2"),0.0);` it will let you see what's wrong in your code – guido Apr 28 '15 at 12:25
  • Your problem is int index = expression.indexOf("-"); if the input is "-2-2". index is than 0 and the returning string is empty. – Olli Zi Apr 28 '15 at 12:38
  • For the record, I'm almost certain that the verb you're looking for is 'subtract', not 'minus'. It could be a regional thing, but that's all I hear in NYC. – Nic Apr 28 '15 at 12:39
  • Try to debug your tests with your eclipse. You can set a breakpoint on the first sentence of your `evaluate` method and then, step by step (`F5` in eclipse view) check the progress of values and expressions for each sentence which has been executed. – killabyte_garcia Apr 28 '15 at 12:46
  • In your parsing you are confusing the unary operator - (negative) with the binary substraction operator - either you should put in the rule "- followed by digit, without space = unary" and "- with whitespace left and whitespace right = binary -" or you should find another way to identify it. – Joeblade Apr 28 '15 at 13:06
  • possible duplicate of [What is a debugger and how can it help me diagnose problems](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Raedwald Apr 28 '15 at 13:24
  • `-2+-3` is a perfectly valid calculation. You need to make a parser that parses numbers with optional sign and obligatory operation between numbers. – Sylwester Apr 28 '15 at 15:43

2 Answers2

1

Let's start debugging. Expression: -2-2

  1. expression.contains("-"); is true.
  2. index = expression.indexOf("-"); return to you 0, but you are assuming that it must be 2(2 is index of second minus in expression).

In this state we have index = 0 and str = "".

For avoiding it, if expression starts with '-', then you can add 0 to beginning of expression.

bot_insane
  • 2,545
  • 18
  • 40
dijkstra
  • 1,068
  • 2
  • 16
  • 39
-1

Firstly, the order of operands must be inverse in the "-" block. Secondly, operators must be processed backward, that is 5-5-5 = (5-5)-5 and not 5-(5-5). For this reason, use lastIndexOf instead of indexOf. This should work:

public class Calculator {

    public double evaluate(final String expression) {

        if (expression.lastIndexOf("+") > 0) {
            final int index = expression.lastIndexOf("+");
            final String str1 = expression.substring(0, index);
            // String str2 = expression.substring(index + 1);
            return evaluate(str1) + evaluate(expression.substring(index + 1));
        }

        else if (expression.lastIndexOf("-") > 0) {
            final int index = expression.lastIndexOf("-");
            final String str1 = expression.substring(0, index);
            // String str2 = expression.substring(index + 1);
            return evaluate(str1) - evaluate(expression.substring(index + 1));
        }
        return Double.parseDouble(expression);
    }
}
fgwe84
  • 168
  • 7