In: Computer Science
Step 4: Create a class called BabyNamesDatabase
This class maintains an ArrayList of BabyName objects.
Instance Variables
Constructor
Mutator Methods
Background Information: Reading from a Text File
The data file contains baby name records from the U.S. Social Security Administration since 1880. The readBabyNameData() method reads four items that are separated by commas, passes them to the BabyName class constructor and adds the BabyName object to the ArrayList.
Sample record: Mary,F,7065,1880
public void readBabyNameData(String filename){
// Read the full set of data from a text file
try{
// open the text file and use a Scanner to read the text
FileInputStream fileByteStream = new FileInputStream(filename);
Scanner scnr = new Scanner(fileByteStream);
scnr.useDelimiter("[,\r\n]+");
// keep reading as long as there is more data
while(scnr.hasNext()) {
// reads each element of the record
String name = scnr.next();
String gender = scnr.next();
// TO DO: read the count and year
int count = ;
int year = ;
// TO DO: assign true/false to boolean isFemale based on
// the gender String
boolean isFemale;
// instantiates an object of the BabyName class
BabyName babyName = new BabyName(name, isFemale, count, year);
// TO DO: add to the ArrayList the babyName created above
}
fileByteStream.close();
}
catch(IOException e) {
System.out.println("Failed to read the data file: " + filename);
}
}
Accessor Methods
Step 5: Generate a Top Ten List
To generate a top ten list for a particular year your solution must be able to sort names in descending order by number of births. This requires changes to the BabyName and BabyNamesDatabase classes.
Changes to BabyName class
public class BabyName implements Comparable{
IMPORTANT NOTE: For the compareTo method below, we are assuming that the name of the instance variable for the number of births is count.
public int compareTo(Object other){
BabyName b = (BabyName) other;
return (b.count – count);
}
Changes to BabyNamesDatabase class
NOTE: Sorting an ArrayList, called tempList, can be performed with one line of code:
Collections.sort(tempList);
Once you have the list of all names in the year sorted in descending order by the count of births, you can do any of these three options to figure out the top ten baby names for that year
BABY NAME JAVA CODE
package pranam; // imported some imaginary package
import java.text.DecimalFormat; // imported Decimal Format
public class Babyname { // create class
Babyname
private static final String girls = null; // create
string of girls (constant
private static final String named = null;
private static final String in = null;
String name;
boolean gender;
int number_of_babies_given_that_name;
int birth_year;
public Babyname(String name, boolean gender,
int
number_of_babies_given_that_name, int birth_year) { //defined a
constructor
super();
this.name = name;
this.gender = gender;
this.number_of_babies_given_that_name =
number_of_babies_given_that_name;
this.birth_year = birth_year;
}
public String getName() { // Getters
and setters for the functions
return name;
}
public void setName(String name)
{
this.name = name;
}
public boolean isGender() {
return gender;
}
public void setGender(boolean gender)
{
this.gender = gender;
}
public int
getNumber_of_babies_given_that_name() {
return
number_of_babies_given_that_name;
}
public void
setNumber_of_babies_given_that_name(
int
number_of_babies_given_that_name) {
this.number_of_babies_given_that_name =
number_of_babies_given_that_name;
}
public int getBirth_year() {
return birth_year;
}
public void setBirth_year(int
birth_year) {
this.birth_year = birth_year;
}
public boolean isFemale(){ // generate the isFemale
function
if(gender=true)
return
true;
else
return
false;
}
@Override
public String toString() { // Generate the toString
function
DecimalFormat fmt = new
DecimalFormat ("###,###,###");
return " ["
+ number_of_babies_given_that_name + " " +
"girls" +" "
+ "named" +" "+ name +" "+ "in" + ","+
birth_year + "]";
}
}
=============================
JAVA PROGRAM
=============================
//////BabyName.JAVA
package PRANUM;
import java.text.DecimalFormat; // imported Decimal Format
//Class:BabyName
public class BabyName implements Comparable{ // create class
Babyname
private static final String girls = null; // create string of girls
(constant
private static final String named = null;
private static final String in = null;
String name;
boolean gender;
int number_of_babies_given_that_name;
int birth_year;
public BabyName(String name, boolean gender,
int number_of_babies_given_that_name, int birth_year) { //defined a
constructor
super();
this.name = name;
this.gender = gender;
this.number_of_babies_given_that_name =
number_of_babies_given_that_name;
this.birth_year = birth_year;
}
public String getName() { // Getters and setters for the
functions
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isGender() {
return gender;
}
public void setGender(boolean gender) {
this.gender = gender;
}
public int getNumber_of_babies_given_that_name() {
return number_of_babies_given_that_name;
}
public void setNumber_of_babies_given_that_name(
int number_of_babies_given_that_name) {
this.number_of_babies_given_that_name =
number_of_babies_given_that_name;
}
public int getBirth_year() {
return birth_year;
}
public void setBirth_year(int birth_year) {
this.birth_year = birth_year;
}
public boolean isFemale(){ // generate the isFemale function
if(gender=true)
return true;
else
return false;
}
@Override
public String toString() { // Generate the toString function
DecimalFormat fmt = new DecimalFormat ("###,###,###");
return " ["
+ number_of_babies_given_that_name + " " + (gender?"girls":"boys")
+" "
+ "named" +" "+ name +" "+ "in" + ","+ birth_year + "]";
}
@Override
public int compareTo(Object other) {
BabyName b = (BabyName) other;
int value =b.number_of_babies_given_that_name -
this.number_of_babies_given_that_name;
return value;
}
}
/////////////BabyNamesDatabase.JAVA
package PRANUM;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class BabyNamesDatabase {
private ArrayList<BabyName> babyNames =
null;
public BabyNamesDatabase (){
babyNames = new
ArrayList<BabyName>();
}
/**
* return the number of baby names
* @return
*/
public int countAllNames(){
return babyNames.size();
}
/**
* count the total number of girls born since
1880.
* This number will be much higher than just the number
of girl names since
* each name represents hundreds, if not thousands, of
newborn babies
* @return
*/
public int countAllGirls(){
int count = 0;
for(BabyName bn: babyNames){
if(bn.getBirth_year() >=1880 && bn.gender ==
true){//gender will be true for female as per logic
count = count +
bn.number_of_babies_given_that_name;
}
}
return count;
}
/**
* count the total number of boys born since
1880.
* This number will be much higher than just the number
of girl names since
* each name represents hundreds, if not thousands, of
newborn babies
* @return
*/
public int countAllBoys(){
int count = 0;
for(BabyName bn: babyNames){
if(bn.getBirth_year() >=1880 && bn.gender ==
false){//gender will be false for male as per logic
count = count +
bn.number_of_babies_given_that_name;
}
}
return count;
}
/**
* The method will search the full list and return a
new list of baby names that match
* the requested year (in the parameter). If there is
no match for the year,
* the returned list will exist but have zero
elements.
* @param year
* @return
*/
public ArrayList<BabyName> searchForYear(int
year) {
ArrayList<BabyName>
newListOfNames = new ArrayList<BabyName>();
for(BabyName bn: babyNames){
if(bn.getBirth_year()==year){
newListOfNames.add(bn);
}
}
Collections.sort(newListOfNames);
return newListOfNames;
}
/**
* The method will navigate the list of baby names and
return the most popular girl name for that specific year.
* Return null if there are no baby names for the year
entered as input parameter.
* @param year
* @return
*/
public BabyName mostPopularGirl(int year){
//get all babynames for the year in
descending order of baby count
ArrayList<BabyName> allNames
= searchForYear(year);
BabyName theBabyName = null;
for(BabyName bn: allNames){
if(bn.gender==true){//the first girl found in the list will be most
popular as it has the highest count
theBabyName = bn;
break;
}
}
return theBabyName;
}
/**
* The method will navigate the list of baby names and
return the most popular boy name for that specific year.
* Return null if there are no baby names for the year
entered as input parameter.
* @param year
* @return
*/
public BabyName mostPopularBoy(int year){
//get all babynames for the year in
descending order of baby count
ArrayList<BabyName> allNames
= searchForYear(year);
BabyName theBabyName = null;
for(BabyName bn: allNames){
if(bn.gender==false){//the first boy found in the list will be most
popular as it has the highest count
theBabyName = bn;
break;
}
}
return theBabyName;
}
/**
* to search the full list and return a new list of
baby names that match
* the requested name (in the parameter). Spelling
should match exactly but it is not case sensitive.
* For example, ‘Angie’, ‘angie’ and ‘ANGIE’ all
match.
* If there are no matches for the name, the returned
list will exist but have zero elements.
* @param name
* @return
*/
public ArrayList <BabyName> searchForName(String
name){
ArrayList<BabyName>
newBabyNames = new ArrayList<BabyName>();
for(BabyName bn: babyNames){
if(bn.getName().equalsIgnoreCase(name)){
newBabyNames.add(bn);
}
}
return newBabyNames;
}
/**
* The method will search the full list and return a
new list of the ten most popular
* baby names in a given year entered as input
parameter. The returned list will exist but
* have zero elements if there are no records for the
specified year. Otherwise, it will have ten names.
* @param year
* @return
*/
public ArrayList <BabyName> topTenNames(int
year) {
ArrayList<BabyName> allNames
= searchForYear(year);
ArrayList<BabyName> top10List
= new ArrayList<BabyName>();
if(allNames!=null &&
!allNames.isEmpty()){
for(BabyName bn:
allNames){
top10List.add(bn);
if(top10List.size()==10)
break;
}
}
return top10List;
}
/**
* read and populate babyNames by reading from
file
* @param filename
*/
public void readBabyNameData(String filename){
// Read the full set of data from a text file
try{
// open the text file and use a Scanner to read the
text
FileInputStream fileByteStream = new
FileInputStream(filename);
Scanner scnr = new Scanner(fileByteStream);
scnr.useDelimiter("[,\r\n]+");
// keep reading as long as there is more data
while(scnr.hasNext()) {
// reads each element of the record
String name = scnr.next();
String gender = scnr.next();
// TO DO: read the count and year
int count = Integer.parseInt(scnr.next());
int year = Integer.parseInt(scnr.next());
// TO DO: assign true/false to boolean isFemale based
on
// the gender String
boolean isFemale =
gender.equals("F")?true:false;
// instantiates an object of the BabyName class
BabyName babyName = new BabyName(name, isFemale,
count, year);
// TO DO: add to the ArrayList the babyName created
above
babyNames.add(babyName);
}
scnr.close();//scanner is closed
fileByteStream.close();
}catch(IOException e) {
System.out.println("Failed to read the data file: " +
filename);
}
}
}
///////////////TestBabyNames.JAVA
package PRANUM;
import java.util.ArrayList;
//tHIS IS THE TEST CLASS
public class TestBabyNames {
public static void main(String[] args){
String filename =
"babynames.txt";
BabyNamesDatabase bnd = new
BabyNamesDatabase();
bnd.readBabyNameData(filename);
System.out.println("Total name
Count = "+ bnd.countAllNames());
System.out.println("Total Boy count
= "+bnd.countAllBoys());
System.out.println("Total Girl
count = "+bnd.countAllGirls());
System.out.println("Most popular
Girl for the year 1880: ");
System.out.println(bnd.mostPopularGirl(1880));
System.out.println("Most popular
Boy for the year 1880: ");
System.out.println(bnd.mostPopularBoy(1880));
System.out.println("All babies for
year 1880 are:");
ArrayList<BabyName> babies1 =
bnd.searchForYear(1880);
babies1.forEach(System.out::println);
System.out.println("Babies with
name Amy are:");
ArrayList<BabyName> babies2 =
bnd.searchForName("Amy");
babies2.forEach(System.out::println);
System.out.println("Top 10 babies
for the year 1881 are:");
ArrayList<BabyName> top10 =
bnd.topTenNames(1881);
top10.forEach(System.out::println);
}
}
================================================
INPUT DATA (babynames.txt)
================================================
Mary,F,7065,1880
AMY,F,3256,1880
Samuel,M,9812,1880
Pablo,M,2110,1880
Kim,F,1925,1880
Peter,M,5463,1881
Michael,M,4545,1881
Rebeca,F,7878,1881
Jenifer,F,4625,1881
Robert,M,6597,1881
Thomas,M,989,1881
Joseph,M,1589,1881
Mili,F,1369,1881
Elisa,F,1598,1881
John,M,9845,1881
Mark,M,4512,1881
Amy,F,7712,1881
Sujan,F,3216,1881
================================================
OUTPUT
=================================================
Total name Count = 18
Total Boy count = 45462
Total Girl count = 38644
Most popular Girl for the year 1880:
[7065 girls named Mary in,1880]
Most popular Boy for the year 1880:
[9812 boys named Samuel in,1880]
All babies for year 1880 are:
[9812 boys named Samuel in,1880]
[7065 girls named Mary in,1880]
[3256 girls named AMY in,1880]
[2110 boys named Pablo in,1880]
[1925 girls named Kim in,1880]
Babies with name Amy are:
[3256 girls named AMY in,1880]
[7712 girls named Amy in,1881]
Top 10 babies for the year 1881 are:
[9845 boys named John in,1881]
[7878 girls named Rebeca in,1881]
[7712 girls named Amy in,1881]
[6597 boys named Robert in,1881]
[5463 boys named Peter in,1881]
[4625 girls named Jenifer in,1881]
[4545 boys named Michael in,1881]
[4512 boys named Mark in,1881]
[3216 girls named Sujan in,1881]
[1598 girls named Elisa in,1881]