Baby name popularity ranking The popularity ranking of baby

(Baby name popularity ranking) The popularity ranking of baby names from year 2006 to 2015 can be downloaded from www.ssa.gov/oact/babynames and stored in files named babynamerank2006.txt, babynamerank2007.txt, …… babynamerank2015.txt. Each file contains 1000 lines. Each line contains a ranking, a boy’s name, number for the boy’s name, a girl name and number for the girl’s name. For example, the first two lines in the file babynameranking2010.txt are as follows:
1 Jacob 21,875 Isabella 22,731
2 Ethan 17,866 Sophia 20477
So the boy’s name Jacob and girl’s name Isabella are ranked #1 and the boy’s name Ethan and the girl’s name Sophia are ranked #2. 21875 boys are names Jacob and 22,731 girls are names Isabella in the year 2010.
Write a program that enables the user to select a year, gender, and enter a name to display the ranking of the name for the selected year and gender, as shown in the following figure. Prompt the user to enter another inquiry or exit the program. To achieve the best efficiency, create two arrays for boy’s names and girl’s names, respectively. Each array has 10 elements for 10 years. Each element is a map that stores a name and its ranking in a pair with the name as the key. Here is a sample run.

Enter the year: 2010 - Enter Enter the gender: M - Enter Enter the name: Javier - Enter Boy name Javier is ranked #190 in year 2010 Enter another inquiry? Y HEnter Enter the year: 2001 -Enter Enter the gender: F - Enter Enter the name: Emily - Enter Girl name Emily is ranked #1 in year 2001 Enter another inquiry? N Enter

Solution

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import java.util.*;
public class Gender extends Application
{
final int BOYS_NAMES = 1;
final int GIRLS_NAMES = 3;
protected ComboBox<String> cboYear = new ComboBox<>();
protected ComboBox<String> cboGender = new ComboBox<>();
protected TextField tfName = new TextField();
protected Button btFindRanking = new Button(\"Find Ranking\");
protected Label lblResults = new Label(\"\");
protected Map[] boys = getNames(BOYS_NAMES);
protected Map[] girls = getNames(GIRLS_NAMES);
public void start(Stage primaryStage)
{
btFindRanking.setOnAction(e -> displayRank());
Scene scene = new Scene(getPane(), 300, 160);
primaryStage.setTitle(\"Gender\");
primaryStage.setScene(scene);
primaryStage.show();
}
private void displayRank()
{
lblResults.setText(getGender() + \" name \"+ tfName.getText() + \" is ranked #\" + getRank()+ \" in year \" + cboYear.getValue());
}
private String getGender()
{
return cboGender.getValue().equals(\"Male\") ? \"Boy\" : \"Girl\";
}
private String getRank()
{
int year = Integer.parseInt(cboYear.getValue()) - 2001;
if (cboGender.getValue().equals(\"Male\"))
{
return boys[year].get(tfName.getText()) + \"\";
}
else
return girls[year].get(tfName.getText()) + \"\";
}
private BorderPane getPane()
{
for (int i = 2001; i <= 2010; i++)
cboYear.getItems().add(i + \"\");
cboGender.getItems().addAll(\"Male\", \"Female\");
GridPane gridPane = new GridPane();
gridPane.setVgap(5);
gridPane.setPadding(new Insets(5, 0, 5, 0));
gridPane.setAlignment(Pos.CENTER);
gridPane.add(new Label(\"Select a year: \"), 0, 0);
gridPane.add(cboYear, 1, 0);
gridPane.add(new Label(\"Boy or girl?: \"), 0, 1);
gridPane.add(cboGender, 1, 1);
gridPane.add(new Label(\"Enter a name: \"), 0, 2);
gridPane.add(tfName, 1, 2);
gridPane.add(btFindRanking, 1, 3);
BorderPane pane = new BorderPane();
pane.setCenter(gridPane);
pane.setBottom(lblResults);
pane.setAlignment(lblResults, Pos.CENTER);
return pane;
}
private Map[] getNames(int gender)
{
Map[] array = new Map[10];
for (int year = 2001, i = 0; year <= 2010 && i < 10; year++, i++)
{
Map<String, String> map = new HashMap<>();
try
{
java.net.URL url = new java.net.URL(\"http://www.\"+ year + \".txt\");
Scanner input = new Scanner(url.openStream());
while (input.hasNext())
{
ArrayList<String> list = new ArrayList<>();
for (int w = 0; w < 5; w++)
{
list.add(w, input.next());
}
map.put(list.get(gender), list.get(0));
}
}
catch (java.net.MalformedURLException ex)
{
System.out.println(\"Invalid URL\");
}
catch (java.io.IOException ex)
{
System.out.println(\"I/0 Errors: no such file\");
}
array[i] = map;
}
return array;
}
}

(Baby name popularity ranking) The popularity ranking of baby names from year 2006 to 2015 can be downloaded from www.ssa.gov/oact/babynames and stored in files
(Baby name popularity ranking) The popularity ranking of baby names from year 2006 to 2015 can be downloaded from www.ssa.gov/oact/babynames and stored in files
(Baby name popularity ranking) The popularity ranking of baby names from year 2006 to 2015 can be downloaded from www.ssa.gov/oact/babynames and stored in files

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site