0

I have the following piece of code that I've been looking at for a while now:

import java.util.Scanner;

public class SumExercise {  
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Input start value");
        int start = scanner.nextInt();
        System.out.println("Input end value");
        int end = scanner.nextInt();
        int sum = computeSum(start,end);
        System.out.println("Sum is of integers from "+start+" to "+
        end+" is "+sum);
        scanner.close();
    }
    public static void computeSum() {
        for(int i = start; i <= end; i++) {
            sum += i;
        }
    }
}

The way the program is supposed to work is that a user inputs a start value (int start = scanner.nextInt();) and then afterwards an end value (int end = scanner.nextInt();) and then the program will calculate the total sum (int sum = computeSum(start,end);).

So if the user inputs 4 as the start value and 8 as the end value, the program will calculate "4+5+6+7+8" and then assign the value "30" to the variable sum.

My exercise is to try and write the method computeSum.

My attempt at solving this has been to try and write a loop that increases with 1 integer where it starts with the variable start and ends with the variable end:

public static void computeSum() {
        for(int i = start; i <= end; i++) {
            sum += i;
        }
}

However it doesn't seem like I can get it to "link" with the computeSum(start,end). Is there something that I'm missing? My program is saying that the arguments are not applicable but I'm not sure what it means by that. I'm sorry, I'm quite new to programming.

WoeIs
  • 1,083
  • 1
  • 15
  • 25
  • Well, yeah. You trying to pass parameters to your method, but your method doesn't take any parameters `public static void computeSum(//paramsShouldGoHere) {`. You are also expecting the computeSum() to return something, which it doesn't either because of `void` – austin wernli Sep 24 '18 at 20:45

2 Answers2

2

You need to change your computeSum to accept start and end, and since you assign the result of computing the sum it should return an int (sum). Like,

public static int computeSum(int start, int end) {
    int sum = 0;
    for(int i = start; i <= end; i++) {
        sum += i;
    }
    return sum;
}

or, in Java 8+, use a lambda. Like,

public static int computeSum(int start, int end) {
    return IntStream.rangeClosed(start, end).sum();
}

or, apply Gauss's formula for summing natural numbers like

public static int computeSum(int start, int end) {
    return sumN(end) - sumN(start);
}

private static int sumN(int n) {
    return ((n + 2) * (n + 1)) / 2;
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • I actually did try to write 'computeSum(int, int)' before but it still generated an error and I thought that was just completely wrong! Thank you for your help. Can you explain to me why the "return sum;" is needed? – WoeIs Sep 24 '18 at 20:51
  • 1
    Because the method is set to return the sum, since it has `int` before the method name, and not `void` (second part of the first sentence of the answer). Hence, `return sum;`. – AntonH Sep 24 '18 at 20:54
  • Because `int sum = computeSum(start,end);` implies you expect the method to *`return`* an `int`. And before you ask, read [this](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value). – Elliott Frisch Sep 24 '18 at 20:55
  • Thank you both for the answer! – WoeIs Sep 24 '18 at 21:06
0

I think that the problem is with start, you cant assign it to i and same goes for sum and end. You need to make them global variables so both of the functions will recognize them

Green
  • 2,405
  • 3
  • 22
  • 46