Guest User

Untitled

a guest
Dec 11th, 2017
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.61 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5. import javax.swing.JOptionPane;
  6.  
  7. /**
  8. * The class manages a set of <i>EarthquakeRecord</i> objects. One set of
  9. * <i>EarthquakeRecord</i> objects will be loaded into memory, but there may be
  10. * multiple indexes to those objects. The sequence of each index is based on a
  11. * sort order which is defined by <i>Comparator</i> objects built in the
  12. * <i>EarthquakeRecord</i> class.
  13. *
  14. * @author Rex.Woollard@AlgonquinCollege.com
  15. * @author Stanley Pieda piedas@algonquincollege.com
  16. */
  17. public class EarthquakeDataSet {
  18. private static final String CompareCity = null;
  19. private static final String CompareMagnitude = null;
  20. private static final String CompareDepth = null;
  21. /**
  22. * Tracks number of records actually stored in array container. The primary
  23. * array may have unused elements if it is larger than this value.
  24. */
  25. private int numRecordsLoaded;
  26. /**
  27. * Reference to the primary array which will be allocated when processing
  28. * the file; it may be oversized if the user specifies a size which is
  29. * greater than the number of records.
  30. */
  31. private EarthquakeRecord[] recordsInOriginalOrder;
  32. /**
  33. * Reference to array which will be created after loading records; after
  34. * sorting, it will contain <i>EarthquakeRecord</i> references sequenced by
  35. * City name; the array will be sized precisely based on value in
  36. * <i>nNumRecordsProcessed</i>.
  37. */
  38. private EarthquakeRecord[] recordsSortedByCity;
  39. /**
  40. * Reference to array which will be created after loading records; after
  41. * sorting, it will contain <i>EarthquakeRecord</i> references sequenced by
  42. * Depth; the array will be sized precisely based on value in
  43. * <i>nNumRecordsProcessed</i>.
  44. */
  45. private EarthquakeRecord[] recordsSortedByDepth;
  46. /**
  47. * Reference to array which will be created after loading records; after
  48. * sorting, it will contain <i>EarthquakeRecord</i> references sequenced by
  49. * Magnitude; the array will be sized precisely based on value in
  50. * <i>nNumRecordsProcessed</i>.
  51. */
  52. private EarthquakeRecord[] recordsSortedByMagnitude;
  53.  
  54. public EarthquakeDataSet() {
  55. }
  56.  
  57. /**
  58. * Accepts filename as argument; opens file; creates array of references
  59. * (the container); builds <i>EarthquakeRecord</i> objects and adds to
  60. * container.
  61. *
  62. * @param sFileName
  63. * @return long value that represents the time taken to complete file
  64. * processing (in milliseconds)
  65. * @throws Exception
  66. * 1:File open error 2:Read record error 3:Parse field error
  67. */
  68. public long loadDataFromFile(File file) throws FileNotFoundException,
  69. Exception {
  70. Scanner scanInput = new Scanner(file); // can throw
  71. // FileNotFoundException
  72. // which is caught in main()
  73. int maxNumRecords;
  74. do { // guard against negative or 0 value from user, since those would
  75. // be meaningless entries
  76. maxNumRecords = Integer.parseInt(JOptionPane
  77. .showInputDialog("Maximum Number of Records: "));
  78. } while (maxNumRecords <= 0);
  79.  
  80. numRecordsLoaded = 0; // nNumRecordsProcessed may be less then
  81. // maxNumRecords if the array is larger than
  82. // needed
  83. long startTime = System.currentTimeMillis(); // used to track the timing
  84. // of each significant
  85. // activity
  86. // first array may be oversized, if fewer records exist than
  87. // maxNumRecords,
  88. // then subsequent arrays will be smaller
  89. recordsInOriginalOrder = new EarthquakeRecord[maxNumRecords];
  90. try { // EarthquakeRecord file parsing manages exception handling and
  91. // accounts for significant variability in field formats,
  92. // this try block catches residual unhandled exceptions
  93. while (numRecordsLoaded < maxNumRecords && scanInput.hasNext()) {
  94. // rawRecord references a String object with the raw, unparsed
  95. // data for one earthquake record
  96. String rawRecord = scanInput.nextLine();
  97.  
  98. // EarthquakeRecord constructor will parse raw String into
  99. // specific instance variables
  100. recordsInOriginalOrder[numRecordsLoaded++] = new EarthquakeRecord(
  101. rawRecord);
  102. }
  103. } catch (Exception e) {
  104. throw new Exception("Input failure at record: " + numRecordsLoaded);
  105. }
  106. System.out.printf("Number Records Loaded: %d", numRecordsLoaded);
  107. // calculate the elapse time to load the data from disk into memory
  108. return System.currentTimeMillis() - startTime;
  109. }
  110.  
  111. public long copyOriginalArray() {
  112. // There will only ever be a single set of EarthquakeRecord objects, but
  113. // there will be 4 big arrays of references to manage 4 different views
  114. // of that common set of objects.
  115. // Create parallel arrays of references, which will act as indexes;
  116. // Populate with the identical sequence of reference values using
  117. // <i>copyOf()</i>.
  118. // Before sorting the three arrays will contain identical sets of values
  119. // in the identical sequence.
  120. // After sorting the three arrays will contain identical sets of values
  121. // but in different sequences (to reflect the different sort order).
  122.  
  123. // TODO: capture start time.
  124. // TODO: Call Arrays.copyOf() for each of the other array references.
  125. // TODO: Return the elapse time
  126. long startTime = System.currentTimeMillis();
  127. recordsSortedByCity = Arrays.copyOf(recordsInOriginalOrder,
  128. recordsInOriginalOrder.length);
  129. recordsSortedByMagnitude = Arrays.copyOf(recordsInOriginalOrder,
  130. recordsInOriginalOrder.length);
  131. recordsSortedByDepth = Arrays.copyOf(recordsInOriginalOrder,
  132. recordsInOriginalOrder.length);
  133. return System.currentTimeMillis() - startTime;
  134.  
  135. }
  136.  
  137. /**
  138. * The sort action is a batch process and is analogous to Google indexing,
  139. * where Google builds a planetary index of data long before you make a
  140. * Google search request. This sort process is prebuilding the indexes of
  141. * <i>EarthquakeRecord</i> objects to facilitate multiple searches.
  142. */
  143. public void sort() {
  144. // TODO: Capture start time for first sort.`
  145. // TODO: Call Arrays.sort() for the array referenced by
  146. // recordsSortedByCity
  147. // TODO: Calculate the elapse time for this one sort and output to the
  148. // screen.
  149.  
  150. long startTime = System.currentTimeMillis();
  151. Arrays.sort(recordsSortedByCity,
  152. EarthquakeRecord.CompareByCity.instance);
  153. System.out.println(System.currentTimeMillis() - startTime);
  154.  
  155. // TODO: Capture start time for second sort.
  156. // TODO: Call Arrays.sort() for the array referenced by
  157. // recordsSortedByDepth
  158. // TODO: Calculate the elapse time for this one sort and output to the
  159. // screen.
  160.  
  161. long startTime2 = System.currentTimeMillis();
  162. Arrays.sort(recordsSortedByDepth,
  163. EarthquakeRecord.CompareByDepth.instance);
  164. System.out.println(System.currentTimeMillis() - startTime2);
  165.  
  166. // TODO: Capture start time for third sort.
  167. // TODO: Call Arrays.sort() for the array referenced by
  168. // recordsSortedByMagnitude
  169. // TODO: Calculate the elapse time for this one sort and output to the
  170. // screen.
  171.  
  172. long startTime3 = System.currentTimeMillis();
  173. Arrays.sort(recordsSortedByMagnitude,
  174. EarthquakeRecord.CompareByMagnitude.instance);
  175. System.out.println(System.currentTimeMillis() - startTime3);
  176. }
  177.  
  178. /**
  179. * Allows user to select index which will be used to display, and thus
  180. * determine an alternate sequence.
  181. */
  182. public void display() {
  183. String[] sortKeys = { "Original", "City Name", "Magnitude", "Depth" };
  184. String returnValue = (String) JOptionPane.showInputDialog(null,
  185. "Select array to sequence display:", "Key Sequence",
  186. JOptionPane.INFORMATION_MESSAGE, null, sortKeys, sortKeys[0]);
  187. if (returnValue.equals(sortKeys[0])) {
  188. display("Original Sequence", recordsInOriginalOrder);
  189. } else if (returnValue.equals(sortKeys[1])) {
  190. display("City Name Sequence", recordsSortedByCity);
  191. } else if (returnValue.equals(sortKeys[2])) {
  192. display("Magnitude Sequence", recordsSortedByMagnitude);
  193. } else {
  194. display("Depth Sequence", recordsSortedByDepth);
  195. }
  196. }
  197.  
  198. /**
  199. * Displays data set based on selected view. Shows only a subset of data;
  200. * enough to survey for the correctness of sorting.
  201. *
  202. * @param sortLabel
  203. * <i>String</i> is shown to provide the user with context.
  204. * @param earthquakeRecords
  205. * reference to the array that contains references to the
  206. * <i>EarthquakeRecord</i> objects, but in the desired viewing
  207. * sequence.
  208. */
  209. private void display(String sortLabel, EarthquakeRecord[] earthquakeRecords) {
  210. System.out.println(sortLabel);
  211. if (numRecordsLoaded < 30)
  212. for (int i = 0; i < numRecordsLoaded; ++i)
  213. System.out.println(earthquakeRecords[i]);
  214. else {
  215. System.out.println("First 10 records:");
  216. for (int i = 0; i < 10; ++i)
  217. System.out.println(earthquakeRecords[i]);
  218.  
  219. System.out.println("Middle 10 records:");
  220. int nStart = numRecordsLoaded / 2 - 5;
  221. int nEnd = nStart + 10;
  222. for (int i = nStart; i < nEnd; ++i)
  223. System.out.println(earthquakeRecords[i]);
  224.  
  225. System.out.println("Last 10 records:");
  226. for (int i = numRecordsLoaded - 10; i < numRecordsLoaded; ++i)
  227. System.out.println(earthquakeRecords[i]);
  228. }
  229. }
  230.  
  231. /* Received following mergesort code from Tyler, modified by Dia */
  232. private void merge(int left,int middle1,int middle2,int right,EarthquakeRecord[] record) {
  233. int leftIndex = left;
  234. int rightIndex = middle2;
  235. int combinedIndex = left;
  236.  
  237. while (leftIndex <= middle1 && rightIndex <= right) {
  238. int mergeSort;
  239. EarthquakeRecord[] combined;
  240. if (mergeSort == 0) { // Sort City
  241. if (EarthquakeDataSet.CompareCity.instance.compare(
  242. record[leftIndex], record[rightIndex]) < 0)
  243. combined[combinedIndex++] = record[leftIndex++];
  244. else
  245. combined[combinedIndex++] = record[rightIndex++];
  246. }
  247. else if (mergeSort == 1) { // Sort Magnitude
  248. if (EarthquakeDataSet.CompareMagnitude.instance.compare(
  249. record[leftIndex], record[rightIndex]) < 0)
  250. combined[combinedIndex++] = record[leftIndex++];
  251. else
  252. combined[combinedIndex++] = record[rightIndex++];
  253. }
  254. if (mergeSort == 2) { // Sort Depth
  255. if (EarthquakeDataSet.CompareDepth.instance.compare(
  256. record[leftIndex], record[rightIndex]) < 0)
  257. combined[combinedIndex++] = record[leftIndex++];
  258. else
  259. combined[combinedIndex++] = record[rightIndex++];
  260. }
  261. }
  262. }
  263.  
  264. /**
  265. * Data sets are potentially huge, thus <i>toString</i> is not used to
  266. * generate a representation of all data, only a count of the number of
  267. * <i>EarthquakeRecord</i> objects captured in the data set.
  268. */
  269. public String toString() {
  270. return "Number of Records: " + numRecordsLoaded;
  271. }
  272. }
Add Comment
Please, Sign In to add comment