Java lab, implement a “guess your name” game where the computer finds a name in
ID: 3812097 • Letter: J
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 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());
.... . . . .
}
}
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.)
Explanation / Answer
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class NameGuesser {
private ArrayList<String> lastNames = new ArrayList<String>();
Scanner sc = new Scanner(System.in);
public void readNames() throws IOException, MalformedURLException
{
System.out.println("Reading Names....");
URL url = new URL("http://www2.census.gov/topics/genealogy/1990surnames/dist.all.last");
Scanner in = new Scanner(url.openStream());
System.out.println("Found URL");
while(in.hasNext()){
String[] data = in.nextLine().split(" ");
String name = data[0];//in.nextLine();
lastNames.add(name);
System.out.println("added "+name);
}
System.out.println("Completed Reading Names.");
System.out.println("Added "+lastNames.size()+" names.");
in.close();
Collections.sort(lastNames);
}
public boolean isBefore(String name){
System.out.print(" Does your name come before ""+name+"" in the dictionary? (Y/N):");
if(sc.next().equalsIgnoreCase("y"))
return true;
return false;
}
public boolean isAfter(String name){
System.out.print(" Does your name come after ""+name+"" in the dictionary? (Y/N):");
if(sc.next().equalsIgnoreCase("y"))
return true;
return false;
}
public void guessName(){
int low = 0;
int high = lastNames.size()-1;
int mid = (low + high)/2;
int numOfGuess = 0;
while(high>low){
String name = lastNames.get(mid);
numOfGuess++;
if(isBefore(name)){
high = mid-1;
mid = (low + high)/2;
}
else if(isAfter(name)){
low = mid+1;
mid = (low + high)/2;
}
else{
System.out.print("is your name ""+name+""? (Y/N): ");
if(sc.next().equalsIgnoreCase("y")){
System.out.println(" Found your name in "+numOfGuess+" Guesses.");
}
else{
System.out.println(" Your name is not in dictionary.");
}
break;
}
}
}
}
public class NameGame {
public static void main(String[] args) {
NameGuesser game = new NameGuesser();
try{
game.readNames();
}catch(Exception e){
e.printStackTrace();
}
game.guessName();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.