[SOLVED] Just follow the instructions!
June 11th, 2022
I dont know how to handle this Computer Science question and need guidance.
Struggling to find relevant content or pressed for time? – Don’t worry, we have a team of professionals to help you on
[SOLVED] Just follow the instructions!
Get a 15% Discount on this Paper
- SUMMARY
- Adjust the code we did in class to do a recursive binary search on a sorted array of Strings instead of integers.
- This lab will involve the following new features:
- Recursion
- Binary Search Algorithm
- DETAILS
- 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.
- Set up your String array.
- 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”;
…
- Change all references to the array and any references pulling values from the array to String type instead of int type.
- Adjustment to binary search method.
- 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.
- Example for equals check:
- Instead of…
- iTarget == …
- It’s this…
- sTarget.equals(…)
- Instead of…
- Example for greater than check:
- Instead of…
- iTarget > …
- It’s this…
- sTarget.compareTo(…) > 0
- 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.
- sTarget.compareTo(…) > 0
- Instead of…
- Example for equals check:
- 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.
- You can remove, comment out, or update the traditional search method at the bottom of the code. It’s up to you.