String Binary Search

From CompSciWiki
Revision as of 17:06, 8 April 2010 by KarlW (Talk | contribs)

Jump to: navigation, search

Back to the Program-A-Day homepage

Problem

You are given an ordered array of Strings. Your program will do a few things. First, it must verify that the array is indeed ordered. This will be done in a separate method, and the mothod will return a boolean value. ('true' indicating that the array is ordered) Second, if the array is ordered, then you must prompt the user to guess a word that might be in the array. This prompt will be done with JOptionPane. Another method will then be required to conduct a binary search of the array to look for the user's guess. This method will also return a boolean indicating success or failure. Finally, the user will only be allowed to guess a maximum of five times before the program will stop.

For output, the program will first print the array. Then it will print the status of the array. (whether it is ordered or not) Then it will print out every guess the user makes. Finally, print out the user's number of attempts and whether they succeeded in entering a word that's in the array.

Your output should look something like this:

Array: "word1" "word2" "word3"
It is ordered.
Guess 1: blah
Guess 2: blah
Guess 3: word1

The array contains "word1".
It took you 3 attempt(s) to get it.
 

Problem Solving and Programming Examples

Wiki bench01.jpg


Solution

Let's start first by putting together the main method. When starting to write any section of code, we need to figure out what variables we will be using. We need: one integer variable to store the maximum number of tries a user can guess; three boolean values, one to track whether the array is ordered or not, one to track whether the user has reached the max number of attempts, and one to track whether they entered a guess that was in the array; a String to hold the input entered by JOptionPane; another integer to track the number of attempts made by the user; and finally, our ordered array of Strings. We'll place these at the top of our main method. Remember to always initialise your variables!

	final int MAX_TRIES = 5;

	boolean done = false;
	boolean found = false;
	boolean inOrder = false;
	String input = "";
	int count = 0;

	String [] ordered = {"a","cake","is","lie","the"};

Notice I used "final" and all caps for the maximum tries variable. That's because this number is a constant, and will never change.

Now, the problem stated that we needed to print out the array's contents, which needs to be done in a method. So I make the call to this non-existent method. At this point, you can either jump ahead and write the body for this method, or continue writing out the main method. While either strategy is fine, keep in mind that when writing an exam, you will be doing so in pencil. If you don't leave yourself enough room, jumping ahead to write a method might leave you out of room for the main method. I always like to write out a method as much as possible before going to the next one, so I will continue writing main.

Since we also need to know if the array is ordered or not, I will also make that non-existent call at this time. This method will be returning a boolean, so I should make sure to store it.

	print(ordered);
	inOrder = checkOrder(ordered);

Now remember that if an array is not ordered, the binary search will not work. So we should write an if-else statement that handles this. Also, an appropriate error message is always useful.

	if(inOrder)
	{
	}
	else
		System.out.println("This array is not in order, I cannot search it.");

Now within the if part, we need to have code for getting the user to make guesses. First we'll print a statement saying that the array is ordered. Then we'll start our while loop, and the condition of the while will be dependent on our "done" boolean, as there are a few conditions that might kick us out of the loop. So now we have:

	if(inOrder)
	{
		System.out.println("This array is ordered.");

		while(!done)
		{
		}
	}
	else
		System.out.println("This array is not in order, I cannot search it.");

Within the while loop we will ask the user for input, store that input, incrememnt the number of guess the user has entered, and print out the guess. Then we will conduct a search for the guess with another method call, which will be returning a boolean. Finally, as in any while loop, we need to check if we should stop the loop. The while loop should end if a guess was correct, or if the maximum number of guess has been reached. The following code goes within the while loop.

	input = JOptionPane.showInputDialog("Enter the word you are looking for?",null);
	count++;
	System.out.println("Guess " +count +": " +input);

	found = findString(input, ordered);

	if(found || count == MAX_TRIES)
		done = true;

The final part for our main method is to print out whether the user has met with success or not. We use the "found" boolean for this and print a success message if "found" is true or a failure message if false. We also need to print out how many attempts it took.

	if(found)
	{
		System.out.println("By Jove, the array contains " +input +"!");
		System.out.println("It took you " +count +" attempt(s) to get it.");
	}
	else
	{
		System.out.println("By Jove, it took you " +count +" tries and you still didn't get it?");
	}

And that is it for the main method!



Code

Solution Code

Back to the Program-A-Day homepage