Skip to content

Commit ff04d3f

Browse files
authored
Add files via upload
1 parent 1ecdcec commit ff04d3f

File tree

5 files changed

+260
-0
lines changed

5 files changed

+260
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="module-library" exported="">
11+
<library>
12+
<CLASSES>
13+
<root url="jar://$MODULE_DIR$/../../lib/courserajava.jar!/" />
14+
</CLASSES>
15+
<JAVADOC />
16+
<SOURCES>
17+
<root url="jar://$MODULE_DIR$/../../lib/courserajava.jar!/" />
18+
</SOURCES>
19+
</library>
20+
</orderEntry>
21+
<orderEntry type="module-library" exported="">
22+
<library>
23+
<CLASSES>
24+
<root url="jar://$MODULE_DIR$/../../lib/apache-csv.jar!/" />
25+
</CLASSES>
26+
<JAVADOC />
27+
<SOURCES>
28+
<root url="jar://$MODULE_DIR$/../../lib/apache-csv.jar!/" />
29+
</SOURCES>
30+
</library>
31+
</orderEntry>
32+
</component>
33+
</module>
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
/**
2+
* Final Project - Analyzing baby names data from 1880 to 2014
3+
* @author: salimt
4+
*/
5+
6+
import edu.duke.DirectoryResource;
7+
import edu.duke.FileResource;
8+
import org.apache.commons.csv.CSVRecord;
9+
10+
import java.io.File;
11+
12+
public class babyNames {
13+
public static void main(String[] args) {
14+
15+
// testTotalBirths();
16+
// testGetRank();
17+
// testGetName();
18+
// testWhatIsNameInYear();
19+
// testYearOfHighestRank();
20+
// testGetAverageRank();
21+
// testGetTotalBirthsRankedHigher();
22+
23+
//----QUIZ Questions----\\
24+
// System.out.println(getRank(1960, "Emily", "F")); //251
25+
// System.out.println(getRank(1971, "Frank", "M")); //54
26+
// System.out.println(getName(1980, 350, "F")); //Mia
27+
// System.out.println(getName(1982, 450, "M")); //Forrest
28+
// whatIsNameInYear("Susan", 1972, 2014, "F"); //Addison
29+
// whatIsNameInYear("Owen", 1974, 2014, "M"); //Leonel
30+
// System.out.println(yearOfHighestRank("Genevieve", "F")); //1914
31+
// System.out.println(yearOfHighestRank("Mich", "M")); //1960
32+
// System.out.println(getAverageRank("Susan", "F")); //173.51
33+
// System.out.println(getAverageRank("Robert", "M")); //10.75
34+
// System.out.println(getTotalBirthsRankedHigher(1990, "Emily", "F")); //323200
35+
// System.out.println(getTotalBirthsRankedHigher(1990, "Drew", "M" )); //1498074
36+
37+
}
38+
39+
//returns total birth statistics of the given year/file
40+
public static void totalBirths(FileResource fr){
41+
int totalBirths = 0;
42+
int totalBoys = 0, totalGirls = 0;
43+
int boyNames = 0, girlNames = 0;
44+
45+
for(CSVRecord record: fr.getCSVParser(false)){
46+
int numBorn = Integer.parseInt(record.get(2)); //gets the number of babies born with the name
47+
totalBirths += numBorn;
48+
if(record.get(1).equals("M")){
49+
totalBoys += numBorn;
50+
boyNames++;
51+
}else{
52+
totalGirls += numBorn;
53+
girlNames++;
54+
}
55+
}
56+
System.out.println("Total births: " + totalBirths + " Total names: " + boyNames+girlNames);
57+
System.out.println("Total boys: " + totalBoys + " Total boy names: " + boyNames);
58+
System.out.println("Total girls: " + totalGirls + " Total girl names: " + girlNames);
59+
}
60+
61+
//TEST method for totalBirths
62+
public static void testTotalBirths(){
63+
FileResource fr = new FileResource();
64+
totalBirths(fr);
65+
}
66+
67+
//method returns the rank of the name in the file for the given gender,
68+
//where rank 1 is the name with the largest number of births. If the name is not in the file, then -1 is returned
69+
public static int getRank(Integer year, String name, String gender){
70+
FileResource fr = new FileResource("yob" + year + ".csv");
71+
int rank = 0;
72+
73+
for(CSVRecord rec: fr.getCSVParser(false)){
74+
if(rec.get(1).equals(gender)){ rank++; }
75+
if(rec.get(0).equals(name) && rec.get(1).equals(gender)){
76+
return rank;
77+
}
78+
}
79+
return -1;
80+
}
81+
82+
//TEST method for getRank
83+
public static void testGetRank(){
84+
System.out.println(getRank(2012, "Mason", "M")); //2
85+
System.out.println(getRank(2012, "Mason", "F")); //-1
86+
System.out.println(getRank(2012, "Ava", "F")); //5
87+
}
88+
89+
//method returns the name of the person in the file at this rank, for the given gender,
90+
//where rank 1 is the name with the largest number of births. If the rank does not exist in the file,
91+
//then “NO NAME” is returned.
92+
public static String getName(Integer year, Integer rank, String gender){
93+
FileResource fr = new FileResource("yob" + year + ".csv");
94+
int nameRank = 0;
95+
96+
for(CSVRecord rec: fr.getCSVParser(false)){
97+
if(rec.get(1).equals(gender)){ nameRank++; }
98+
99+
if(nameRank == rank && rec.get(1).equals(gender)){
100+
return rec.get(0);
101+
}
102+
}
103+
return "NO NAME";
104+
}
105+
106+
//TEST method for getName
107+
public static void testGetName(){
108+
System.out.println(getName(2012, 2, "M")); //Mason
109+
System.out.println(getName(2012, 5, "M")); //NO NAME
110+
System.out.println(getName(2012, 5, "F")); //Ava
111+
}
112+
113+
//method determines what name would have been named if they were born in a
114+
//different year, based on the same popularity. That is, you should determine the rank of name
115+
//in the year they were born, and then print the name born in newYear that is at the same rank and same gender.
116+
public static void whatIsNameInYear(String name, Integer year, Integer newYear, String gender){
117+
int oldRank = getRank(year, name, gender);
118+
String newName = getName(newYear, oldRank, gender);
119+
120+
System.out.println(name + "born in " + year + " would be " + newName + " if he/she was born in " + newYear);
121+
}
122+
123+
//TEST method for whatIsNameInYear
124+
public static void testWhatIsNameInYear(){
125+
whatIsNameInYear("Isabella", 2012, 2014, "F"); //Sophia
126+
whatIsNameInYear("Noah", 2013, 2012, "M"); //Jacob
127+
}
128+
129+
//method selects a range of files to process and returns an integer, the year with the
130+
//highest rank for the name and gender.
131+
//If the name and gender are not in any of the selected files, it should return -1
132+
public static int yearOfHighestRank(String name, String gender){
133+
DirectoryResource dr = new DirectoryResource();
134+
int highestRank = 0;
135+
int highestYear = -1;
136+
137+
for(File f: dr.selectedFiles()){
138+
int currYear = Integer.parseInt(f.getName().replaceAll("[^\\d]", ""));
139+
int currRank = getRank(currYear, name, gender);
140+
141+
if(highestRank == 0 && currRank != -1){
142+
highestRank = currRank;
143+
highestYear = currYear;
144+
}
145+
if(currRank < highestRank && currRank != -1){
146+
highestRank = currRank;
147+
highestYear = currYear;
148+
}
149+
}
150+
return highestYear;
151+
}
152+
153+
//TEST method for yearOfHighestRank
154+
public static void testYearOfHighestRank(){
155+
System.out.println(yearOfHighestRank("Isabella", "F")); //2012
156+
System.out.println(yearOfHighestRank("Lee", "M")); //-1
157+
System.out.println(yearOfHighestRank("Emma", "F")); //2014
158+
System.out.println(yearOfHighestRank("Emma", "M")); //-1
159+
}
160+
161+
//method selects a range of files to process and returns a double representing the average rank
162+
//of the name and gender over the selected files. It should return -1.0 if the name is not ranked
163+
//in any of the selected files
164+
public static double getAverageRank(String name, String gender){
165+
DirectoryResource dr = new DirectoryResource();
166+
int totalRank = 0, countedYears = 0;
167+
168+
for(File f: dr.selectedFiles()){
169+
int currYear = Integer.parseInt(f.getName().replaceAll("[^\\d]", ""));
170+
int currRank = getRank(currYear, name, gender);
171+
172+
if(currRank != -1){
173+
totalRank += currRank;
174+
countedYears++;
175+
}
176+
}
177+
double result = (double)totalRank/countedYears;
178+
if(result > 0){ return result; }
179+
return -1;
180+
}
181+
182+
//TEST method for getAverageRank
183+
public static void testGetAverageRank(){
184+
System.out.println(getAverageRank("Sophia", "F")); //1.66
185+
System.out.println(getAverageRank("Mason", "M")); //3.0
186+
System.out.println(getAverageRank("Jacob", "M")); //2.66
187+
System.out.println(getAverageRank("Lee", "M")); //-1
188+
System.out.println(getAverageRank("Jacob", "F")); //-1
189+
}
190+
191+
//method returns an integer, the total number of births of those names with the same gender
192+
//and same year who are ranked higher than name
193+
public static int getTotalBirthsRankedHigher(Integer year, String name, String gender){
194+
FileResource fr = new FileResource("yob" + year + ".csv");
195+
int births = 0;
196+
int personsRank = getRank(year, name, gender); //given persons rank in the given year
197+
198+
for(CSVRecord rec: fr.getCSVParser(false)){
199+
if(rec.get(1).equals(gender)){
200+
if(personsRank > getRank(year, rec.get(0), gender)){
201+
births += Integer.parseInt(rec.get(2));
202+
}
203+
}
204+
}
205+
return births;
206+
}
207+
208+
//TEST method for getTotalBirthsRankedHigher
209+
public static void testGetTotalBirthsRankedHigher(){
210+
System.out.println(getTotalBirthsRankedHigher(2012, "Ethan", "M")); //15
211+
System.out.println(getTotalBirthsRankedHigher(2012, "Emma", "F")); //10
212+
System.out.println(getTotalBirthsRankedHigher(2012, "Isabella", "F")); //19
213+
System.out.println(getTotalBirthsRankedHigher(2012, "Sophia", "F")); //0
214+
System.out.println(getTotalBirthsRankedHigher(2012, "Lee", "M")); //0
215+
}
216+
217+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Emma,F,500
2+
Olivia,F,400
3+
Sophia,F,300
4+
Isabella,F,200
5+
Ava,F,100
6+
Noah,M,100
7+
Liam,M,40
8+
Mason,M,30
9+
Jacob,M,20
10+
William,M,10

0 commit comments

Comments
 (0)