## CISC 1115 Introduction to Modern Programming Techniques Lab #1 Review

### Lab 1.1 (Lab 6.6 in the 1115 labs) — Fun with Numbers (InfoOf3) (Approval)

Write a program that reads three numbers from the keyboard, and then prints out some information about the relationships between the numbers-- whether they are all equal, in ascending order, in descending order, and so on. To determine these relationships, write the following boolean-valued functions:
• `boolean allAreEqual(int a, int b, int c);`
• `boolean twoAreEqual(int a, int b, int c);` // `false` if all three are equal
• `boolean noneAreEqual(int a, int b, int c);`
• `boolean areAscending(int a, int b, int c);` // `true` if a <= b <= c
• `boolean areDescending(int a, int b, int c);` // `true` if a >= b >= c
• `boolean strictlyAscending(int a, int b, int c);` // `true` if a < b < c
• `boolean strictlyDescending(int a, int b, int c);` // `true` if a > b > c
Try to "talk boolean" — i.e., use boolean variables and conditional operators rather than if statements where possible
• For example, write:
```boolean isNegative = x < 0;
```
rather than:
```		boolean isNegative;
if (x i< 0)
isNegtive = true;
else
isNegative = false;
```

Sample Test Run

first number? 1
second number? 2
third number? 3
allAreEqual: false
twoAreEqual: false
noneAreEqual: true
areAscending: true
areDescending: false
strictlyAscending: true
strictlyDescending: false

• Simple file I/O
• Conditionals and logical operators
• Simple methods
• This exercise is marked for 'Approval' because:
• I want to check your program structure and indentation
• I want to make sure you are actually defining and calling the specified methods

### Lab 1.2 (Lab 9.5 in the 1115 labs) — 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 somewhat more complex structure — 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).

### Lab (1.3) (Lab 10.4 in the 1115 labs) — Finding the Last Occurrence of a Value in a File (FindLast)

Write a program that prompts the user (at the keyboard)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. Continue asking the user for values until they signal end-of-file (i.e., in Windows one enters end-of-file at the keyboard by entering Ctl-Z, in Unix end-of-file is indicated by entering Ctl-D). The logic is the same as end-of-file for a disk file-- i.e., if your Scanner variable is named 'keyboard', you would have the loop condition 'while (keyboard.hasNextInt())'.

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
• Somewhat involved logic — keeping track of most recent position of searched value

### Lab 1.4 (Lab 12.1 in the 1115 labs) — Array Information (ArrayInfo) (Approval)

The file `numbers.text` contains a header value, followed by that many double values. Create an array of the appropriate size, populate the array with the doubles, and print out various information about the array (see below).

Sample Test Run

For example if the file `numbers.text` contains:

```8
12.3
2.5
9.4
3.14
22.15
17
54.3
7.6
```
the program should produce the following output:
```The array: {12.3, 2.5, 9.4, 3.14, 22.15, 17.0, 54.3, 7.6} contains 8 elements
The first element of the array is 12.3
The last element of the array is 7.6 and is at position 7
The middle element of the array is 3.14 and is at position 3
The largest element of the array is 54.3 and is at position 6
The smallest element of the array is 2.5 and is at position 1
```
Notes:
• You should be using methods for printing, and the max/min calculations.
• Notice the array is printed using comma logic.
• Even though I'm not holding you responsible for remembering the logic for reproduction on an exam, there's no reason you shouldn't use it in an assignment, where you have access to the proper code in the lecture notes.
• Feel free to copy any of the code/methods from lecture 12 (there's no real need to use the `first`, `last`, or `middle` methods — — feel free to simply use the proper subscripts).
• While the `max` method presented inthe lecture notes is useful, it is not enough; you are not just being asked to print out the largest and smallest value, you are also required to print out their positions (i.e., indexes in the array). You thus should consider writing a `maxPos` method that returns the index (rather than the actual value) of the largest element in the array (same for minimu).
• Reading in data using a header value
• More practice with arrays
• 'Muscle-memory practice for comma logic
• A variation on the `max` method; this also emphasizes the distinction between the value of the element and its index (position/location within the array).
• This exercise is marked for 'Approval' because:
• Again, there are several methods I want to make sure you're using
• I just want to see your programming style