import java.io.*;
/**
* This application displays the sum of the integers in a specified
* range.
*
* @author author name
* @version 1.0.0
*/
public class SumCalculator {
private static BufferedReader stdIn =
new BufferedReader(new InputStreamReader(System.in));
private static PrintWriter stdOut =
new PrintWriter(System.out, true);
private static PrintWriter stdErr =
new PrintWriter(System.err, true);
/**
* Returns the sum of the integers in the specified
* range [lower,upper].
*
* @param lower the lower limit.
* @param upper the upper limit.
* @return the sum of the integers in the specified
* range [lower,upper].
*/
public static int sumRange(int lower, int upper) {
stdErr.println(" sumRange called. lower:" + lower
+ " upper:" + upper);
int total = 0;
for (int i = lower ; i <= upper; i++) {
total += i;
stdErr.println(" for loop, i:" + i + " total:"
+ total);
}
return total;
}
/**
* Displays the sum of the integers in a specified range.
*
* @param args not used.
*/
public static void main(String[] args) {
int lower;
int upper;
try {
stdErr.print("lower limit: ");
stdErr.flush();
lower = Integer.parseInt(stdIn.readLine());
stdErr.print("upper limit: ");
stdErr.flush();
upper = Integer.parseInt(stdIn.readLine());
stdOut.println("The result is: " + sumRange(lower, upper));
} catch (NumberFormatException nfe) {
stdErr.println(nfe);
} catch (IOException ioe) {
stdErr.println(ioe);
}
}
}
Previou page | Next page |