In this lab, you will implement a “guess your name” game where the computer find
ID: 3818534 • Letter: I
Question
In this lab, you will implement a “guess your name” game where the computer finds a name in a sorted list of names. The computer will use this method to “guess” your last name:
This program tries to guess your last name, but you have to give some hints. Does your name come before "LANGLITZ" in the dictionary? (Y/N) y Does your name come before "DURSO" in the dictionary? (Y/N) y Does your name come before "BURROWS" in the dictionary? (Y/N) n …. Is your name "DUKE"? (Y/N) y
1. Read names from census web site and sort them Start out with a program that reads all names from the "last names" file at http://www2.census.gov/topics/genealogy/1990surnames/dist.all.last. Discard the statistical information. Sort the names, using Collections.sort.
Use this code outline:
public class NameGuesser {
private ArrayList<String> lastNames = new ArrayList<String>();
public void readNames() throws IOException, MalformedURLException { URL url = new URL("http://www2.census.gov/topics/genealogy/1990surnames/dist.all.last");
Scanner in = new Scanner(url.openStream());
. . .
} }
Write the code for reading all names and sorting them in the readNames() method of the NameGuesser class?
2. Now you will implement the method in NameGuesser that guesses the name, call the method guessName.
• At the beginning of the search, set low to 0 and high to the maximum index of the names list. • Set mid to the average of low and high, and then ask the user if the name comes before the list entry at index mid. • Depending on the answer, update low or high. • If low >= high, ask if the name matches.
Write the code for your guessName method and add it to NameGuesser?
3. Write a separate class called NameGame that that instantiates an instance of NameGuesser to play the game. 4. How many entries does your names list have? How many guesses does your program need to make until it has guessed the user's name or given up? (Use big-Oh for your answer.)
Explanation / Answer
public class NameGuesser {
private ArrayList<String> lastNames = new ArrayList<String>();
public void readNames() throws IOException, MalformedURLException
{
// Read the last names
URL url = new URL("http://www.census.gov/genealogy/names/dist.all.last");
Scanner in = new Scanner(url.openStream());
while (in.hasNext())
{
// Read the last name
String lastName = in.next();
lastNames.add(lastName);
// Ignore the statistical information
in.nextDouble();
in.nextDouble();
in.nextInt();
}
in.close();
// Sort the names
Collections.sort(lastNames);
}
public void guessName()
{
int low = 0;
int high = lastNames.size() - 1;
int mid = (low + high) / 2;
Scanner in = new Scanner(System.in);
System.out.println("My Program is going to guess your last name,"
+ " with some of your help.");
String reply;
boolean complete = false;
while (!complete)
{
System.out.println("Does your name comes before "" + lastNames.get(mid)
+ "" in the dictionary? (Yes/No)");
reply = in.next();
if (reply.equalsIgnoreCase("Yes"))
{
high = mid - 1;
}
else
{
low = mid + 1;
}
mid = (low + high) / 2;
if (low >= high)
{
complete = true;
}
}
System.out.println("Is your name "" + lastNames.get(mid) + ""? (Yes/No)");
reply = in.next();
if (reply.equalsIgnoreCase("No"))
{
System.out.println("Sorry, your last name is not in the list.");
}
}
}
The names list has total 88799 entries.
In every guess the system will either be correct or incorrect. this way half the times the answer may be correct or half of the time it may be incorrect. It works the same way as we search a name in a dictionary to find its meaning. SO if i have to search for the word "Sorting" in dictionary. I will directly first jump to half way to the alphabetical order. This way the guess is to move on 2nd half of the page. In computer sciennce we call this as O(log(n)) algorithm.
public class NameGuesser {
private ArrayList<String> lastNames = new ArrayList<String>();
public void readNames() throws IOException, MalformedURLException
{
// Read the last names
URL url = new URL("http://www.census.gov/genealogy/names/dist.all.last");
Scanner in = new Scanner(url.openStream());
while (in.hasNext())
{
// Read the last name
String lastName = in.next();
lastNames.add(lastName);
// Ignore the statistical information
in.nextDouble();
in.nextDouble();
in.nextInt();
}
in.close();
// Sort the names
Collections.sort(lastNames);
}
public void guessName()
{
int low = 0;
int high = lastNames.size() - 1;
int mid = (low + high) / 2;
Scanner in = new Scanner(System.in);
System.out.println("My Program is going to guess your last name,"
+ " with some of your help.");
String reply;
boolean complete = false;
while (!complete)
{
System.out.println("Does your name comes before "" + lastNames.get(mid)
+ "" in the dictionary? (Yes/No)");
reply = in.next();
if (reply.equalsIgnoreCase("Yes"))
{
high = mid - 1;
}
else
{
low = mid + 1;
}
mid = (low + high) / 2;
if (low >= high)
{
complete = true;
}
}
System.out.println("Is your name "" + lastNames.get(mid) + ""? (Yes/No)");
reply = in.next();
if (reply.equalsIgnoreCase("No"))
{
System.out.println("Sorry, your last name is not in the list.");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.