Write a program that repeatedly quizzes the user about name

Write a program that repeatedly quizzes the user about name popularity. For instance:

> In 2015, was the name Charlotte (1) or Logan (2) more popular (enter 1 or 2)?

> 1

> Incorrect. There were 11332 girls named Charlotte, and 12862 boys named Logan

> Play again (y/n)?

To write this, read in the data in the files boynames2015.txt and girlnames2015.txt using relative pathnames (this will make grading easier for us). Create a class to store the rank, name, and number of births. For each line in the file, add an instance of the class to an array. Have your class implement the Comparable interface.

For practice with binary files, choose one of the arrays (e.g. girls) and write it to a binary file ending with “.dat” (e.g. “girls.dat”). This will store each class object in “raw” form.

Then, read the binary file you just wrote and print the most and least popular name from the dataset. Be sure to close each file stream after you are done using it.

You do not need to submit the .dat file you create.
Girl Names: https://goo.gl/bwr7PM
Boy Names: https://goo.gl/Ky4c6a

Rubric:

Solution

Please find the required solution:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

/**
* Quiz Popularity
*/
public class QuizExample {

//Read file and store to array
static List<Entity> readFile( String filename ) throws FileNotFoundException
{
List<Entity> entitylist = new ArrayList<>();
Scanner fileReader = new Scanner(new File(filename));
boolean firstLine = true;
int i = 0;
while( fileReader.hasNext() )
{
String line = fileReader.nextLine();
if( firstLine )
firstLine = false;
else
{
String[] value = line.split(\"\\\\s++\");
Entity entity = new Entity();
entity.setName(value[1]);
entity.setRank(Integer.valueOf(value[0]));
entity.setNumOfBirths(Integer.valueOf(value[2]));
entitylist.add(entity);
}
}
//close the input stream
fileReader.close();
return entitylist;
}

public static void main( String[] args ) throws FileNotFoundException
{
System.out.println(\"Reading file girlnames2015.txt\");
List<Entity> girls = readFile(\"girlnames2015.txt\");

// write the array list to .bat file
System.out.println(\"writing to file girls.bat\");
writeTOBinaryFile(\"girls.bat\", girls);
//Then, read the binary file you just wrote and print the most and least popular name from the dataset
System.out.println(\"reading from file girls.bat\");
List<Entity> girlsFromBinaryFile = readBinaryObjectFromFile(\"girls.bat\");
Entity leastPopular = Collections.min(girlsFromBinaryFile);
Entity mostPopular = Collections.max(girlsFromBinaryFile);
System.out.println(
\"Least Popular-Name:\" + leastPopular.getName() + \", Number of Births:\" + leastPopular.getNumOfBirths());
System.out.println(
\"Most Popular-Name:\" + mostPopular.getName() + \", Number of Births:\" + mostPopular.getNumOfBirths());

System.out.println(\"Quiz started\");

/**
* Read data from keyboard and compare the results
*/
Scanner keyboard = new Scanner(System.in);
Random rdm = new Random();
int input;
do
{
Entity one, two;
System.out.println(\" In 2015, was the name \" + (one = girlsFromBinaryFile.get(rdm.nextInt(1000))).getName()
+ \" or \" + (two = girlsFromBinaryFile.get(rdm.nextInt(1000))).getName()
+ \" more popular (enter 1 or 2 or anynumber to quit)?\");
input = keyboard.nextInt();

//Compare input and display result
if( input == 1 || input == 2 )
{
if( (input == 1 && one.compareTo(two) < 0) || (input == 2 && two.compareTo(one) < 0) )
{
System.out.println(\"InCorrect. There were \" + one.getNumOfBirths() + \" girls named \" + one.getName()
+ \", and \" + two.getNumOfBirths() + \" boys named \" + two.getName());
}
else
System.out.println(\"Correct. There were \" + one.getNumOfBirths() + \" girls named \" + one.getName()
+ \", and \" + two.getNumOfBirths() + \" boys named \" + two.getName());
}
else
{
System.out.println(\"you selected to quit\");
}
} while( input == 1 || input == 2 );
keyboard.close();

System.out.println(\"Quiz ended\");
}

/**
* Store object in raw form
*
* @param filename
* @param obj
*/
static void writeTOBinaryFile( String filename, List<Entity> obj )
{
try
{
FileOutputStream fout = new FileOutputStream(filename);
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(obj);
oos.close();
}
catch( Exception e )
{
e.printStackTrace();
}
}

/**
* Read object from raw form
*
* @param filename
* @return
*/
static List<Entity> readBinaryObjectFromFile( String filename )
{
List<Entity> ret = null;
try
{
FileInputStream fin = new FileInputStream(filename);
ObjectInputStream ois = new ObjectInputStream(fin);
ret = (List<Entity>) ois.readObject();
ois.close();
}
catch( Exception e )
{
e.printStackTrace();
}
return ret;
}
}

class Entity implements Comparable<Entity>, Serializable {

int rank;
String name;
int numOfBirths;

public int getRank()
{
return rank;
}

public void setRank( int rank )
{
this.rank = rank;
}

public String getName()
{
return name;
}

public void setName( String name )
{
this.name = name;
}

public int getNumOfBirths()
{
return numOfBirths;
}

public void setNumOfBirths( int numOfBirths )
{
this.numOfBirths = numOfBirths;
}

@Override
public int compareTo( Entity o )
{
return this.getNumOfBirths() - o.getNumOfBirths();
}
}

Sample output:

Reading file girlnames2015.txt
writing to file girls.bat
reading from file girls.bat
Least Popular-Name:Alianna, Number of Births:266
Most Popular-Name:Emma, Number of Births:20355
Quiz started
In 2015, was the name Malaysia or Luciana more popular (enter 1 or 2 or anynumber to quit)?
1
Correct. There were 719 girls named Malaysia, and 677 boys named Luciana
In 2015, was the name Lydia or Danica more popular (enter 1 or 2 or anynumber to quit)?
2
InCorrect. There were 3474 girls named Lydia, and 399 boys named Danica
In 2015, was the name Giuliana or Violet more popular (enter 1 or 2 or anynumber to quit)?
1
InCorrect. There were 683 girls named Giuliana, and 4779 boys named Violet
In 2015, was the name Ireland or Kairi more popular (enter 1 or 2 or anynumber to quit)?
2
Correct. There were 272 girls named Ireland, and 365 boys named Kairi
In 2015, was the name Luz or Alexa more popular (enter 1 or 2 or anynumber to quit)?
3
you selected to quit
Quiz ended
Write a program that repeatedly quizzes the user about name popularity. For instance: > In 2015, was the name Charlotte (1) or Logan (2) more popular (enter
Write a program that repeatedly quizzes the user about name popularity. For instance: > In 2015, was the name Charlotte (1) or Logan (2) more popular (enter
Write a program that repeatedly quizzes the user about name popularity. For instance: > In 2015, was the name Charlotte (1) or Logan (2) more popular (enter
Write a program that repeatedly quizzes the user about name popularity. For instance: > In 2015, was the name Charlotte (1) or Logan (2) more popular (enter

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site