Date Assigned: Wednesday, March 2, 2005
Due Date: Wednesday, March 9, 2005, before the beginning of your lab section.
Goals: to practice with different kinds of 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. Precise time measurements are required in order to compute the magnitude and source of the seismic waves accurately. Your assignment is to design an application which implements a simple clock. The steps and the desired structure are described as follows:
Step 1. Create an object-template clock class. Include the following components in the clock class:
· Define a field for each clock object to store its own unique 4-digit integer station number, identifying where the clock is located.
· Define a field for each clock object to store its own time, represented as a string (for example, “12:43:50” representing 12 hours, 43 minutes and 50 seconds).
· Define a constructor method with one parameter, the clock’s station number. The default constructor should instantiate a clock object with a default time of 12:00:00.
· Define an accessor type of method (a get method to retrieve the value in a data field) to return the value of the clock’s time field.
· Define a mutator type of method (a set method to change the value in a data field) with a time value passed as an argument in order to set the value of the clock’s time field to a specific time.
· Define a method named toString which returns the value of the clock’s station number field and the current value of the clock’s time field.
Step 2. Create an application class, including the following tasks:
· Instantiate one clock object using the default constructor. Supply your own station number.
· Call the accessor method of the clock and print to the screen the value of the time field.
· Prompt the user to enter a new time for the clock in the format hh:mm:ss, and read in the new time.
· Call the mutator method of the clock and pass the user-supplied time in order to reset the time field of the clock to that value.
· Call the accessor method of the clock again and print to the screen the updated value of the time field.
· Use the toString method of the clock object to print out the state (field values) of the clock object, to verify the correct settings. Include the clock’s station number as well as the value of the time field.
· Check to make sure the tasks above are working correctly before going on to Step 3.
Step 3. Add the following overloaded methods and a helper method to the object-template class. A method overloads the original method if it has the same name but different types of parameters. Overloading allows different versions of method to be used for different situations.
· Define an overloaded constructor method with a specific time as a parameter. This constructor should instantiate a clock object, assign a station number for the clock, and assign the specific time to the time field. This allows a new clock object to be created and a specified time value to be initialized at the same time.
· Define an overloaded mutator method without parameters. Within this method, reset the clock’s time field back to the default of 12:00:00.
· Define a facilitator (helper type) synchronization method that takes another clock object as a parameter, accesses the time value of the other clock object, and sets the first clock to that same time. Note: Objects of the same class can access each other’s private fields directly, but do you want to do that? Enforce encapsulation by using the public accessor and mutator methods instead.
Step 4. Add the following tasks to the application class:
· Call the mutator method defined in Step 3 (the version without parameters) to reset the time field of the first clock object back to the default value “12:00:00”.
· Call the accessor method of the first clock again and print to the screen the changed value of the time field to verify that it has been reset to 12:00:00.
· Prompt the user to enter a new time for a second clock object in the format hh:mm:ss, and read it in.
· Instantiate a second clock object using the overloaded constructor defined in Step 3 with the user-supplied time as a parameter.
· Call the accessor method of the second clock and print to the screen the value of the time field to verify that the clock’s time field has been set to the user’s time.
· Call the synchronization method of the first clock object to set the time field of that clock object to match the same value as the time field of the second clock object.
· Use the toString methods of both clock objects to print out the state of both clock objects.
Problem 2 (8 Points): Looping.
Your assignment is to write a Java application to help analyze data from seismically active regions of the world. The application should read a data file named quake.txt containing recent measurements of earthquake magnitudes in various regions. The magnitude numbers are based upon the Richter scale, expressed in whole numbers and decimal fractions. The date of the earthquake measurement is included on the line below the Richter number. A special character # is included at the top of the file and at the end of the magnitude measurements for each region. Your application should work for a data file of any size. The following example shows the format of the data file:
OFF THE WEST COAST OF
Thursday, February 24, 2005 at 23:26:41
Tuesday, February 22, 2005 at 02:25:22
Tuesday, February 15, 2005 at 12:43:50
Monday, February 28, 2005 at 04:57:01
Write your application to do the following:
1. Read in the data values from the file, and for EACH region compute the following based upon the data in the file:
· The maximum magnitude value for that region contained in the file.
· The total number of earthquakes included in the file for that region.
2. Print out to an output file named summary.txt the following text for EACH region contained in the input data file:
· The name of the region
· The number of earthquakes for that region
· The magnitude of the maximum earthquake
· The date of the maximum earthquake
For example, if the sample data input file were used for this application, the output file summary.txt would contain the following lines:
Region: OFF THE WEST COAST OF NORTHERN SUMATRA Number of earthquakes reported: 3 Maximum magnitude: 6.4 on Tuesday, February 22, 2005 at 02:25:22
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 name must have the words “implements Serializable” following the class name to indicate that the extra functionality of the serializable interface is available for the class. The file objects used for output and input of objects are of type ObjectOutputStream and ObjectInputStream, and the associated methods are writeObject and readObject. Implement the serialization interface for the clock class, and save the current state of one of your clock 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.