Question: Java lab, implement a guess your name game where the computer finds a name in a sorted list of names. The computer will use this
Java lab,
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
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());
.... . . . .
}
}
1) 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.
3) At the beginning of the search, set low to 0 and high to the maximum index of the names list.
4) 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.
5) Depending on the answer, update low or high.
6) If low >= high, ask if the name matches.
Write the code for your guessName method and add it to NameGuesser?
Write a separate class called NameGame that that instantiates an instance of NameGuesser to play the game.
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.)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
