Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that prompts the user to enter a year in the range [2001, 2010],

ID: 3593923 • Letter: W

Question

Write a program that prompts the user to enter a year in the range [2001, 2010], a gender and a name, then displays the ranking of the name for the selected year and gender.

Technical requirements for this problem:

(a) The data for this project is to be read from by the program directly from the following URLs (DON’T store these files locally): http://www.cs.armstrong.edu/liang/data/babynamesranking2001.txt http://www.cs.armstrong.edu/liang/data/babynamesranking2002.txt http://www.cs.armstrong.edu/liang/data/babynamesranking2003.txt http://www.cs.armstrong.edu/liang/data/babynamesranking2004.txt http://www.cs.armstrong.edu/liang/data/babynamesranking2005.txt http://www.cs.armstrong.edu/liang/data/babynamesranking2006.txt http://www.cs.armstrong.edu/liang/data/babynamesranking2007.txt http://www.cs.armstrong.edu/liang/data/babynamesranking2008.txt http://www.cs.armstrong.edu/liang/data/babynamesranking2009.txt http://www.cs.armstrong.edu/liang/data/babynamesranking2010.txt

b) The data files consist of lines of data, each containing a ranking (an integer), a boy's name (a string), the number of boy's that were given that name (an integer), a girl's name (a string) and the number of girls that where given that name (an integer). Values are separated by spaces and/or tabs, which java.util.Scanner will use as separators by default.. Here is an example of the first 10 lines of one of the files (babynamesranking2002.txt):

1 Jacob 30541 Emily 24450

2 Michael 28220 Madison 21771

3 Joshua 25965 Hannah 18802

4 Matthew 25142 Emma 16520

5 Ethan 22099 Alexis 15629

6 Andrew 21996 Ashley 15335

7 Joseph 21872 Abigail 15292

8 Christopher 21665 Sarah 14741

9 Nicholas 21380 Samantha 14652

10 Daniel 21291 Olivia 14627

c) Using java.util.Scanner to read from a URL: Create a java.net.URL instance object with the URL string, then pass the return of the URL method openStream() to the constructor of Scanner. For example:

try (java.util.Scanner input

= new java.util.Scanner(new java.net.URL(

"http://www.cs.armstrong.edu/liang/data/babynamesranking2010.txt"

).openStream())) {

d) The data is to be stored in two arrays of maps, one for boy’s names and one for girl’s names. Each array must have one element for each of the 10 years of data. Each element in the array is a map (a class from the Java API that implements interface Map) that stores key / value pairs, each pair consisting of a name and its ranking, with the name serving as the key.

Explanation / Answer

Below is your program

BabyNamesRanking.java

import java.util.*;

public class BabyNamesRanking {

protected static Map[] boys = new Map[10];

protected static Map[] girls = new Map[10];

public static void main(String[] args) {

// Create a Scanner

Scanner input = new Scanner(System.in);

getNames(); // Store data

boolean inquiry;

do {

// Prompt the user to enter year, gender and name

System.out.print("Enter the year: ");

String year = input.nextLine();

System.out.print("Enter the gender: ");

String gender = input.nextLine();

System.out.print("Enter the name: ");

String name = input.nextLine();

// Display the ranking for the name

System.out.println(getGender(gender.toUpperCase()) + " name " + name + " is ranked #"

+ getRank(year, gender.toUpperCase(), name) + " in year " + year);

// Prompt the user to enter another

// inquiry or exit the program

System.out.print("Enter another inquiry? ");

inquiry = input.nextLine().toUpperCase().equals("Y");

} while (inquiry);

}

/* Returns the specified gender */

private static String getGender(String gender) {

return gender.equals("M") ? "Boy" : "Girl";

}

/** Stores names and ranks in the arrays */

private static void getNames() {

for (int year = 2001, i = 0; year <= 2010 && i < 10; year++, i++) {

// Create maps for boy and girl names

Map<String, String> mapBoys = new HashMap<>();

Map<String, String> mapGirls = new HashMap<>();

// Read data from url

try {

java.net.URL url = new java.net.URL(

"http://www.cs.armstrong.edu/liang/data/babynamesranking" + year + ".txt");

// Create input file from url

Scanner input = new Scanner(url.openStream());

while (input.hasNext()) {

String rank = input.next();

mapBoys.put(input.next(), rank);

input.next();

mapGirls.put(input.next(), rank);

input.next();

}

} catch (java.net.MalformedURLException ex) {

System.out.println("Invalid URL");

} catch (java.io.IOException ex) {

System.out.println("I/0 Errors: no such file");

}

// Add maps to arrays

boys[i] = mapBoys;

girls[i] = mapGirls;

}

}

/** Returns the ranking for the name of the specified year and gender */

private static String getRank(String yr, String gender, String name) {

int year = Integer.parseInt(yr) - 2001;

if (gender.charAt(0) == 'M') {

return boys[year].get(name) + "";

} else

return girls[year].get(name) + "";

}

}

Output

Enter the year: 2008
Enter the gender: F
Enter the name: Emma
Girl name Emma is ranked #1 in year 2008
Enter another inquiry? y
Enter the year: 2008
Enter the gender: F
Enter the name: Sofia
Girl name Sofia is ranked #36 in year 2008
Enter another inquiry? n