Question

In: Computer Science

Learning objectives; File I/O practice, exceptions, binary search, recursion. Design and implement a recursive version of...

Learning objectives; File I/O practice, exceptions, binary search, recursion. Design and implement a recursive version of a binary search. Instead of using a loop to repeatedly check for the target value, use calls to a recursive method to check one value at a time. If the value is not the target, refine the search space and call the method again. The name to search for is entered by the user, as is the indexes that define the range of viable candidates can be entered by the user (that are passed to the method). The base case that ends recursion is either finding the value within the range or running out of data to search. The program will work on an array of sorted String objects read in from a text file. Your program will generate an exception, that provides a message to the user, if the file can't be found or if the starting index is after the ending index. You will create your own text file of first names in alphabetical order (binary search required ordering) to test your program, make sure to include that file with your submission. JAVA CODE

Solutions

Expert Solution

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class RecursiveBinarySearch {
  
private static int recursiveBinarySearch(String arr[], int leftIndex, int rightIndex, String target)
{
int index = -1;
if(rightIndex >= leftIndex && leftIndex < arr.length - 1)
{
int midIndex = leftIndex + (rightIndex - leftIndex) / 2;
if(arr[midIndex].compareToIgnoreCase(target) == 0)
index = midIndex;
else if(arr[midIndex].compareToIgnoreCase(target) > 0)
index = recursiveBinarySearch(arr, leftIndex, midIndex - 1, target);
else
index = recursiveBinarySearch(arr, midIndex + 1, rightIndex, target);
}
return index;
}
  
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Scanner fileReader;
System.out.print("Enter the input file name: ");
String inFileName = sc.nextLine().trim();
  
// read file to count number of names
int countStrs = 0;
try
{
fileReader = new Scanner(new File(inFileName));
while(fileReader.hasNextLine())
{
String s = fileReader.nextLine();
countStrs++;
}
fileReader.close();
}catch(FileNotFoundException fnfe){
System.out.println(inFileName + " could not be found! Exiting..");
System.exit(0);
}
  
// read all names from input file
System.out.println("\nAttempting to read " + inFileName + "..");
String names[] = new String[countStrs];
int index = 0;
try
{
fileReader = new Scanner(new File(inFileName));
while(fileReader.hasNextLine())
{
names[index++] = fileReader.nextLine().trim();
}
System.out.println(countStrs + " names successfully read from " + inFileName + ".");
fileReader.close();
}catch(FileNotFoundException fnfe){
System.out.println(inFileName + " could not be found! Exiting..");
System.exit(0);
}
  
// sort the array of names in ascending order
for(int i = 0; i < countStrs - 1; i++)
{
for(int j = 0; j < countStrs - i - 1; j++)
{
if(names[j].compareToIgnoreCase(names[j + 1]) > 0)
{
String temp = names[j];
names[j] = names[j + 1];
names[j + 1] = temp;
}
}
}
  
// prompt user to enter the name to search
System.out.println();
String target;
do
{
System.out.print("Enter the target name (quit to stop): ");
target = sc.nextLine().trim();
if(target.equalsIgnoreCase("quit"))
break;
int resultIndex = recursiveBinarySearch(names, 0, countStrs - 1, target);
if(resultIndex == -1)
System.out.println("Sorry! " + target + " is not found in the array.\n");
else
System.out.println(target + " is found at index " + (resultIndex + 1) + ".\n");
}while(!target.equalsIgnoreCase("quit"));
  
}
}

******************************************************* SCREENSHOT *********************************************************

INPUT FILE (names.txt) - This file needs to be created before running the code and this file should be created within the same working project directory where the above RecursiveBinarySearch.java file will be residing.

CONSOLE OUTPUT :

THANK YOU!! PLEASE VOTE


Related Solutions

JAVA CODE Learning objectives; File I/O practice, exceptions, binary search, recursion. Design and implement a recursive...
JAVA CODE Learning objectives; File I/O practice, exceptions, binary search, recursion. Design and implement a recursive version of a binary search.  Instead of using a loop to repeatedly check for the target value, use calls to a recursive method to check one value at a time.  If the value is not the target, refine the search space and call the method again.  The name to search for is entered by the user, as is the indexes that define the range of viable candidates...
Java String search Design and implement a recursive version of a binary search.  Instead of using a...
Java String search Design and implement a recursive version of a binary search.  Instead of using a loop to repeatedly check for the target value, use calls to a recursive method to check one value at a time.  If the value is not the target, refine the search space and call the method again.  The name to search for is entered by the user, as is the indexes that define the range of viable candidates can be entered by the user (that are...
In the recursive version of binary search each function call reduces the search size by half....
In the recursive version of binary search each function call reduces the search size by half. Thus in the worst case for a sorted list of length n. The algorithm makes log n calls. Is it possible to make fewer than log n calls? Group of answer choices 1) Yes, depends on when it finds the element it is looking for 2) No, it will always make log n calls.
This assignment will give you practice in Handling Exceptions and using File I/O. Language for this...
This assignment will give you practice in Handling Exceptions and using File I/O. Language for this program is JAVA Part One A hotel salesperson enters sales in a text file. Each line contains the following, separated by semicolons: The name of the client, the service sold (such as Dinner, Conference, Lodging, and so on), the amount of the sale, and the date of that event. Prompt the user for data to write the file. Part Two Write a program that...
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with...
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with comments and I need the input file to be placed with Scanner not BufferedReader Please help I need Class River Class CTRiver and Class Driver Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong() that returns true if river is above 30 miles...
Implement a recursive binary search on an array of 8-byte integers in LEGV8
Implement a recursive binary search on an array of 8-byte integers in LEGV8
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
I am trying to implement a search function for a binary search tree. I am trying...
I am trying to implement a search function for a binary search tree. I am trying to get the output to print each element preceding the the target of the search. For example, in the code when I search for 19, the output should be "5-8-9-18-20-19" Please only modify the search function and also please walk me through what I did wrong. I am trying to figure this out. Here is my code: #include<iostream> using namespace std; class node {...
I was trying to implement a simple binary search tree using this given class of bst...
I was trying to implement a simple binary search tree using this given class of bst in c++ public: BST(); ~BST(); void insertKey(int newKey); bool hasKey(int searchKey); std::vector<int> inOrder(); int getHeight(); however; i am still required to use another class for the nodes as a pointer and i need to manage memory leak. in main we should ask for the numbers we need to insert in the binary search tree and also let the user end it with a letter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT