CISC 1115
Introduction to Programming Using Java
Lab #10
Techniques II
Lab 10.1 — Totalling Above and Below a Threshold (ThresholdTotals)
The file values.text consists an initial threshold value (an integer) followed by a sequence of integers. Read in the integers (until eof) and keep a total
of those numbers above (or equal to) the threshold, and those below, printing out the sums after all integers have been read in.
Sample Test Run
For example if the file values.text
contains:
50
23
67
50
13
23
the program should produce the following output:
The sum of the values above the threshold of 50 is 117
The sum of the values below the threshold of 50 is 59
- This example illustrates that totals (accumulations) are often conditional; in this case we are totalling values saitsfying
the conditions of being above/below some threshold. This exercise also has an example of a file with different types of data
(threshold value / sequence of integers) in the same file (previously we hade meta-data -- e.g. a header value, and data; here both the threshold
and the sequence are straight out-and-out data)..
Lab 10.2 — Totalling Up To 100 (Until100)
Read in and total values from the keyboard, stopping onc the total reaches 100, and printing out the last value of the total before it reached 100.
Sample Test Run
Here is a sample execution of the program.
User input is in bold. Your program should replicate the prompts and output:
Enter a number: 80
Enter a number: 10
Enter a number: 15
2 numbers were read in with a total of 90 before 100 was reached
- This is a 'tricky variation on Program 10.2 — whereas over there you stopped after you reached a total of 100 and then printed the
sum out; in this exercise, you still have to reach or pass 100, but the value printed out is the last sum right before that.
It's a matter of getting the while condition, the loop body, and the 'pump priming' right (hint you'll need to keep track of the total
before the next number is added, because that is what is printed).
Lab 10.3 — Counting Above and Below a Threshold (ThresholdCounts)
The file values.text consists an initial threshold value (an integer) followed by a sequence of integers. Read in the integers (until eof) and keep a count
of those numbers above (or equal to) the threshold, and those below, printing out the counts after all integers have been read in.
Sample Test Run
For example if the file values.text
contains:
50
23
67
50
13
23
the program should produce the following output:
The number of values above the threshold of 50 is 2
The number of values below the threshold of 50 is 3
- Simlar to Lab 10.1, but keeps track of the count rather than the sum. The relationship between the count and sum is similar to that
of the loop variable and value read in in an input loop,in both cases the former is simply how many times we've encountered a value
or entered the loop, which the latter is concerned with the actual value of the data encountered.
Lab 10.4 — Finding the Last Occurrence of a Value in a File (FindLast)
Write a program that prompts the user for a value and searches the file
numbers.text
for that value, printing out
the
last position where the number appears in the file (the first number in the file is at position 1). If the number
does not appear in the file, print out an appropriate message. Prompting the user for values should repeat until end of file.
Sample Test Run
For example if the file numbers.text
contains:
10
23
43
5
12
23
9
8
10
1
16
9
execution of the program should look like:
Enter a number: 10
10 last appears in the file at position 9
Enter a number: 29
29 does not appear in the file
Enter a number: 9
9 last appears in the file at position 12
Enter a number:
Some Guidance and Notes
- Write a method
int findLast(int val)
, that accepts the value to be searched, creates a Scanner
object for the file numbers.text
and searches the file
- The search logic is a bit different since we're interested in the last occurrence, not the first.
- With an array (coming soon), we could search backwards; however, there's no way for us to do
that with a file (at least not with a
Scanner
).
- In your
main
method, have a Scanner
for the keyboard, prompt the user for values, call the
above method with the value read in, and use the returned value to determine what to print.
- Refer to Lecture 9 for handling eof at the keyboard.
- A variation of the forward search through the file (Program 10.4).
- Practice with two scanners and eof processing from the keyboard