|
|
Laboratory 6
Whiling away the lab
Week of 14 March, 2005
|
Objective
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.
Key Concepts
- while statement
- Common looping problems
Getting Started
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, Upper.java,
CharacterCounting.java, One.txt
and Two.txt for your manipulation. All four of the
Java files need to be submitted 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.
- Consider the following while loop-based program that displays the sum of the
integers in the range 1 … 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.
- Open the file SeriesSum.java.
- 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).
- The mistake takes place in updating sum. Variable sum is not being increased
by the right amount. Modify the code to increment sum appropriately.
- Run the program using the values 1, 3, 4, -5, and 0. Does the program work
correctly now? Once it is correct,
submit it.
- 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 …
maxNumber. Thus, IntervalSum.java is a generalization of SeriesSum.java, whose
intervals of numbers always started with 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
user-specified interval.
- Complete the program and compile it. Test the program to make sure it is
working correctly.
- 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
-
Submit program IntervalSum.java.
- Close program IntervalSum.java.
Text conversion
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.
- Open the program Upper.java.
- Examine the program to get a sense of how it accomplishes its task.
- Although the method stdin.read() reads a single character, its returns value
is int. If the stream has un-extracted characters, the next available character
is returned. However, if end of the stream has occurred, stdin.read() returns
-1.
For this reason, variable input is made an int.
- The loop text expression compares the value of input with -1. If they do not
match, then end of stream has not yet occurred and there is a character to
process.
- The body of the loop processes the character. The process begins with a
conversion that casts the int representation of the input to a char
representation. A char representation can be tested for uppercase-ness using
Character class method isUpperCase(). Method isUpperCase() returns a boolean
value that indicates whether its actual parameter is an uppercase letter.
- 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.
- 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 ctrl-z followed by enter produces the signal; most other
systems often use ctrl-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.
- Test your program. Once it is completed correctly,
submit it.
- Close program Upper.java.
Using Input File Streams
Often programs examine data files. In this exercise, you will learn to use input
file streams to extract data from a file. Other types of file processing will be
considered later.
- Open the program CharacterCounting.java. The intended purpose of this program
is to count the number of characters in a user-specified file.
- CharacterCounting.java first prompts its user for the name of the file and
creates a Scanner file stream representation filein from the filename. The
representation is a two-step process. First, a File representation is built
using the name of the file of interest as its String parameter. Class File is
the basic file naming library in Java and is part of java.io.. A Scanner
representation filein then is created out of the File representation. With filein, a programmer has access to the accustomed input extraction methods
(e.g., nextLine() and hasNext()).
- CharacterCounting.java also defines a variable numberCharacters whose value
represents the number of characters extracted so far. Because when it is defined
no characters have been read from the file, it is initialized to 0.
- Using the code from Upper.java as a model, add code to CharacterCounting.java
that processes the text from the file input stream.
- Compile and run the program trying data sets One.txt and
Two.txt. Make sure
you understand why the program produced its output.
- Once CharacterCounting.java is completed correctly,
submit it.
- Close program CharacterCounting.java.
Finishing up
- Copy any files you wish to keep to your own drive.
- Delete all of the files you copied or created on the laboratory machine.