example of samlpe code given below names list this is a text reader and stream e
ID: 3837908 • Letter: E
Question
example of samlpe code given below names list
this is a text reader and stream example for a begginer java class
there are two files named “BoyNames2015.txt” and “GirlNames2015.txt”. These files contain the 1,000 most popular boy and girl names in the United States in 2015, as compiled by the Social Security Administration. The files contain one line per name. Each line has a name, a blank, and the number of registered births using that name in 2015. Names are listed in descending order of popularity, so that the most popular name for the year appears at the top of the file.
Create a class named “NameAndBirths” with two instance variables, one a String containing a name, and one an int containing the number of registered births in 2015 for that name.
Write a program that reads the girl and boy files into memory using arrays of 1,000 NameAndBirths objects each (one for girls, one for boys). Then allow the user to input a name. The program should search through both arrays of NameAndBirths objects. If there is a match, then it should output the popularity ranking, the gender, and the number of registered births for that name and gender. The program should also indicate if there is no matching name entry.
For example, if the user enters the name “Justice,” then the program should output:
Justice is ranked 452 among girls with 696 registered births.
Justice is ranked 525 among boys with 543 registered births.
If the user enters the name “Benjamin,” then the program should output:
Benjamin is not ranked among the top 1000 girl names.
Benjamin is ranked 10 among boys with 13,608 registered births.
Submit the listings for the class containing main and the NameandBirths class, and a PrintScreen of the output when running your program using at least three names.
girl names
Explanation / Answer
public class NameAndBirths {
private String name;
private int registeredNumber;
public NameAndBirths(String name,int registeredNumber) {
this.setName(name);
this.setRegisteredNumber(registeredNumber);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRegisteredNumber() {
return registeredNumber;
}
public void setRegisteredNumber(int registeredNumber) {
this.registeredNumber = registeredNumber;
}
}
=============================================================================
import java.io.File;
import java.util.Scanner;
public class NameAndBirthsDriver {
public static void main(String[] argv){
NameAndBirths boys[] = new NameAndBirths[1000];
NameAndBirths girls[] = new NameAndBirths[1000];
Scanner sc;
try{
sc = new Scanner(new File("BoyNames2015.txt"));
int i =0;
while(sc.hasNext()){
String name = sc.next();
int num = sc.nextInt();
NameAndBirths boy = new NameAndBirths(name, num);
boys[i] = boy;
i++;
}
sc.close();
sc = new Scanner(new File("GirlNames2015.txt"));
i =0;
while(sc.hasNext()){
String name = sc.next();
int num = sc.nextInt();
NameAndBirths girl = new NameAndBirths(name, num);
girls[i] = girl;
i++;
}
sc.close();
}
catch(Exception e){
}
sc = new Scanner(System.in);
System.out.print("Enter one Name :");
String name = sc.next();
for(int i=0; i<boys.length; i++){
NameAndBirths boy = boys[i];
if(boy.getName().equalsIgnoreCase(name)){
System.out.println(name+" is ranked "+(i+1)+" among boys with "+boy.getRegisteredNumber()+" registered births.");
}
}
for(int i=0; i<girls.length; i++){
NameAndBirths girl = girls[i];
if(girl.getName().equalsIgnoreCase(name)){
System.out.println(name+" is ranked "+(i+1)+" among girls with "+girl.getRegisteredNumber()+" registered births.");
}
}
sc.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.