Skip to content

Commit 0c22cd6

Browse files
committed
add week4 solution
1 parent 6775b5f commit 0c22cd6

File tree

159 files changed

+2267141
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

159 files changed

+2267141
-0
lines changed

Week4/BabyNames/Assignment.java

+224
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
2+
/**
3+
* Write a description of Assignment here.
4+
*
5+
* @author Deny Kiantono
6+
* @version 1.0
7+
*/
8+
9+
import edu.duke.*;
10+
import org.apache.commons.csv.*;
11+
import java.io.*;
12+
13+
public class Assignment {
14+
public void totalBirths(FileResource fr) {
15+
int totalBirths = 0;
16+
int boyTotalBirths = 0;
17+
int girlTotalBirths = 0;
18+
int boyCount = 0;
19+
int girlCount = 0;
20+
21+
for (CSVRecord record : fr.getCSVParser(false)) {
22+
int numBorn = Integer.parseInt(record.get(2));
23+
totalBirths += numBorn;
24+
25+
if (record.get(1).equals("M")) {
26+
boyTotalBirths += numBorn;
27+
boyCount++;
28+
} else {
29+
girlTotalBirths += numBorn;
30+
girlCount++;
31+
}
32+
}
33+
34+
System.out.println("Total data = " + (boyCount + girlCount));
35+
System.out.println("Total births = " + totalBirths);
36+
37+
System.out.println("Total girls = " + girlCount);
38+
System.out.println("Total girls births = " + girlTotalBirths);
39+
40+
System.out.println("Total boys = " + boyCount);
41+
System.out.println("Total boys births = " + boyTotalBirths);
42+
}
43+
44+
public void testTotalBirths() {
45+
FileResource fr = new FileResource();
46+
totalBirths(fr);
47+
}
48+
49+
public int getRank(int year, String name, String gender) {
50+
FileResource fr = new FileResource("us_babynames/us_babynames_test/yob" + year + "short.csv" );
51+
int rank = 0;
52+
boolean recordFound = false;
53+
54+
for (CSVRecord record : fr.getCSVParser(false)) {
55+
String currentName = record.get(0);
56+
String currentGender = record.get(1);
57+
58+
if (currentGender.equals(gender)) {
59+
rank++;
60+
61+
if (currentName.equals(name)) {
62+
recordFound = true;
63+
break;
64+
}
65+
}
66+
}
67+
68+
if (recordFound) {
69+
return rank;
70+
} else {
71+
return -1;
72+
}
73+
}
74+
75+
public void testGetRank() {
76+
int year = 2012;
77+
String name = "Mason";
78+
String gender = "M";
79+
80+
int rank = getRank(year, name, gender);
81+
System.out.println(name + " rank is " + rank);
82+
}
83+
84+
public String getName(int year, int rank, String gender) {
85+
FileResource fr = new FileResource("us_babynames/us_babynames_test/yob" + year + "short.csv" );
86+
String name = "NO NAME";
87+
int currentRank = 0;
88+
89+
for (CSVRecord record : fr.getCSVParser(false)) {
90+
String currentGender = record.get(1);
91+
92+
if (currentGender.equals(gender)) {
93+
currentRank++;
94+
95+
if (currentRank == rank) {
96+
name = record.get(0);
97+
break;
98+
}
99+
}
100+
}
101+
102+
return name;
103+
}
104+
105+
public void testGetName() {
106+
int year = 2012;
107+
int rank = 2;
108+
String gender = "M";
109+
110+
String name = getName(year, rank, gender);
111+
System.out.println(year + " rank " + rank + " is " + name);
112+
}
113+
114+
public void whatIsNameInYear(String name, int year, int newYear, String gender) {
115+
int currentYearRank = getRank(year, name, gender);
116+
String newName = getName(newYear, currentYearRank, gender);
117+
118+
System.out.println(name + " born in " + newYear + " would be " + newName + " if she was born in " + newYear);
119+
}
120+
121+
public void testWhatIsNameInYear() {
122+
whatIsNameInYear("Isabella", 2012, 2014, "F");
123+
}
124+
125+
public int yearOfHighestRank(String name, String gender) {
126+
int year = Integer.MIN_VALUE;
127+
int rank = Integer.MAX_VALUE;
128+
DirectoryResource dr = new DirectoryResource();
129+
130+
for (File f : dr.selectedFiles()) {
131+
int currentYear = Integer.parseInt(f.getName().substring(3, 7));
132+
int currentRank = getRank(currentYear, name, gender);
133+
134+
if (currentRank != -1 && currentRank < rank) {
135+
rank = currentRank;
136+
year = currentYear;
137+
}
138+
139+
}
140+
141+
if (year == Integer.MIN_VALUE) {
142+
return -1;
143+
} else {
144+
return year;
145+
}
146+
}
147+
148+
public void testYearOfHighestRank() {
149+
String name = "Mason";
150+
String gender = "M";
151+
System.out.println(name + " most popular year is " + yearOfHighestRank(name, gender));
152+
}
153+
154+
public double getAverageRank(String name, String gender) {
155+
double totalRank = 0;
156+
int recordCount = 0;
157+
DirectoryResource dr = new DirectoryResource();
158+
159+
for (File f : dr.selectedFiles()) {
160+
int currentYear = Integer.parseInt(f.getName().substring(3, 7));
161+
int currentRank = getRank(currentYear, name, gender);
162+
163+
if (currentRank != -1) {
164+
totalRank += currentRank;
165+
recordCount++;
166+
}
167+
}
168+
169+
if (recordCount == 0) {
170+
return -1.0;
171+
} else {
172+
return totalRank / recordCount;
173+
}
174+
}
175+
176+
public void testGetAverageRank() {
177+
String name = "Mason";
178+
String gender = "M";
179+
System.out.println("Average rank for " + name + " is " + getAverageRank(name, gender));
180+
181+
name = "Jacob";
182+
gender = "M";
183+
System.out.println("Average rank for " + name + " is " + getAverageRank(name, gender));
184+
}
185+
186+
public int getTotalBirthsRankedHigher(int year, String name, String gender) {
187+
FileResource fr = new FileResource("us_babynames/us_babynames_test/yob" + year + "short.csv" );
188+
int rank = getRank(year, name, gender);
189+
int totalBirths = 0;
190+
int currentRank = 0;
191+
192+
for (CSVRecord record : fr.getCSVParser(false)) {
193+
String currentGender = record.get(1);
194+
195+
if (currentGender.equals(gender)) {
196+
currentRank++;
197+
198+
if (currentRank < rank) {
199+
int currentBirths = Integer.parseInt(record.get(2));
200+
totalBirths += currentBirths;
201+
} else {
202+
break;
203+
}
204+
}
205+
}
206+
207+
return totalBirths;
208+
}
209+
210+
public void testGetTotalBirthsRankedHigher() {
211+
int year = 2012;
212+
String name = "Ethan";
213+
String gender = "M";
214+
System.out.println("Total births higher than " + name + " is " + getTotalBirthsRankedHigher(year, name, gender));
215+
}
216+
217+
218+
219+
220+
221+
222+
223+
224+
}

0 commit comments

Comments
 (0)