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

CIS 201 Assignments: Assignment 10 Babv Names Description This assignment will g

ID: 3692362 • Letter: C

Question

CIS 201 Assignments: Assignment 10 Babv Names Description This assignment will give you practice with file processing. Turn in a file named BabyNames.java. Be sure you get the file names.txt that is below this assignment description on Moodle. Every 10 years, the Social Security Administration gives data about the 1000 most popular boy and girl names for children born in the US. Your task in this program is to prompt the user for a name, and thern to display popularity statistics about that name for each decade since 1900 Your program is to give an introduction and then prompt the user for a name to display. Then it will read through a data file searching for that name. If the name is found in the file, you should print the name and the statistics about that name's popularity in each decade. Your program will read its data from a file named names.txt. Each line of this file has a name, followed by the popularity rank of that name in 1900, 1910, 1920, and so on. The default input file has 11 numbers per line, meaning that the last number represents the ranking in the year 2000. A rank of 1 was the most popular name that year, while a rank of 999 was not very popular. A rank of 0 means the name did not appear in the top 1000 that year at all. Here is a sample of the data: Lionel 387 344 369 333 399 386 408 553 492 829 972 Lisa 0 0 0 0 464 38 1 6 31 113 298 Lise 0 0 0 0 0 997 0 0 0 0 0 Lisette 0 0 0 0 0 0 0 816 958 0 864 "Lionel" was #387 in 1900 and is slowly decreasing. "Lisa" made the list in 1940 and peaked in 1960 at #1 If the name is found in the file, you should output the name's rank, decade by decade, at the console. If the name is not found in the file, you should simply output that it was not found. The following is a couple of example interactions with this program: hallersm@ma rcy. ~$ ava BabyNames This program graphs the popularity of a name in Social Security baby name statistiCS recorded since the vear 1900 Type a name: zOIDberG "zOIDberG" not found. hallersm@marcyjavaBabyNames This program graphs the popularity of a name in Social Security baby name statistics recorded since the year 1900 Type a name: francine Statistics on name "Francine" 1900: 0 1910: 0 1920: 884 1930: 475 1940: 278 1950: 269 1960: 368

Explanation / Answer

import java.io.*;
import java.util.*;
public class HelloWorld
{
public static void main(String []args)
{
try{
FileInputStream fstream = new FileInputStream("names.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine,name;
int status = 0;
Scanner scanner = new Scanner(System.in);
System.out.print("Type a name: ");
name=(scanner.nextLine());
while ((strLine = br.readLine()) != null)   
{
int i=1900,j=1;
String names[] = strLine.split("\s+");
if(names[0].toString().toLowerCase().equals(name.toLowerCase()))
{
status =1;
System.out.println(" Statistics on name "" + name + """);
while(names[j] != null)
{
System.out.println(" " + i + ": " + names[j]);
i = i+10;
j=j+1;
}
}
}
if(status == 0)
System.out.println(name + " not found.");
in.close();
}
catch (Exception e)
{
//System.err.println("Error: " + e.getMessage());
}
}
}