Project 1: Java I/O, Arrays, Objects, Exceptions
Project files are in project1.jar
For this project, you will be writing a program
that will visit various sources of information to collect data. At
each stop, the data collected will be added to any previously
collected data and saved in an array for later recall. It's sort of like
a fishing trip ... in fact, the data items you will be collecting
will be information about some channel catfish catches and the container
where you keep the information you gather
will be an array of BigFish objects.
The sources for the information you gather will be
- local text files (in your directory)
- remote text files (somewhere on the web)
- binary files (not human readable)
- object files, which can contain objects or even collections of objects
You are to save the stuff you've found in a
file of
BigFish objects and you are also to display
that information on your console.
The stuff you read (from keyboard, files or other locations)
has to have a
known format or organization. For this purpose, we have defined a
Java class called BigFish with three
attributes (year caught, weight, location), a toString()
method which can be used in conjuntion with println() to
display the object's content, and
constructors which will allow you to create new BigFish
objects when necessary.
The BigFish class definition has been coded for you and is
provided as a part of this project.
As you discover more about doing I/O, you quickly
realize that I/O operations don't always succeed. Sometimes,
something might go wrong (file not found, wrong data type,
end of file, etc.).
This leads us to the topic of exceptions and exception handling,
which will also be a part of this
project. When you are doing operations where an exception might occur,
Java will sometimes require you to specify what you want to do should
an exception occur. You have two choices:
- handle the exception: which means you have to write Java
code spelling out what is to be done if an exception occurs
- pass the exception on to the next higher level: which means
that you are willing to let the executing routine be immediately
terminated and have control return to the calling routine which will
then have the same two choices (handle the exception or pass it on).
For the Java features/classes/objects you use in this project,
you will need to be aware of the ones that can generate (
throw)
exceptions. There is no sense in trying to handle an exception unless
you know how you want to handle it. Thus, it may make sense, in some
cases, to pass the exception up to the calling routine (in which case
your program will terminate abnormally with a fatal error).
Here is a suggested list of steps for this project.
- Step 1
Read all the data from a local text file (provided for you in the
project jar file) named "bigfish.txt" which you
have stored in your own local directory. The number of lines in this file
will be a multiple of three. The first line of each triple will
identify the year of the catch. The second line will be the weight of the
fish. The third line will be the location where the fish was caught.
When all the catfish data has been added to your array, display the
data on the console, and then print some summary data which includes
the total number of fish in your collection and their average weight
(even though that is a pretty meaningless statistic). Finally,
use an ObjectOutputStream to write all the catfish data to a file named
"myfish.objects" as a stream of BigFish objects.
- Step 2
Enhance the program you wrote for step 1 by adding code to read
additional data from a remote text file and add it to the data collected
in the first step.
The file containing the data for step two is
"http://thinkdeeply.net/java/projects/project1/bigfish.txt".
When all the file data from both local and remote text files has been
added to your array, display the
data and summary statistics on the console. Also write the data to
the "myfish.objects" file as before.
- Step 3
Enhance the program you wrote for step 2 by adding code to read
additional data from a binary file named "bigfish.data" which will
be found at the URL
"http://thinkdeeply.net/java/projects/project1/bigfish.data"
This file contains triples of values (year; weight; location in that order)
which were written using the writeDouble and writeUTF methods, so
you will need to use the compatible readDouble and readUTF methods to
read the data from this file.
When all the file data has been added to your vector, display the
data and summary statistics on your console, and write everything to
the "myfish.objects" file.
- Step 4
Enhance the program you wrote for step 3 by adding code to read
additional data from a file of objects named "bigfish.object" which will
be found at the URL
"http://thinkdeeply.net/java/projects/project1/bigfish.object"
When all the file data has been added to your vector, display the
data and summary statistics on your console and write the "myfish.objects"
file as before.
You will need to use the readObject method to input data from the
bigfish.object file.
Each object you read from the file needs to be cast as a
BigFish object so that you can work with it like your other
BigFish objects.
The local data file (bigfish.txt),
the BigFish class (BigFish.java),
and a demo program (Project1Demo.java) to
help you get started are all in a jar (Java Archive) file named
http://thinkdeeply.net/java/projects/project1.jar
which you can download from the class web site.
Also included in the project1 jar file is a java program named
"Check1" which you can run to display the contents of your
myfish.objects output file. It will display each of the BigFish
objects in your objects file and will compute the average weight
of those fish. The average weight it calculates should agree with
the average weight you calculate. [Note: The jar file contains only
Check1.class file, not the Check1.java file. Therefore you can't
see the source code for the Check1 program, and you don't need to
compile it, since I have already done that for you.]
All your code (main routine and and any subroutines you decide to code)
should be one java class, saved in a java file whose name is your first initial
(capitalized) followed by your last name (with a leading capital letter) followed
by the numeral 1 (e.g., BObama1.java).
Attach this java file to an email message and send it to joel.carver@und.edu
before the deadline. Do not send any class files, data files, or other files as
additional attachments. Do not send a zip file.
All of your work for this project should be turned in as one uncompressed java file.