Computer Program Reading Comprehension Project
U of T Computer Program Reading Comprehension Project
Minimum Sum
This program finds the minimum n where the sum of 1+2+..+n > 1000. This exercise helps users understand while loop. It also illustrates the use of a main method.

Download files: MiniSum.java
Areas: main


MiniSum.java
1/**This program finds the minimum n where the sum of 1+2+..+n > 1000.
2 */
3
4import java.io.*;
5import java.lang.System.* ;
6
7public class MiniSum {
8
9  final static int UPPERLIMIT = 1000;
10  private static int sum = 0;
Displaying 10 of 27 lines
View full source


Question 1
Is it necessary for the program to import java.lang.System.*? Can we delete the import statement?
Hints Solution

Question 2
Can other classes have access to sum which is defined in private static int sum = 0; ?
Hints Solution

Question 3
What is the difference between n++ and ++n?
Hints Solution

Question 4
Describe what sum += n++; does? How does it change the values of sum and n?
Hints Solution

Question 5
In the end of the method findMinimum, why return n-1 rather than n?
Solution

Question 6
What is the difference between the print and println method?
Solution

Question 7
Which is the entry point for running an application, findMinimum or main method?
Solution

Question 8
Suppose we change the requirement of the program. We replace 1000 with 6. That is, we want to find the minimum n where 1+2+..+n > 6. If we only replace line 9 of the code:
final static int UPPERLIMIT = 1000;
with
final static int UPPERLIMIT = 6;
would the program work?
Hints Solution

Question 9
With reference to the previous question, how should we change the program to satisfy the modified requirement?
Hints Solution

Question 10
Suppose we make the change suggested in the solution of the previous question, but we do not change line 9. That is UPPERLIMIT is 1000. Would the program satisfy the original requirement?
Hints Solution