CISC 1115
Introduction to Programming Using Java
Lab #9
Loops

How to Develop and Submit your Labs

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

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

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

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

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

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

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

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

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