Being able to repeatedly execute statements of interest is a powerful
programming mechanism. In this laboratory you will gain practice with the Java
while looping statement.
- while statement
- Common looping problems
Using the procedures in the previous laboratories (i.e. right-click on the link
and select "save as"), copy the files SeriesSum.java,
IntervalSum.java, and Upper.java for your manipulation. All
three of the
Java files need to be completed for this lab.
Determining sums of numbers
The first two while examples are concerned with computing sums of consecutive
numbers. Although simple in nature, they illustrate nicely while loop behavior.
- Open the file SeriesSum.java.
- Consider the following while loop-based program that displays the sum of the
integers in the range 1 to maxNumber, where maxNumber
is a user-supplied positive
value. If maxNumber is 4, then the program displays the sum of 1
+ 2 + 3 + 4,,
which is 10. The program contains one logic error (bug). Attempt to find it. A
common debugging technique is to step through code manually (i.e., pretend you
are the computer). Try three stepping through times – using in turn the values
1, 3, and 4.
- If you cannot find the error in the program, you should run it using different
input values to see what is actually happening (e.g., 1, 3, and 4). If
that does not help, as a TA for assistance.
- Once the bug is found, fix it.
- Run the program using the values 1, 3, 4, -5, and 0. Does the program work
correctly now? Once it is correct, close program SeriesSum.java.
- Open the file IntervalSum.java. The intended purpose of IntervalSum.java is to
calculate the sum of integers from a user-specified interval minNumber
to maxNumber. Thus, IntervalSum.java is a generalization of SeriesSum.java,
whose minNumber is always 1.
- As written, IntervalSum.java just prompts its users for the two values and
checks that the extracted inputs are sensible. The program does not define the
index variable whose value repeatedly takes on the numbers in the interval nor
does it contain the loop that sums those numbers. Using the code from
SeriesSum.java as a model, add a while loop that sums the numbers from the
- Complete the program and compile it. Test the program to make sure it is
- To test your program some more – complete the comments at the end of the
program to show the result of runs using the following inputs. In your comments
report both the expected and actual results
- 1 and 4
- -4 and -1
- -3 and 3
- 5 and 5
- 8 and 6
- Close program IntervalSum.java.
Often programs analyze text to see if it has some particular property. The next
example program counts the number of uppercase letters in the text that it
extracts. The program extracts its values from the standard input, which by
default comes from the keyboard. You will need to add the code so that the
program also counts the total number of characters in the input.
- Open the program Upper.java.
- Examine the program to get a sense of how it accomplishes its task.
- The outer while loop condition is stdin.hasNext().
This means that while there is still input to be read in from the
keyboard, this method will return true. When there is no
more input to be read in (i.e. when the user enters Ctrl-Z), it will
- The first line in the outer while loop creates an input
object which holds the current line read in from the keyboard.
- An inner while loop iterates through this input String so
that it can analyze each individual character.
- For each character, if it is upper case, then the numberUpperCase
variable is incremented. This is done via a call to the
- Either way, i is incremented, as it is the counter variable for the
inner while loop.
- Compile and run the program. It is important to observe that the program does
not issue a prompt to the user to start supplying text. The program just waits
silently for its input. This is why we always put a legend in our programs
(note that you don't have to for this lab).
- Provide the following text as input. When finished, you need to signal the
program that you are done supplying standard input. On Windows-based PCs, a line
consisting solely of a Control-Z followed by enter produces the signal; most other
systems often use Control-D followed by enter.
Java is great!
This course is FANTASTIC.
This Line Has a Lot of Uppercase Letters
- Modify the program to count also the total number of characters (including spaces and punctuation). The
modification requires an additional counter variable. When defining the
variable, provide a comment that justifies its initial value. This new variable
should be incremented once per non-end of stream value.
- Have your program print out the total number of characters
- Test your program. Once it is completed correctly, close program Upper.java.
- Copy any files you wish to keep to your home directory.
- Delete all of the files you copied or created on the laboratory machine.
- Have a great spring break!