CISC 1115
Introduction to Programming Using Java
Lab #5
Files

How to Develop and Submit your Labs

Lab 5.1 — Sums of Pairs (SumOfPairs)

The file numbers.text contain four numbers on two lines -- one pair per line. Read the numbers in as integers and print the sum of each pair — one sum per line. Use a for loop to process the two pairs. (As an aside, it makes no difference whether they are on the same line or not, as nextInt reads in the next integer, whether it is on the cirrent line of the next line. I simply placed the pairs on the same line for readability.)

Sample Test Run

For example if the file numbers.text contains:

1 2
3 4
the program should produce he following output:
The sum of 1 and 2 is 3
The sum of 3 and 4 is 7

Lab 5.2 — Concatenations of Pairs (ConcatOfPairs)

The file numbers.text contain four numbers on two lines -- one pair per line. Read the numbers in as Strings, and print out the concatenation of each pair — one pair per line. As in Lab 5.1, use a for loop to process the two pairs.

Sample Test Run

For exmple, if the file numbers.text contains:

1 2
3 4
the program should produce the following output:
The concatenation of 1 and 2 is 12
The concatenation of 3 and 4 is 34

Lab 5.3 — nextLine Demonstration (NextLineDemo)

The file mailing_list.text contains two lines of address data in the following format:
last-name first-name house-number street-name street-type city state zip
For example:
Arnow David 2900 Bedfore Avenue Brooklyn NY 11210
Write a program that reads the data from the file as a full line, using nextLine, and prints the line out. Use a for loop to process the two lines.

Sample Test Run

For example, if the file mailing_list.text contains:

Arnow David 2900 Bedford Avenue Brooklyn NY 11210
Weiss Gerald 123 Anywhere Street Nowhere NW 19826
the program should produce the following output:
The line of address data: Arnow David 2900 Bedford Avenue Brooklyn NY 11210
The line of address data: Weiss Gerald 123 Anywhere Street Nowhere NW 19826

Lab 5.4 — Mailing Label (MailingLabel) (Approval)

The file mailing_list.text for this lab is formatted as in Lab 5.3.

Write a program that reads the data from the file as individual strings using next (remember, next stops reading when it encounters whitespace — a blank, tab, or newline), and prints the data out as a mailing label (as shown below).

The label should be printed using the method (which you are to write):

void printLabel(String first, String last, String houseNumber, String streetName, String streetType, String city, String state, String zip)
(Note:: ALthough it is usually referred to as a 'house number', that field is actually a string as it could contain characters other than digits, e.g., 103-27 or 1115B).

Use a for loop (in main) that reads the data and calls the method for each line.

Sample Test Run

For example, if the file mailing_list.text contains:

Arnow David 2900 Bedford Avenue Brooklyn NY 11210
Weiss Gerald 123 Anywhere Street Nowhere NW 19826
the program should produce the following output:
David Arnow
2900 Bedford Avenue
Brooklyn, NY 11210

Gerald Weiss
123 Anywhere Street
Nowhere, NW 19826

Lab 5.5 — Mailing Label — Take 2 (MailingLabel)

Redo Lab 5.4, but this time prompt for the name of the mailing list data file (which is in the same format as before), rather than hardcoding it as mailing_list.text in the program).

Sample Test Run

For example, if the file first_list.text contains:

Arnow David 2900 Bedfore Avenue Brooklyn NY 11210
Weiss Gerald 123 Anywhere Street Nowhere NW 19826
here is a sample execution of the program:

Enter name of data file: first_list.text
David Arnow
2900 Bedfore Avenue
Brooklyn, NY 11210

Gerald Weiss
123 Anywhere Street
Nowhere, NW 19826

Lab 5.6 — Maximum of Data from Two Files — (TwoFileMax)

The data files, data1.text and data2.text contain 3 integers each. Write a program that opens the two data files (so you will need to declare and create two Scanners) — one for each file, and reads in one number from each file and prints out the larger of the two and which file it cam from.

Sample Test Run

For example, if the file data1.text contains:

12
3
45
and the file data2.text contains:
9
14
102
the program should produce the following output:
The larger of 12 and 9 is 12 and comes from data1.text
The larger of 3 and 14 is 14 and comes from data2.text
The larger of 45 and 102 is 102 and comes from data2.text