CS 1401 Assignment #7

advertisement
CS 1401 Assignment #7
Date Assigned: Monday, March 20, 2006
Due Date: correspondingly, Monday, March 27, 2006, or Tuesday, March 28, 2006,
before the beginning of your lab section.
Goals: to practice with different kinds of file I/O operations, looping statements, classes,
and overloaded methods.
Points: This assignment is worth a total of 20 points. No late assignments will be
accepted, but credit will be given for partially completed assignments turned in on time.
Assignment: UTEP Computer Science students have the opportunity to gain valuable
experience through participation in research projects sponsored by organizations such as the
National Science Foundation, NASA, and other departments on campus. Some of the current
projects support the fast-growing field of geoinformatics, and involve the collaboration of CS
students and members of the Department of Geologic Sciences. For more information see:
http://paces.geo.utep.edu/research/geoinformatics/geoinformatics.shtml .
Problem 1 (12 points): Classes and Overloaded Methods. Seismic wave monitoring
stations are located throughout the world to measure the magnitude and pinpoint the
location of earthquakes. Seismic waves can be converted to a digital format by sampling
them at regular intervals as shown in the figure below. Digitized waves can then be
numerically processed and resequenced for analysis and simulation.
Step 1. To support numerical processing, you need to write a class named SamplePoint,
which allows recording of wave sample points. Every point must record the measured
value of the wave, the sample number, the wave identifier number, the number of the
seismograph where it was recorded, and the date when it was recorded. The class should
include the following components:
a. Define a default constructor method that takes all the values of a sample point as
parameters. Sample points plotted on the previous figure have the following values:
Value
0.000110
0.000250
0.000710
0.004891
0.030272
Sample:
0
1
2
3
4
Wave:
341
341
341
341
341
Seismograph:
8972
8972
8972
8972
8972
Date:
3/21/2006
3/21/2006
3/21/2006
3/21/2006
3/21/2006
b. Define an accessor method (a get method that returns the value stored in a data field)
for every SamplePoint instance field.
c. Define a method named toString that returns a string containing all of the values of a
SamplePoint object concatenated with labels for the field values.
d. To support resequencing, define a mutator method (a set method that changes the value
of a data field) for the sample field.
Step 2. Create an application class that performs the following tasks:
a. Prompts the user for values of a sample point and instantiates SamplePoint once using
the entered values.
b. Uses the toString method of the instanced SamplePoint to print to the screen the
instance field values.
c. Uses the sample mutator method to modify the position of the entered point within a
digitized wave, i.e., shift its current position up by 1000.
d. Uses toString to display the values of the SamplePoint object to verify its new
position.
Step 3. A method overloads an original method if it has the same name but a different
number and/or type of parameters. Overloading allows different versions of a method to
be used for different situations.
a. For sampled seismic waves, notice that a new sample point can be created with data
from a previous point in addition to the value and sample number of the new sample
point. Add an overloaded constructor method to the SamplePoint class. The new
constructor should use an existing sample point in addition to the wave value for a new
sample point. The new point should follow the existing point in sample number.
Step 4. Add the following tasks to the application class.
a. Prompt the user for the values of four more sample points and use the overloaded
constructor of Step 3 to create new SamplePoint instances from previously entered
points.
b. Use the toString method of all sample points to print a list of their field values.
Problem 2 (8 points) Looping and file I/O.
Write a Java application to help analyze data from a digitized seismic wave. Your
application should read a data file named digiwave.txt containing the values of a set of
points of a sampled wave. The following example shows the format of the data file:
Artic Seismic Observatory
Wave: 341
Seismograph: 8972
Date: 3/20/2006
Points:
0
0.000110
1
0.000250
2
0.000710
3
0.004891
4
0.030272
Your application should read in the data values from the file and compute the
minimum, maximum, and average value of the digitized seismic wave points. Print out to
an output file named digistats.txt the name of the input file, the wave number, the
seismograph number, the date, and the computed wave statistics. An example output file
may contain the following:
Input file: digiwave.txt
Wave: 341
Seismograph: 8972
Date: 3/20/2006
Minimum value: 0.000110
Maximum value: 0.030272
Average value: 0.0072466
For extra credit. Java provides an easy way to save and retrieve objects. Objects created in an
application may be written to a file to save their current state. Afterwards, the data values may be
read back in to recreate the object. This process is called serialization (translation from object to
bytes in a file) and deserialization (the reverse operation). To support serialization, the object’s
class definition must have the words “implements Serializable” following the class name to
indicate that the extra functionality of the Serializable interface type is available for the class. The
file objects used for output and input of the objects are of type ObjectOutputStream and
ObjectInputStream, and the associated I/O methods are writeObject and readObject. Implement
the Serializable interface for the SamplePoint class and save the current state of one of your
sample point objects to an output file. Read the object back in using deserialization. Refer to pp.
354-358 of the textbook for more details. Please note that you will only be given credit for the
extra credit assignment if your main assignment works.
Deliverables: as announced in the labs and explained in the handouts given to you in the labs.
Download