CISC 1115
Introduction to Programming Using Java
Lab #9
Loops
Lab 9.1 — Sum of Pairs Revisited — Take 1 (SumOfPairs)
The file numbers.text consiits 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 consiits 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 nly, 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 noo 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 conisits 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 conists 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 becase the trailer value was epcified to be a pair (rather than the actual data being pairs).
- And again, it's imortant to distinguish between meta-data and data.
Lab 9.4 — Sum of Pairs Revisited — Take 4 (SumOfPairs)
The file numbers.text conisits 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.5 — Calculating Averages (Averages)
The file numbers.text conisits 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.6 — A Numeric Table (NumericTable)
Print a table of the numbers from 1 to 10 diaplying 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.7 — A Numeric Table &mdash Take 2 (NumericTable) (Optional)
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.8 — A Lottery of Sorts (FeelingLucky) (Optional)
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 number. The specification of a seed was necessary so that:
- You can see the same output a 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).