// CSE 142, Summer 2008 (Marty Stepp) // This program computes the sum between two ranges of integers // and the difference between them. // // It demonstrates writing methods that return values // along with computing a cumulative sum. // public class SumRanges { public static void main(String[] args) { int sum1 = sum(1, 7); System.out.println("The sum from 1 to 7 is " + sum1); int sum2 = sum(-3, 8); System.out.println("The sum from -3 to 8 is " + sum2); System.out.println("Difference is " + Math.abs(sum1 - sum2)); } // Computes and returns the sum of the integers between the given // minimum and maximum inclusive. public static int sum(int min, int max) { int total = 0; for (int i = min; i <= max; i++) { total = total + i; // total += i; } return total; } }