COSC 236
Lab 9
The goal of this lab is to get used with basic Java notions introduced so
far and file processing (discussed in Lecture #8). Take a look at the examples
and the sample programs in the lecture notes and try to apply the same concepts
and style when writing your own programs. For this lab you will write 2
programs, given below. For all programs you have to write 2 versions: one that
throws the FileNotFoundException
exception and one that handles the exception (try/catch). Also, make sure that your
programs handle (consume) unwanted input. Test your programs in a similar
fashion to the output samples listed below.
First, create a file called "input.txt" that contains a
sequence of integer values separated by one or more spaces or returns. You
can use the Notepad to create the file. When you save the program choose the
"text file" option and name your file "input" because Notepad will
automatically append the extension ".txt". Put this file in the folder you are going to
use for the following Java programs (anyway, know where your text file is
located, so that you can give a full path in case it isn't in the same folder
as your Java program). You will use this file in the programs below.
1. Write a
Java program that reads the file "input.txt" and calculates and
prints the average of all integer numbers in this file.
SAMPLE OUTPUT:
throws FileNotFoundException |
try/catch |
OUTPUT SAMPLE #1 for input.txt: 1 2 3 4 5 6 x x x 8 12 x x 34 x x 10 20
OUTPUT SAMPLE
#2 for
input.txt: xxxxx x x x x xx x x
x x x
|
OUTPUT SAMPLE #1 for input.txt: 1 2 3 4 5 6 x x x 8 12 x x 34 x x 10 20
OUTPUT SAMPLE
#2 for
input.txt: 1 2 3 4 5 6 x x x
8 12 x x 34 x x 10 20
OUTPUT SAMPLE
#3 for
input.txt: xxxxx x x x x xx x x
x x x
|
2. Write a Java program that reads the file
"input.txt" and writes all even values from this file into a new file
called "output.txt."
SAMPLE OUTPUT:
throws FileNotFoundException |
try/catch |
OUTPUT SAMPLE #1 for input.txt: xx x xx 10 12 1 3 5 34 x 2 5 7 9 x x 44
OUTPUT SAMPLE
#2 for
input.txt: xx xx xx x x x xxx xxx |
OUTPUT SAMPLE #1: OUTPUT SAMPLE
#2 for
input.txt: xx x xx 10 12 1 3 5 34 x 2 5 7 9 x x 44
OUTPUT SAMPLE
#3 for
input.txt: xx xx xx x x xxx x xx x x x |
Notes:
A. The lab will NOT be graded, do NOT hand anything in to the
instructor.
B. The lab should be completed by the start of the next scheduled lab class.
Save the .java files on your disk and e-mail them (attachments)
to Vishal Patel at vpatel12@students.towson.edu
Very important: Make sure that you have COSC 236.section, your name,
and Lab#9 in the Subject box of your e-mail.
C. In case you have any problems, contact the TA or the instructor for
assistance.