-
Notifications
You must be signed in to change notification settings - Fork 1
/
Cemetery.java
253 lines (240 loc) · 8.28 KB
/
Cemetery.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/**`
* Module 5 Project: Cemetery
*
* Name: Hershel Thomas
*
* AP Computer Science, Virtual Virginia
*/
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.text.DecimalFormat;
import java.util.Arrays;
public class Cemetery
{
//////// MAIN ////////
public static void main (String [] args)
{
File file = new File("cemetery.txt");
int numEntries = countEntries(file);
Person[] cemetery = readIntoArray(file, numEntries);
//TESTING ONLY: un-comment the 2 lines below to see if array was created properly
//for (int i = 0; i < numEntries; i++)
//System.out.println(cemetery[i].getName() + " " + cemetery[i].getAge());
int min = locateMinAgePerson(cemetery);
int max = locateMaxAgePerson(cemetery);
System.out.println("In the St. Mary Magdelene Old Fish Cemetery --> ");
System.out.println("Name of youngest person: " + cemetery[min].getName());
System.out.println("Age of youngest person: " + cemetery[min].getAge());
//prints out ResidentialAddress of youngest person in order to meet level 2 requirments.
System.out.println("Residential Address of youngest person: " + cemetery[min].getResidentialAddress());
System.out.println("Name of oldest person: " + cemetery[max].getName());
System.out.println("Age of oldest person: " + cemetery[max].getAge());
//prints out ResidentialAddress of oldest person in order to meet level 2 reuirments.
System.out.println("Residential Address of oldest person: " + cemetery[max].getResidentialAddress());
//prints out most common Residential Adress and how many times it appears to meet full level 2 requriments.
System.out.println(mostPopularAddress(cemetery));
}
//////// METHODS (Cemetery) ////////
/* Counts and returns the number of entries in File f.
* Uses a try-catch block.
* @param f -- the file object
*/
public static int countEntries(File f)
{
int i = 0;
try
{
Scanner scan = new Scanner(f);
while (scan.hasNextLine())
{
i++;
scan.nextLine();
}
}
catch (Exception e)
{
System.out.println("Check filename.");
}
return i;
}
/* Reads the data.
* Fills the array with Person objects.
* Uses a try-catch block.
* @param f -- the file object
* num -- the number of lines in the File f
*/
public static Person[] readIntoArray (File f, int num)
{
Person[] persons;
persons = new Person[num];
try
{
Scanner scan = new Scanner(f);
String nexLi = scan.nextLine();
for (int j = 0; j < persons.length; j++) {
persons[j] = makePerson(nexLi);
if (scan.hasNextLine()) {
nexLi = scan.nextLine();
}
}
}
catch (Exception e)
{
System.out.println("File not found");
}
return persons;
}
/*A method to return the String telling the most popular Address
* and how many times it appeard to achieve the full 100 percent grade
*/
public static String mostPopularAddress(Person[] arr) {
String address = "";
int countNames = 0;
int mostPopular = 0;
for (int i = 0; i < arr.length; i++) {
countNames = 0;
for (int j = 0; j < arr.length; j++) {
if (arr[i].getResidentialAddress().equals(arr[j].getResidentialAddress())) {
countNames++;
}
if (countNames > mostPopular) {
mostPopular = countNames;
address = arr[i].getResidentialAddress();
}
}
}
return "Most common Residential Adress is: " + address + ", appearing " + mostPopular + " times!";
}
/* A helper method that instantiates one Person object.
* @param entry -- one line of the input file.
*/
private static Person makePerson(String entry)
{
String currentName = entry.substring(0,25);
String currentBurialDate = entry.substring(25, 37);
String currentAge = entry.substring(37,42);
//Adding currentBurialPlace in order to meet level 2 requriments.
String currentResidentialAddress = entry.substring(42);
return new Person(currentName, currentBurialDate, currentResidentialAddress, currentAge);
}
/* Finds and returns the location (the index) of the Person
* who is the youngest.
* @param arr -- an array of Person objects.
*/
public static int locateMinAgePerson(Person[] arr)
{
double minAge = 10000000;
int locateMin = 0;
//String minPerson = "not working if get this";
for (int i = 0; i < arr.length; i++) {
if (Double.parseDouble(arr[i].age) < minAge) {
minAge = Double.parseDouble(arr[i].age);
//minPerson = arr[i].toString();
locateMin = i;
} else {
locateMin = locateMin;
}
}
return locateMin;
}
/* Finds and returns the location (the index) of the Person
who is the oldest.
@param arr -- an array of Person objects.
*/
public static int locateMaxAgePerson(Person[] arr)
{
double maxAge = 0;
int locateMax = 0;
//String maxPerson = "not working if get this";
for (int i = 0; i < arr.length; i++) {
if (Double.parseDouble(arr[i].age) > maxAge) {
maxAge = Double.parseDouble(arr[i].age);
//maxPerson = arr[i].toString();
locateMax = i;
} else {
locateMax = locateMax;
}
}
return locateMax;
}
}
class Person
{
//// FIELDS ////
/* Declare fields for the name, the burial date, and the age. */
public String name;
public String burialDate;
public String residentialAddress;
public String age;
/* @param n -- a String
////// CONSTRUCTOR //////
/* @param n -- a String representing a name from the input file.
* bd -- a String representing a burial date from the input file.
* a -- a String representing an age from the input file.
* Level 2 requriment: bp -- String reresenting residential Adress from input file.
*/
public Person(String n, String bd, String ra, String a)
{
name = n;
burialDate = bd;
residentialAddress = ra;
age = calculateAge(a) + "";
}
//////// METHODS (Person) ////////
/* Calculates the numerical equivalent of an age in String format.
* If the String contains a "w" (weeks) or "d" (days), calculates appropriate portion
* of a year.
@param a -- a String representing a person's age.
*calulates age in term of years.
*/
public double calculateAge(String a)
{
double numericalAge;
if(a.contains("w"))
{
int pos = a.indexOf("w");
double numWeeks = Double.parseDouble(a.substring(0,pos));
numericalAge = numWeeks/52.0;
}
else if(a.contains("d"))
{
//write code here!
int pos = a.indexOf("d");
double numDays = Double.parseDouble(a.substring(0,pos));
numericalAge = numDays/365.0;
}
else
{
//write code here!
double num = Double.parseDouble(a.substring(0,a.length()));
numericalAge = num*1.0;
}
return numericalAge;
}
////////// ACCESSOR METHODS (Person) //////////
/* Write 3 accessor methods for the fields of the Person class.
* (See the main method in the Cemetery class.)
*/
/*For Level 2 requriments, I can program an accessor method for
* burial place and create a
*
*/
public String getName() {
return name;
}
public String getBurialDate() {
return burialDate;
}
/*adding getResidentialAddress accessor method here for 4th method
* that returns residential address in order to meet level 2 requirments
* Used in conjuction with Method that returns the most popular adress
* including how many times it appeared.
*/
public String getResidentialAddress() {
return residentialAddress;
}
public String getAge() {
return age;
}
}