CISC 1115
Introduction to Programming Using Java
Lab #9
Loops
Lab 9.1 — Sum of Pairs Revisited — Take 1 (SumOfPairs)
The file numbers.text consists of pairs of numbers, preceded by a header value. Read in the header value and
then that number of pair, printing the sum of each pair to
System.out
. When all pairs have
been read in, print out the number of pairs processed.
Sample Test Run
For example if the file numbers.text
contains:
3
1 2
3 4
6 7
the program should produce the following output:
3
7
13
Processed 3 pairs of numbers
- This exercise is a straightforward case of the use of a header value (though the count is of pairs rather than individual numbers.
- The count at the end should be trivial (it's the header value); it's there in anticipation of printing counts in subsequent
labs using trailer values and eof
Lab 9.2 — Sum of Pairs Revisited — Take 2 (SumOfPairs)
The file numbers.text consists of pairs of numbers, with a trailer value of -1. Read in the pairs,
printing the sum of each pair to the file
results.text
. When all pairs have
been read in, print out the number of pairs processed.
Sample Test Run
For example if the file numbers.text
contains:
1 2
3 -1
6 7
-1
the program should produce the following output:
The sum of 1 and 2 is 3
The sum of 3 and -1 is 2
The sum of 6 and 7 is 13
Processed 3 pairs of numbers
- This exercise is a fairly straightforward case of the use of a trailer value
- The trailer value of -1 is specified for the first element of the pair only, this in the above example, the
-1
in the
second element of the second pair (3 -1
) is not considered to be a trailer value
- Notice that the last line of data need not contain a second value — the trailer value consists of a single negative value;
there is no need for a full pair
- Thinking about the trailer as meta-data may help: the trailer is not actual data and thus need not
conform to the rules for the data; it can have its own format — in this case a single -1.
- The count at the end is no longer trivial, since there's no header value; instead you must maintain the count within the loop —
the trailer value program in the lecture notes has that code.
Lab 9.3 — Sum of Pairs Revisited — Take 3 (SumOfPairs)
The file numbers.text consists of pairs of numbers, but this time the trailer value is a pair, both of which are -1. Read in the pairs,
printing the sum of each pair to the file
results.text
. When all pairs have
been read in, print out the number of pairs processed.
Sample Test Run
For example if the file numbers.text
contains:
-1 2
3 -1
6 7
-1 -1
the program should produce the following output:
The sum of -1 and 2 is 1
The sum of 3 and -1 is 2
The sum of 6 and 7 is 13
Processed 3 pairs of numbers
- In contrast to the previous exercise, the trailer value here consists of BOTH values of the pair.
- The
-1
's in the first and second pairs therefore are not considered trailer values, since the other element
is not -1
.
- Notice that the last line of data needs to be a pair. Again, this is because the trailer value was specified to be a pair (rather than the actual data being pairs).
- And again, it's important to distinguish between meta-data and data.
Lab 9.4 — Sum of Pairs Revisited — Take 4 (SumOfPairs)
The file numbers.text consists of pairs of numbers, but this time the trailer value is a -1 in
the second number of the pair only. Read in the pairs,
printing the sum of each pair to the file
results.text
. When all pairs have
been read in, print out the number of pairs processed.
Sample Test Run
For example if the file numbers.text
contains:
-1 2
3 1
6 7
1 -1
the program should produce the following output:
The sum of -1 and 2 is 1
The sum of 3 and 1 is 4
The sum of 6 and 7 is 13
Processed 3 pairs of numbers
- This is a fairly unrealistic sort of trailer value; while I could come up with some sort of story, it would be quite somewhat forced …
having the second element in the pair be the trailer value means the first value is 'garbage', and having it there is somewhat confusing.
- The reason I'm including this scenario (and the next exercise as well) is to emphasize the basic rule I taught about
while
loops and 'priming the pump': all variables under your control
must be initialized before the first entry into the condition of the loop, and as a result the pump is primed by hoisting such initialization code above the loop header.
Furthermore, anything else that needs to be done to achieve or aid in that initialization must also be placed above the loop header.
- In this exercise, the code to read in the first value must therefore also be placed above the loop header, since it must be done before the second value is read in.
- Notice that as in the previous exercise, the last line of data needs to be a pair, but here, it is only the second value that is meaningful, the first can be any
value since it is being ignored.
- And again, it's important to distinguish between meta-data and data.
- Don't forget that any code hoisted above the loop needs to be repeated at the bottom of the loop body.
Lab 9.5 — Sum of Pairs Revisited — Take 5 … Lab 9.4 from the Keyboard (SumOfPairs)
Redo the previous lab, but this time the data is coming from the keyboard.
Sample Test Run
Here is a sample execution of the program.
User input is in bold.
first number? -1
second number? 2
first number? -3
second number? 1
first number? 6
second number? 7
first number? 5
second number? -1
and the following output should be written to
results.text
:
The sum of -1 and 2 is 1
The sum of 3 and -1 is 2
The sum of 6 and 7 is 13
Processed 3 pairs of numbers
- While this just seems to add insult to injury — the previous exercise used an improbably trailer value, and now this one just has the data come from the keyboard —
and it is true that it is an even more unrealistic scenario, it is valuable as a continuation and follow-up to the notion of what has to be done to prime the pump.
- Since the data is coming from the keyboard, prompts need to be used, and those also have to be hoisted up above the loop
- We thus have both the first and last numbers being input above the loop, but now their prompts must accompany them as well.
- And again, any code hoisted above the loop needs to be repeated at the bottom of the loop body.
Lab 9.6 — Sum of Pairs Revisited — Take 6 (SumOfPairs)
The file numbers.text consists of pairs of numbers. Read in the pairs until eof,
printing the sum of each pair to the file
results.text
. When all pairs have
been read in, print out the number of pairs processed.
Sample Test Run
For example if the file numbers.text
contains:
-1 2
3 -1
6 7
-1 -1
the program should produce the following output:
The sum of -1 and 2 is 1
The sum of 3 and -1 is 2
The sum of 6 and 7 is 13
The sum of -1 and -1 is -2
Processed 4 pairs of numbers
- As mentioned in class, eof processing is essentially a special case of a trailer value; except the trailer value s supplied and
handled by the I/O runtime (the
hasNextxxx
methods in the case of Scanner
).
- In particular, the -1's, throughout the file are are now data rather than meta-data.
Lab 9.7 — Calculating Averages (Averages) (Approval)
The file numbers.text consists of sequences of numbers, each sequence preceded by a header value nd then followed by that many integers.
Read in the sequences and print their averages. When all sequences have been read in, print out the number of sequences processed.
Sample Test Run
For example if the file numbers.text
contains:
3 1 2 3
5 12 14 6 4 0
10 1 2 3 4 5 6 7 8 9 10
1 17
2 90 80
the program should produce the following output:
The average of the 3 integers 1 2 3 is 2.0
The average of the 5 integers 12 14 6 4 0 is 7.2
The average of the 10 integers 1 2 3 4 5 6 7 8 9 10 is 5.5
The average of the 1 integers 17 is 17.0
The average of the 2 integers 90 80 is 85.0
5 sets of numbers processed
- This data in this example is of a more complex structure than the previous examples — both eof and header value
processing is required (the former to determine when there are no more sequences, and the latter to process each sequence).
- This exercise is in anticipation of a later lecture on Data Input Patterns that covers structured input in more detail.
Lab 9.8 — A Numeric Table (NumericTable)
Print a table of the numbers from 1 to 10 dallying in a tabular fashion the number, its cube root, square root, square, and cube
(The method
Math.cbroot
returns the cube root of its argument.)
Sample Test Run
Here is the desired output:
0 0.000000 0.000000 0 0
1 1.000000 1.000000 1 1
2 1.259921 1.414214 4 8
3 1.442250 1.732051 9 27
4 1.587401 2.000000 16 64
5 1.709976 2.236068 25 125
6 1.817121 2.449490 36 216
7 1.912931 2.645751 49 343
8 2.000000 2.828427 64 512
9 2.080084 3.000000 81 729
10 2.154435 3.162278 100 1000
- Practice in formatting output using the
printf
method.
Lab 9.9 — A Numeric Table — Take 2 (NumericTable)
Same as Lab 9.6, but the table is more elaborate.
Sample Test Run
Here is the desired output:
n| cube root|square root|square|cube
--+----------+-----------+------+----
0| 0.000000| 0.000000| 0| 0
1| 1.000000| 1.000000| 1| 1
2| 1.259921| 1.414214| 4| 8
3| 1.442250| 1.732051| 9| 27
4| 1.587401| 2.000000| 16| 64
5| 1.709976| 2.236068| 25| 125
6| 1.817121| 2.449490| 36| 216
7| 1.912931| 2.645751| 49| 343
8| 2.000000| 2.828427| 64| 512
9| 2.080084| 3.000000| 81| 729
10| 2.154435| 3.162278| 100|1000
Notes:
Lab 9.10 — A Lottery of Sorts (FeelingLucky) (Approval)
Write a program that prompts the user for an integer between 1 and 100, and then begins generating (and printing) random integers between
1 and 100 until the chosen number appears. The number of random numbers generated until the match is then printed.
Please use a seed value of 1 when creating your Random object (i.e., new Random(1)
).
Sample Test Run
Here is a sample execution of the program.
User input is in bold. Your program should replicate the prompts and output:
Pick a number between 1 and 100: 17
85
88
47
13
54
4
34
6
78
48
69
73
17
It took 13 tries to match
- Practice with random numbers. The specification of a seed was necessary so that:
- You can see the same output as my execution
- Codelab checks correctness by comparing your output to the correct output. Without a seed,
the random number sequence would be different than mine and the output would be different
(my solution also used a seed value of 1, thus ensuring that our 'random' sequences would
be identical).