In Java write this program: https://www.ssa.gov/oact/babynames/limits.html - lin
ID: 3597053 • Letter: I
Question
In Java write this program:
https://www.ssa.gov/oact/babynames/limits.html - link to hyperlink!!
You will write a Java program that allows searches on name popularity by birth year (from 1880-2016). Here is a sample run of the program (user input is in red) Enter a birth year (1880-1996): 1996 Enter (n)ame, (r)ank, (t)op, or (qDuit:n Enter name: Taylor Taylor is ranked 6 for females Taylor is ranked 82 for males Enter (n)ame, (r)ank, (t)op, or (q)uit: n Enter name: Abc Abc is not ranked for females Abc is not ranked for males Enter (n)ame, (r)ank, (t)op, or (a)uit: r Enter rank:5 Samantha is the #5 female name Joshua is the #5 male nam Enter (n)ame, (r)ank, (t)op, or (q)uit: T Enter size of top list (1-20): 4 Top 4 female names: (1) Emily (2) Jessica (3) Ashley (4) Sarah Top 4 male names (1) Michael (2) Matthew (3) Jacob (4) Christopher Enter (n)ame, (r)ank, (t)op, or (q)uit: qExplanation / Answer
Implemented the code as per the requirement. Please comment if you want any modification required.
code:
------------
package com.chegg.expert;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;
public class BabyNames {
public static String[] fnames;
public static String[] mnames;
public static int call = 1;
public static Scanner s;
static int ind = 0, ind1=0;
public static void main(String[] args) throws IOException {
s = new Scanner(System.in);
boolean quit = false;
String fname = "";
System.out.print("Enter a birth year(1880-2016): ");
int year = 0;
while(!quit) {
year = s.nextInt();
if(year<1880 || year>2016) {
System.out.print("Enter a valid yob between 1880 to 2016: ");
continue;
}
quit = true;
break;
}
System.out.println("yob"+year+".txt");
readNamesData("yob"+year+".txt");
}
public static void readNamesData(String fname) throws IOException {
boolean quit = false;
String ch = "";
s = new Scanner(System.in);
while(!quit) {
System.out.print("Enter (n)ame, (r)ank, (t)op, (q)uit: ");
s = new Scanner(System.in);
ch = s.next();
if(call == 1) {
call++;
File fil = new File("");
String path = fil.getAbsolutePath();
System.out.println(path+"/names/"+fname);
File file = new File( path+"/names/"+fname);
s = new Scanner(file);
long count = Files.lines(Paths.get(path+"/names/"+fname)).count();
System.out.println(count);
String line = "";
fnames = new String[(int) count];
mnames = new String[(int) count];
while(s.hasNext()) {
line = s.nextLine();
//System.out.println(line);
String[] arr = line.split(",");
if(arr[1].equals("M")) {
mnames[ind] = arr[0];
ind++;
}
else if(arr[1].equals("F")) {
fnames[ind1] = arr[0];
ind1++;
}
//System.out.println("Entry #" + ++i + ": first name: " + firstname + ", last name: " + lastname + ", phone number: " + phoneNumber);
}
}
if(ch.equalsIgnoreCase("n")) {
nameOption();
}
else if(ch.equalsIgnoreCase("r")) {
rankOption();
}
else if(ch.equalsIgnoreCase("t") ){
topOption();
}
else if(ch.equalsIgnoreCase("q")) {
quit = true;
}
else {
System.out.println("Please enter valid input..");
}
}
}
public static int numNames(String fname, char sex) {
//As file reading already done above here jus returning the count if males and females
if(sex=='M') {
return ind;
}
else {
return ind1;
}
}
public static int getRank(String name, char sex) {
boolean found = false;
long i=0;
if(sex == 'F') {
for(i=0; i<ind1;i++) {
if(fnames[(int) i].equalsIgnoreCase(name)) {
found = true;
break;
}
}
}
if(found) {
System.out.println(name+" ranked "+(i+1)+" for females");
}
else {
System.out.println(name+"not ranked for females");
}
if(sex == 'M') {
for(i=0; i<ind;i++) {
if(mnames[(int) i].equalsIgnoreCase(name)) {
found = true;
break;
}
}
}
if(found) {
System.out.println(name+" ranked "+(i+1)+" for males");
}
else {
System.out.println(name+"not ranked for males");
}
return 0;
}
public static void nameOption() {
s = new Scanner(System.in);
System.out.print("Enter name: ");
String name = s.nextLine();
boolean found = false;
long i=0;
for(i=0; i<ind1;i++) {
if(fnames[(int) i].equalsIgnoreCase(name)) {
found = true;
break;
}
}
if(found) {
System.out.println(name+" ranked "+(i+1)+" for females");
}
else {
System.out.println(name+"not ranked for females");
}
for(i=0; i<ind;i++) {
if(mnames[(int) i].equalsIgnoreCase(name)) {
found = true;
break;
}
}
if(found) {
System.out.println(name+" ranked "+(i+1)+" for males");
}
else {
System.out.println(name+"not ranked for males");
}
}
public static void rankOption(){
s = new Scanner(System.in);
System.out.println("Enter rank: ");
int lim = s.nextInt();
System.out.println(fnames[lim]+" is the #"+lim+" female name");
System.out.println(mnames[lim]+" is the #"+lim+" male name");
}
public static void topOption() {
System.out.println("Enter top: ");
s = new Scanner(System.in);
int lim = s.nextInt();
System.out.println("Top "+lim+" female names");
for(int i=0; i<lim; i++) {
System.out.println(fnames[i]);
}
System.out.println("Top "+lim+" male names");
for(int i=0; i<lim; i++) {
System.out.println(mnames[i]);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.