Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a Java program which will use Binary Search to find a name within a list of sorted names. This program will read a list

Write a Java program which will use Binary Search to find a name within a list of sorted names. This program will read a list of names from a website then guesses a users last based on information provided by the user.

To implement this program, create a Java class called NameGuesser. First, implement a main method. The main method will attempt to guess a users last name. Before attempting to guess a name, your program must acquire a sorted list of names. Create a new URL object with the following URL

https://www2.census.gov/topics/genealogy/1990surnames/dist.all.last

and use scanner to parse the information contained in the file line by line, discarding the statistical information. Store the parsed information in an ArrayList. After parsing the file at the given URL, sort the ArrayList of names. (Hint, use Collections.sort()) Use the following code snippet to get started.

Scanner in = new Scanner(aUrl.openStream());

Once you have acquired a sorted list of last names, you will begin the process of guessing a users last name. You will use a version of binary search for the guessing. Use the following algorithm to implement binary search:

Define three integer variables named low, high, and mid.

At the beginning of the search, set low to 0 and high to the maximum index of the given Linked List.

Set mid to the average of low and high

Ask the use if their name comes before the name stored at index mid in the sorted ArrayList of names

Depending on the answer, update low or high.

If the users name comes after the item at mid, you want to set low to mid + 1

If the users name comes before the item at mid, you want to set high to mid - 1

Repeat this process until low >= high. When low >= high, ask the user if the name at mid is their name.

When you have completed your implementation of the NameGuesser class, use the provided test cases to test your work. When complete, submit your NameGuesser class to Blackboard. Also, in your Blackboard submission, please answer the following questions:

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-O for your answer.)

Below is an example of what your programs output should look like.

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

here is NameGuesserTest.java :

import static org.junit.Assert.*; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.PrintStream; import org.junit.After; import org.junit.Before; import org.junit.Test; public class NameGuesserTest { private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); @Before public void setUpStreams() { System.setOut(new PrintStream(outContent)); System.setErr(new PrintStream(errContent)); } @Test public void testSMITH() { //Pass an input into the console and then execute the main method of the program String input = "N N Y N Y N Y N N N Y Y Y Y D "; InputStream in = new ByteArrayInputStream(input.getBytes()); System.setIn(in); NameGuesser.main(null); String rawOutput = outContent.toString(); String[] splitOutput = rawOutput.split(" "); assertEquals("When looking at the number of guesses needed, we",15,splitOutput.length-1); String[] allGuesses = {"LANGLITZ","RITCHIE","SWAGGER","SHANKLIN","SPANO","SIRIANNI","SNAVELY","SLAGTER","SLUSARSKI","SMISEK","SMOLSKI","SMITS","SMITHEY","SMITHEE","SMITH"}; for(int i = 0; i < allGuesses.length; i++) { assertEquals("When checking if guess " +(i+1)+ " was " + allGuesses[i] + " we",true,splitOutput[i].contains(allGuesses[i])); } } @Test public void testWHITTEN() { //Pass an input into the console and then execute the main method of the program String input = "N N N N Y N N N Y Y N N N Y Y D "; InputStream in = new ByteArrayInputStream(input.getBytes()); System.setIn(in); NameGuesser.main(null); String rawOutput = outContent.toString(); String[] splitOutput = rawOutput.split(" "); assertEquals("When looking at the number of guesses needed, we",16,splitOutput.length-1); String[] allGuesses = {"LANGLITZ","RITCHIE","SWAGGER","VESEY","WIKER","WARRING","WERKHOVEN","WHITEHORN","WICKS","WHITTING","WHITLEDGE","WHITSELL","WHITTEMORE","WHITTENTON","WHITTENBECK","WHITTEN"}; for(int i = 0; i < allGuesses.length; i++) { assertEquals("When checking if guess " +(i+1)+ " was " + allGuesses[i] + " we",true,splitOutput[i].contains(allGuesses[i])); } } @Test public void testCRAIG() { //Pass an input into the console and then execute the main method of the program String input = "Y Y N Y N N N N Y N N N N Y D "; InputStream in = new ByteArrayInputStream(input.getBytes()); System.setIn(in); NameGuesser.main(null); String rawOutput = outContent.toString(); String[] splitOutput = rawOutput.split(" "); assertEquals("When looking at the number of guesses needed, we",15,splitOutput.length-1); String[] allGuesses = {"LANGLITZ","DURSO","BURROWS","CREST","CHERNEY","COLLELO","CORNFORTH","COUSER","CRAIGWELL","COWLEY","CRABBE","CRAFTER","CRAGUN","CRAIGER","CRAIG"}; for(int i = 0; i < allGuesses.length; i++) { assertEquals("When checking if guess " +(i+1)+ " was " + allGuesses[i] + " we",true,splitOutput[i].contains(allGuesses[i])); } } @Test public void testSINGLETARY() { //Pass an input into the console and then execute the main method of the program String input = "N N Y N Y Y N N N N Y Y Y N Y D "; InputStream in = new ByteArrayInputStream(input.getBytes()); System.setIn(in); NameGuesser.main(null); String rawOutput = outContent.toString(); String[] splitOutput = rawOutput.split(" "); assertEquals("When looking at the number of guesses needed, we",16,splitOutput.length-1); String[] allGuesses = {"LANGLITZ","RITCHIE","SWAGGER","SHANKLIN","SPANO","SIRIANNI","SHREWSBERRY","SIGNS","SIMMERS","SINGEWALD","SINQUEFIELD","SINISCALCHI","SINGLTON","SINGLER","SINGLETERRY","SINGLETARY"}; for(int i = 0; i < allGuesses.length; i++) { assertEquals("When checking if guess " +(i+1)+ " was " + allGuesses[i] + " we",true,splitOutput[i].contains(allGuesses[i])); } } @Test public void testALVEREZ() { //Pass an input into the console and then execute the main method of the program String input = "Y Y Y Y Y D"; InputStream in = new ByteArrayInputStream(input.getBytes()); System.setIn(in); NameGuesser.main(null); String rawOutput = outContent.toString(); String[] splitOutput = rawOutput.split(" "); assertEquals("When looking at the number of guesses needed, we",6,splitOutput.length-1); String[] allGuesses = {"LANGLITZ","DURSO","BURROWS","BELLINGHAM","ASHTON","ALVEREZ"}; for(int i = 0; i < allGuesses.length; i++) { assertEquals("When checking if guess " +(i+1)+ " was " + allGuesses[i] + " we",true,splitOutput[i].contains(allGuesses[i])); } } @After public void cleanUpStreams() { System.setOut(null); System.setErr(null); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 23rd International Conference Dexa 2012 Vienna Austria September 2012 Proceedings Part 1 Lncs 7446

Authors: Stephen W. Liddle ,Klaus-Dieter Schewe ,A Min Tjoa ,Xiaofang Zhou

2012th Edition

3642325998, 978-3642325991

More Books

Students also viewed these Databases questions

Question

What are the types of forms of communication ?

Answered: 1 week ago

Question

Explain the process of MBO

Answered: 1 week ago