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
Download files: MiniSum.java
Areas: main
|
|
MiniSum.java |
|
View full source |
| Question 1 |
| Is it necessary for the program to import java.lang.System.*? Can we delete the import statement? |
| Question 2 |
| Can other classes have access to sum which is defined in private static int sum = 0; ? |
| Question 3 |
| What is the difference between n++ and ++n? |
| Question 4 |
| Describe what sum += n++; does? How does it change the values of sum and n? |
| Question 5 |
| In the end of the method findMinimum, why return n-1 rather than n? |
| Question 6 |
| What is the difference between the print and println method? |
| Question 7 |
| Which is the entry point for running an application, findMinimum or main method? |
| 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? |
| Question 9 |
| With reference to the previous question, how should we change the program to satisfy the modified requirement? |
| 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? |
