Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Arrays;
- import java.util.Scanner;
- import javax.swing.JOptionPane;
- /**
- * The class manages a set of <i>EarthquakeRecord</i> objects. One set of
- * <i>EarthquakeRecord</i> objects will be loaded into memory, but there may be
- * multiple indexes to those objects. The sequence of each index is based on a
- * sort order which is defined by <i>Comparator</i> objects built in the
- * <i>EarthquakeRecord</i> class.
- *
- * @author Rex.Woollard@AlgonquinCollege.com
- * @author Stanley Pieda piedas@algonquincollege.com
- */
- public class EarthquakeDataSet {
- private static final String CompareCity = null;
- private static final String CompareMagnitude = null;
- private static final String CompareDepth = null;
- /**
- * Tracks number of records actually stored in array container. The primary
- * array may have unused elements if it is larger than this value.
- */
- private int numRecordsLoaded;
- /**
- * Reference to the primary array which will be allocated when processing
- * the file; it may be oversized if the user specifies a size which is
- * greater than the number of records.
- */
- private EarthquakeRecord[] recordsInOriginalOrder;
- /**
- * Reference to array which will be created after loading records; after
- * sorting, it will contain <i>EarthquakeRecord</i> references sequenced by
- * City name; the array will be sized precisely based on value in
- * <i>nNumRecordsProcessed</i>.
- */
- private EarthquakeRecord[] recordsSortedByCity;
- /**
- * Reference to array which will be created after loading records; after
- * sorting, it will contain <i>EarthquakeRecord</i> references sequenced by
- * Depth; the array will be sized precisely based on value in
- * <i>nNumRecordsProcessed</i>.
- */
- private EarthquakeRecord[] recordsSortedByDepth;
- /**
- * Reference to array which will be created after loading records; after
- * sorting, it will contain <i>EarthquakeRecord</i> references sequenced by
- * Magnitude; the array will be sized precisely based on value in
- * <i>nNumRecordsProcessed</i>.
- */
- private EarthquakeRecord[] recordsSortedByMagnitude;
- public EarthquakeDataSet() {
- }
- /**
- * Accepts filename as argument; opens file; creates array of references
- * (the container); builds <i>EarthquakeRecord</i> objects and adds to
- * container.
- *
- * @param sFileName
- * @return long value that represents the time taken to complete file
- * processing (in milliseconds)
- * @throws Exception
- * 1:File open error 2:Read record error 3:Parse field error
- */
- public long loadDataFromFile(File file) throws FileNotFoundException,
- Exception {
- Scanner scanInput = new Scanner(file); // can throw
- // FileNotFoundException
- // which is caught in main()
- int maxNumRecords;
- do { // guard against negative or 0 value from user, since those would
- // be meaningless entries
- maxNumRecords = Integer.parseInt(JOptionPane
- .showInputDialog("Maximum Number of Records: "));
- } while (maxNumRecords <= 0);
- numRecordsLoaded = 0; // nNumRecordsProcessed may be less then
- // maxNumRecords if the array is larger than
- // needed
- long startTime = System.currentTimeMillis(); // used to track the timing
- // of each significant
- // activity
- // first array may be oversized, if fewer records exist than
- // maxNumRecords,
- // then subsequent arrays will be smaller
- recordsInOriginalOrder = new EarthquakeRecord[maxNumRecords];
- try { // EarthquakeRecord file parsing manages exception handling and
- // accounts for significant variability in field formats,
- // this try block catches residual unhandled exceptions
- while (numRecordsLoaded < maxNumRecords && scanInput.hasNext()) {
- // rawRecord references a String object with the raw, unparsed
- // data for one earthquake record
- String rawRecord = scanInput.nextLine();
- // EarthquakeRecord constructor will parse raw String into
- // specific instance variables
- recordsInOriginalOrder[numRecordsLoaded++] = new EarthquakeRecord(
- rawRecord);
- }
- } catch (Exception e) {
- throw new Exception("Input failure at record: " + numRecordsLoaded);
- }
- System.out.printf("Number Records Loaded: %d", numRecordsLoaded);
- // calculate the elapse time to load the data from disk into memory
- return System.currentTimeMillis() - startTime;
- }
- public long copyOriginalArray() {
- // There will only ever be a single set of EarthquakeRecord objects, but
- // there will be 4 big arrays of references to manage 4 different views
- // of that common set of objects.
- // Create parallel arrays of references, which will act as indexes;
- // Populate with the identical sequence of reference values using
- // <i>copyOf()</i>.
- // Before sorting the three arrays will contain identical sets of values
- // in the identical sequence.
- // After sorting the three arrays will contain identical sets of values
- // but in different sequences (to reflect the different sort order).
- // TODO: capture start time.
- // TODO: Call Arrays.copyOf() for each of the other array references.
- // TODO: Return the elapse time
- long startTime = System.currentTimeMillis();
- recordsSortedByCity = Arrays.copyOf(recordsInOriginalOrder,
- recordsInOriginalOrder.length);
- recordsSortedByMagnitude = Arrays.copyOf(recordsInOriginalOrder,
- recordsInOriginalOrder.length);
- recordsSortedByDepth = Arrays.copyOf(recordsInOriginalOrder,
- recordsInOriginalOrder.length);
- return System.currentTimeMillis() - startTime;
- }
- /**
- * The sort action is a batch process and is analogous to Google indexing,
- * where Google builds a planetary index of data long before you make a
- * Google search request. This sort process is prebuilding the indexes of
- * <i>EarthquakeRecord</i> objects to facilitate multiple searches.
- */
- public void sort() {
- // TODO: Capture start time for first sort.`
- // TODO: Call Arrays.sort() for the array referenced by
- // recordsSortedByCity
- // TODO: Calculate the elapse time for this one sort and output to the
- // screen.
- long startTime = System.currentTimeMillis();
- Arrays.sort(recordsSortedByCity,
- EarthquakeRecord.CompareByCity.instance);
- System.out.println(System.currentTimeMillis() - startTime);
- // TODO: Capture start time for second sort.
- // TODO: Call Arrays.sort() for the array referenced by
- // recordsSortedByDepth
- // TODO: Calculate the elapse time for this one sort and output to the
- // screen.
- long startTime2 = System.currentTimeMillis();
- Arrays.sort(recordsSortedByDepth,
- EarthquakeRecord.CompareByDepth.instance);
- System.out.println(System.currentTimeMillis() - startTime2);
- // TODO: Capture start time for third sort.
- // TODO: Call Arrays.sort() for the array referenced by
- // recordsSortedByMagnitude
- // TODO: Calculate the elapse time for this one sort and output to the
- // screen.
- long startTime3 = System.currentTimeMillis();
- Arrays.sort(recordsSortedByMagnitude,
- EarthquakeRecord.CompareByMagnitude.instance);
- System.out.println(System.currentTimeMillis() - startTime3);
- }
- /**
- * Allows user to select index which will be used to display, and thus
- * determine an alternate sequence.
- */
- public void display() {
- String[] sortKeys = { "Original", "City Name", "Magnitude", "Depth" };
- String returnValue = (String) JOptionPane.showInputDialog(null,
- "Select array to sequence display:", "Key Sequence",
- JOptionPane.INFORMATION_MESSAGE, null, sortKeys, sortKeys[0]);
- if (returnValue.equals(sortKeys[0])) {
- display("Original Sequence", recordsInOriginalOrder);
- } else if (returnValue.equals(sortKeys[1])) {
- display("City Name Sequence", recordsSortedByCity);
- } else if (returnValue.equals(sortKeys[2])) {
- display("Magnitude Sequence", recordsSortedByMagnitude);
- } else {
- display("Depth Sequence", recordsSortedByDepth);
- }
- }
- /**
- * Displays data set based on selected view. Shows only a subset of data;
- * enough to survey for the correctness of sorting.
- *
- * @param sortLabel
- * <i>String</i> is shown to provide the user with context.
- * @param earthquakeRecords
- * reference to the array that contains references to the
- * <i>EarthquakeRecord</i> objects, but in the desired viewing
- * sequence.
- */
- private void display(String sortLabel, EarthquakeRecord[] earthquakeRecords) {
- System.out.println(sortLabel);
- if (numRecordsLoaded < 30)
- for (int i = 0; i < numRecordsLoaded; ++i)
- System.out.println(earthquakeRecords[i]);
- else {
- System.out.println("First 10 records:");
- for (int i = 0; i < 10; ++i)
- System.out.println(earthquakeRecords[i]);
- System.out.println("Middle 10 records:");
- int nStart = numRecordsLoaded / 2 - 5;
- int nEnd = nStart + 10;
- for (int i = nStart; i < nEnd; ++i)
- System.out.println(earthquakeRecords[i]);
- System.out.println("Last 10 records:");
- for (int i = numRecordsLoaded - 10; i < numRecordsLoaded; ++i)
- System.out.println(earthquakeRecords[i]);
- }
- }
- /* Received following mergesort code from Tyler, modified by Dia */
- private void merge(int left,int middle1,int middle2,int right,EarthquakeRecord[] record) {
- int leftIndex = left;
- int rightIndex = middle2;
- int combinedIndex = left;
- while (leftIndex <= middle1 && rightIndex <= right) {
- int mergeSort;
- EarthquakeRecord[] combined;
- if (mergeSort == 0) { // Sort City
- if (EarthquakeDataSet.CompareCity.instance.compare(
- record[leftIndex], record[rightIndex]) < 0)
- combined[combinedIndex++] = record[leftIndex++];
- else
- combined[combinedIndex++] = record[rightIndex++];
- }
- else if (mergeSort == 1) { // Sort Magnitude
- if (EarthquakeDataSet.CompareMagnitude.instance.compare(
- record[leftIndex], record[rightIndex]) < 0)
- combined[combinedIndex++] = record[leftIndex++];
- else
- combined[combinedIndex++] = record[rightIndex++];
- }
- if (mergeSort == 2) { // Sort Depth
- if (EarthquakeDataSet.CompareDepth.instance.compare(
- record[leftIndex], record[rightIndex]) < 0)
- combined[combinedIndex++] = record[leftIndex++];
- else
- combined[combinedIndex++] = record[rightIndex++];
- }
- }
- }
- /**
- * Data sets are potentially huge, thus <i>toString</i> is not used to
- * generate a representation of all data, only a count of the number of
- * <i>EarthquakeRecord</i> objects captured in the data set.
- */
- public String toString() {
- return "Number of Records: " + numRecordsLoaded;
- }
- }
Add Comment
Please, Sign In to add comment