Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
1,031
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.72 KB | None | 0 0
  1. /**
  2. * Module 5 Project: Cemetery
  3. *
  4. * Name:
  5. *
  6. * AP Computer Science, Virtual Virginia
  7. */
  8. import java.util.Scanner;
  9. import java.io.File;
  10. import java.io.FileNotFoundException;
  11. import java.text.DecimalFormat;
  12.  
  13. public class Cemetery
  14. {
  15. //////// MAIN ////////
  16. public static void main (String [] args)
  17. {
  18. File file = new File("cemetery.txt");
  19. int numEntries = countEntries(file);
  20. Person[] cemetery = readIntoArray(file, numEntries);
  21.  
  22. //TESTING ONLY: un-comment the 2 lines below to see if array was created properly
  23. for (int i = 0; i < cemetery.length; i++)
  24. System.out.println(cemetery[i].getName() + " " + cemetery[i].getAge());
  25.  
  26. int min = locateMinAgePerson(cemetery);
  27. int max = locateMaxAgePerson(cemetery);
  28. System.out.println("In the St. Mary Magdelene Old Fish Cemetery --> ");
  29. System.out.println("Name of youngest person: " + cemetery[min].getName());
  30. System.out.println("Age of youngest person: " + cemetery[min].getAge());
  31. System.out.println("Name of oldest person: " + cemetery[max].getName());
  32. System.out.println("Age of oldest person: " + cemetery[max].getAge());
  33. }
  34.  
  35. //////// METHODS (Cemetery) ////////
  36.  
  37. /* Counts and returns the number of entries in File f.
  38. * Uses a try-catch block.
  39. * @param f -- the file object
  40. */
  41. public static int countEntries(File f)
  42. {
  43. int i = 0;
  44. try
  45. {
  46. Scanner scan = new Scanner(f);
  47. while (scan.hasNextLine())
  48. {
  49. i++;
  50. scan.nextLine();
  51. }
  52. }
  53. catch (Exception e)
  54. {
  55. System.out.println("Check filename.");
  56. }
  57.  
  58. return i;
  59. }
  60.  
  61. /* Reads the data.
  62. * Fills the array with Person objects.
  63. * Uses a try-catch block.
  64. * @param f -- the file object
  65. * num -- the number of lines in the File f
  66. */
  67. public static Person[] readIntoArray (File f, int num)
  68. {
  69. Person cemetaryArray[] = new Person[num-2];
  70. int i = 0;
  71. try
  72. {
  73. Scanner scan = new Scanner(f);
  74. while (scan.hasNextLine())
  75. {
  76. if(scan.hasNext("NAME "))
  77. scan.nextLine();
  78. else if(scan.hasNext("---"))
  79. scan.nextLine();
  80. else
  81. {
  82. cemetaryArray[i] = makePerson(scan.next());
  83. i++;
  84. scan.nextLine();
  85. }
  86. }
  87. }
  88. catch (Exception e)
  89. {
  90. System.out.println("Test.");
  91. }
  92. return cemetaryArray;
  93. }
  94.  
  95. /* A helper method that instantiates one Person object.
  96. * @param entry -- one line of the input file.
  97. */
  98. private static Person makePerson(String entry)
  99. {
  100. String currentName = entry.substring(0,25);
  101. String currentBurialDate = entry.substring(25,37);
  102. String currentAge = entry.substring(37,42);
  103. return new Person(currentName, currentBurialDate, currentAge);
  104. }
  105.  
  106. /* Finds and returns the location (the index) of the Person
  107. * who is the youngest.
  108. * @param arr -- an array of Person objects.
  109. */
  110. public static int locateMinAgePerson(Person[] arr)
  111. {
  112. double c = Integer.MAX_VALUE;
  113. for(Person i: arr)
  114. {
  115. if(i.getAge() < c)
  116. c = i.getAge();
  117. }
  118. return (int) c;
  119. }
  120.  
  121. /* Finds and returns the location (the index) of the Person
  122. who is the oldest.
  123. @param arr -- an array of Person objects.
  124. */
  125. public static int locateMaxAgePerson(Person[] arr)
  126. {
  127. double c =Integer.MIN_VALUE;
  128. for(Person i: arr)
  129. {
  130. if(i.getAge() > c)
  131. c = i.getAge();
  132. }
  133. return (int) c;
  134. }
  135. }
  136.  
  137. class Person
  138. {
  139. //// FIELDS ////
  140. private String name;
  141. private String burialDate;
  142. private double age;
  143. /* Declare fields for the name, the burial date, and the age. */
  144.  
  145. ////// CONSTRUCTOR //////
  146.  
  147. /* @param n -- a String representing a name from the input file.
  148. * bd -- a String representing a burial date from the input file.
  149. * a -- a String representing an age from the input file.
  150. */
  151. public Person(String n, String bd, String a)
  152. {
  153. name = n;
  154. burialDate = bd;
  155. age = calculateAge(a);
  156. }
  157.  
  158. //////// METHODS (Person) ////////
  159.  
  160. /* Calculates the numerical equivalent of an age in String format.
  161. * If the String contains a "w" (weeks) or "d" (days), calculates appropriate portion
  162. * of a year.
  163. @param a -- a String representing a person's age.
  164. */
  165. public double calculateAge(String a)
  166. {
  167. double numericalAge;
  168. if(a.contains("w"))
  169. {
  170. int pos = a.indexOf("w");
  171. double numWeeks = Double.parseDouble(a.substring(0,pos));
  172. numericalAge = numWeeks/52.0;
  173. }
  174. else if(a.contains("d"))
  175. {
  176. int pos = a.indexOf("d");
  177. double numDays = Double.parseDouble(a.substring(0,pos));
  178. numericalAge = numDays/365.0;
  179. }
  180. else
  181. {
  182. numericalAge = Double.parseDouble(a.substring(0));
  183. }
  184. return numericalAge;
  185. }
  186.  
  187. ////////// ACCESSOR METHODS (Person) //////////
  188.  
  189. /* Write 3 accessor methods for the fields of the Person class.
  190. * (See the main method in the Cemetery class.)
  191. */
  192. public double getAge()
  193. {
  194. return age;
  195. }
  196. public String getburialDate()
  197. {
  198. return burialDate;
  199. }
  200. public String getName()
  201. {
  202. return name;
  203. }
  204.  
  205.  
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement