Just follow the instructions!

Question Description

  1. SUMMARY
  1. Adjust the code we did in class to do a recursive binary search on a sorted array of Strings instead of integers.
  1. This lab will involve the following new features:
    1. Recursion
    2. Binary Search Algorithm
  1. DETAILS
  1. These instructions will be more bare than usual since the source code from class is being provided and I want you to think through all the changes you need to make.
  1. Set up your String array.
    1. Instead of having a “for” loop populating your array with random numbers, go ahead and make your array 10 in length and populate it in 10 lines of code that put a different string in each array slot, such as:

asWords[0] = “Frank”;

  1. Change all references to the array and any references pulling values from the array to String type instead of int type.
  1. Adjustment to binary search method.
    1. Besides figuring out which things have to now become String type, you need to compare the string values a bit different than the integers by using the “equals” and “compareTo” methods.
      1. Example for equals check:
        1. Instead of…
          1. iTarget == …
        2. It’s this…
          1. sTarget.equals(…)
      2. Example for greater than check:
        1. Instead of…
          1. iTarget > …
        2. It’s this…
          1. sTarget.compareTo(…) > 0
            1. The reason you are seeing if the comparison is greater than 0 is because the “compareTo” method returns a number greater than 0 if sTarget is greater than the string that’s passed in the parentheses. If it’s equal, it returns 0 (but that won’t happen because the equals comparison was in the first if statement). And if it’s less than, it returns a number less than 0.
  1. You can remove, comment out, or update the traditional search method at the bottom of the code. It’s up to you.

Get your college paper done by experts

Do my question How much will it cost?

Place an order in 3 easy steps. Takes less than 5 mins.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *