-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBabynames.java
More file actions
216 lines (166 loc) · 6.61 KB
/
Babynames.java
File metadata and controls
216 lines (166 loc) · 6.61 KB
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
import edu.duke.*;
import org.apache.commons.csv.*;
import java.io.*;
public class Babynames {
public void totalBirths(FileResource fr) {
int totalBirths = 0;
int boyTotalBirths = 0;
int girlTotalBirths = 0;
int boyCount = 0;
int girlCount = 0;
for (CSVRecord record : fr.getCSVParser(false)) {
int numBorn = Integer.parseInt(record.get(2));
totalBirths += numBorn;
if (record.get(1).equals("M")) {
boyTotalBirths += numBorn;
boyCount++;
} else {
girlTotalBirths += numBorn;
girlCount++;
}
}
System.out.println("Total data = " + (boyCount + girlCount));
System.out.println("Total births = " + totalBirths);
System.out.println("Total girls = " + girlCount);
System.out.println("Total girls births = " + girlTotalBirths);
System.out.println("Total boys = " + boyCount);
System.out.println("Total boys births = " + boyTotalBirths);
}
public void testTotalBirths() {
FileResource fr = new FileResource();
totalBirths(fr);
}
public int getRank(int year, String name, String gender) {
FileResource fr = new FileResource("D:/Coursera/Problem Solving with java/CSV FILE/us_babynames_by_year/yob" + year + ".csv" );
int rank = 0;
boolean recordFound = false;
for (CSVRecord record : fr.getCSVParser(false)) {
String currentName = record.get(0);
String currentGender = record.get(1);
if (currentGender.equals(gender)) {
rank++;
if (currentName.equals(name)) {
recordFound = true;
break;
}
}
}
if (recordFound) {
return rank;
} else {
return -1;
}
}
public void testGetRank() {
int year = 1971 ;
String name = "Frank";
String gender = "M";
int rank = getRank(year, name, gender);
System.out.println(name + " rank is " + rank);
}
public String getName(int year, int rank, String gender) {
FileResource fr = new FileResource("D:/Coursera/Problem Solving with java/CSV FILE/us_babynames_by_year/yob" + year + ".csv" );
String name = "NO NAME";
int currentRank = 0;
for (CSVRecord record : fr.getCSVParser(false)) {
String currentGender = record.get(1);
if (currentGender.equals(gender)) {
currentRank++;
if (currentRank == rank) {
name = record.get(0);
break;
}
}
}
return name;
}
public void testGetName() {
int year = 1982;
int rank = 450;
String gender = "M";
String name = getName(year, rank, gender);
System.out.println(year + " rank " + rank + " is " + name);
}
public void whatIsNameInYear(String name, int year, int newYear, String gender) {
int currentYearRank = getRank(year, name, gender);
String newName = getName(newYear, currentYearRank, gender);
System.out.println(name + " born in " + year + " would be " + newName + " if she was born in " + newYear);
}
public void testWhatIsNameInYear() {
whatIsNameInYear("Owen", 1974, 2014, "F");
}
public int yearOfHighestRank(String name, String gender) {
int year = Integer.MIN_VALUE;
int rank = Integer.MAX_VALUE;
DirectoryResource dr = new DirectoryResource();
for (File f : dr.selectedFiles()) {
int currentYear = Integer.parseInt(f.getName().substring(3, 7));
int currentRank = getRank(currentYear, name, gender);
if (currentRank != -1 && currentRank < rank) {
rank = currentRank;
year = currentYear;
}
}
if (year == Integer.MIN_VALUE) {
return -1;
} else {
return year;
}
}
public void testYearOfHighestRank() {
String name = "Mich";
String gender = "M";
System.out.println(name + " most popular year is " + yearOfHighestRank(name, gender));
}
public double getAverageRank(String name, String gender) {
double totalRank = 0;
int recordCount = 0;
DirectoryResource dr = new DirectoryResource();
for (File f : dr.selectedFiles()) {
int currentYear = Integer.parseInt(f.getName().substring(3, 7));
int currentRank = getRank(currentYear, name, gender);
if (currentRank != -1) {
totalRank += currentRank;
recordCount++;
}
}
if (recordCount == 0) {
return -1.0;
} else {
return totalRank / recordCount;
}
}
public void testGetAverageRank() {
String name = "Susan";
String gender = "F";
System.out.println("Average rank for " + name + " is " + getAverageRank(name, gender));
name = "Robert";
gender = "M";
System.out.println("Average rank for " + name + " is " + getAverageRank(name, gender));
}
public int getTotalBirthsRankedHigher(int year, String name, String gender) {
FileResource fr = new FileResource("D:/Coursera/Problem Solving with java/CSV FILE/us_babynames_by_year/yob" + year + ".csv" );
int rank = getRank(year, name, gender);
int totalBirths = 0;
int currentRank = 0;
for (CSVRecord record : fr.getCSVParser(false)) {
String currentGender = record.get(1);
if (currentGender.equals(gender)) {
currentRank++;
if (currentRank < rank) {
int currentBirths = Integer.parseInt(record.get(2));
totalBirths += currentBirths;
} else {
break;
}
}
}
return totalBirths;
}
public void testGetTotalBirthsRankedHigher() {
int year = 1990;
String name = "Drew";
String gender = "M";
System.out.println("Total births higher than " + name + " is " + getTotalBirthsRankedHigher(year, name, gender));
}
}