Home > Blockchain >  Use recursion to collapse the overridden add methods in the code to the left such that the user can
Use recursion to collapse the overridden add methods in the code to the left such that the user can

Time:03-21

Can someone help me with this question. I made a couple of changes to my code but I do not get the desired output. My output says add method is overloaded, and add method is not recursive. Use recursion to collapse the overridden add methods in the code to the left such that the user can input any number of terms.

Output 3 4 7.0 and 3 4 5 12.0

import java.util.*;

public class Recursion {

    public static void main(String args[]) {

        Scanner input = new Scanner(System.in);
        String exp = input.nextLine();
        System.out.println(solver(exp.split(" ")));

    }
    public static double solver(String[] expression) {

        double result = 0;

        if (expression.length == 3) {
            result = add(Double.parseDouble(expression[0]), Double.parseDouble(expression[2]));
        }
        else if (expression.length == 5) {
            result = add(Double.parseDouble(expression[0]), Double.parseDouble(expression[2]),
                    Double.parseDouble(expression[4]));
        }
        else if (expression.length == 7) {
            result = add(Double.parseDouble(expression[0]), Double.parseDouble(expression[2]),
                    Double.parseDouble(expression[4]), Double.parseDouble(expression[6]));
        }
        else if (expression.length == 9) {
            result = add(Double.parseDouble(expression[0]), Double.parseDouble(expression[2]),
                    Double.parseDouble(expression[4]), Double.parseDouble(expression[6]),
                    Double.parseDouble(expression[8]));
        }
        else if (expression.length == 11) {
            result = add(Double.parseDouble(expression[0]), Double.parseDouble(expression[2]),
                    Double.parseDouble(expression[4]), Double.parseDouble(expression[6]),
                    Double.parseDouble(expression[8]), Double.parseDouble(expression[10]));
        }

        return result;

    }

    public static double add(double a, double b) {return a   b;}
    public static double add(double a, double b, double c) {return a   b   c;}
    public static double add(double a, double b, double c, double d) {return a   b   c   d;}
    public static double add(double a, double b, double c, double d, double e) {return a   b   c   d   e;}
    public static double add(double a, double b, double c, double d, double e, double f) {return a   b   c   d   e   f;}
}

CodePudding user response:

You are not passing the correct indexes to the various add methods. For example, if you want to add three numbers, you should do the following:

result = add(Double.parseDouble(expression[0]), Double.parseDouble(expression[1]), Double.parseDouble(expression[2]));

CodePudding user response:

That doable with recursion. But before diving into this, it's worth to point out on an issue with the code you've provided.

Your existing solution is brittle since it depends on the consistency of the user input, and it will fail because of the single additional white space or if a white space will be missing.

And since your code is intended to perform the arithmetical addition, I think it'll be better to split the input on the plus symbol and give a user a bit of freedom with white spaces.

For that, we need to pass the following regular expression into the split() method:

"\\s*\\ \\s*"
  • \s* - implies 0 or more white spaces;
  • \ - plus symbol has a special meaning in regular expressions and needs to be escaped with a slash.

Now, a quick recap on recursion.

Every recursive method consists of two parts:

  • Base case - that represents a simple edge-case (condition when recursion terminates) for which the outcome is known in advance.
  • Recursive case - a part of a solution where recursive calls are made and where the main logic resides.

To process the given array recursively, we can track the position in the array by passing it with each method call.

The base case will represent a situation when there's no more elements left in the array.

In recursive case we need to parse the number under the current position and to the result of the recursive call with position incremented by 1 to it. This sum will constitute the return value.

public static double addAsDouble(String[] expression, int pos) {
    if (pos == expression.length) {
        return 0;
    }

    return Double.parseDouble(expression[pos])   addAsDouble(expression, pos   1);
}

main()

public static void main(String args[]) {
    Scanner input = new Scanner(System.in);
    String exp = input.nextLine();

    System.out.println(addAsDouble(exp.split("\\s*\\ \\s*"), 0));
}

Output

3   4  5
12.0
  • Related