Question

Must be written in JAVA Code Modify Fig. 19.2 to use recursive method recursiveLinear-Search to perform...

Must be written in JAVA Code

Modify Fig. 19.2 to use recursive method recursiveLinear-Search to perform a linear search of the array. The method should receive the search key and starting index as arguments. If the search key is found, return its index in the array; otherwise, return –1. Each call to the recursive method should check one index in the array.

Figure 19.2

import java.security.SecureRandom;

import java.util.Arrays;

import java.util.Scanner;

public class LinearSearchTest{

public static int linearSearch(int data[], int searchKey) {

for (int index = 0; index < data.length; index++) {

if (data[index] == searchKey) {

return index;

}

}

return -1;

}

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

SecureRandom generator = new SecureRandom();

int[] data = new int [10];

for (int i = 0; i < data.length; i++) {

data[i] = 10 + generator.nextInt(90);

}

System.out.printf("%s%n%n", Arrays.toString(data));

System.out.print("Please enter an integer value (-1 to quit): ");

int searchInt = input.nextInt();

while (searchInt != -1) {

int position = linearSearch(data, searchInt);

if (position == -1) {

System.out.printf("%d was not found%n%n", searchInt);

}

else {

System.out.printf("%d was found in position %n%n", searchInt, position);

}

System.out.print("Please enter an integer value (-1 to quit): ");

searchInt = input.nextInt();

}

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

import java.security.SecureRandom;

import java.util.Arrays;

import java.util.Scanner;

public class LinearSearchTest {

    public static int linearSearch(int data[], int searchKey, int startingIndex) {

        if(startingIndex==data.length)

            return -1;

        if(data[startingIndex]==searchKey)

            return startingIndex;

        return linearSearch(data, searchKey, startingIndex+1);

    }

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        SecureRandom generator = new SecureRandom();

        int[] data = new int[10];

        for (int i = 0; i < data.length; i++) {

            data[i] = 10 + generator.nextInt(90);

        }

        System.out.printf("%s%n%n", Arrays.toString(data));

        System.out.print("Please enter an integer value (-1 to quit): ");

        int searchInt = input.nextInt();

        while (searchInt != -1) {

            int position = linearSearch(data, searchInt, 0);

            if (position == -1) {

                System.out.printf("%d was not found%n%n", searchInt);

            } else {

                System.out.printf("%d was found in position %d%n%n", searchInt, position);

            }

            System.out.print("Please enter an integer value (-1 to quit): ");

            searchInt = input.nextInt();

        }

    }

}

Add a comment
Know the answer?
Add Answer to:
Must be written in JAVA Code Modify Fig. 19.2 to use recursive method recursiveLinear-Search to perform...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please write where its written write your code here!! please use java for the code! please...

    Please write where its written write your code here!! please use java for the code! please use the given code and I will rate thanks Magic index in an array a[1..n] is defined to be an index such that a[ i ] = i. Given an array of integers, write a recursive method to find the first magic index from left to right. If one exists in the given array, return the index number i, otherwise return -1. Here are...

  • You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement...

    You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement this method return new int[] {}; }    There is a utility method provided for you with the following signature. You may use this method to convert a list of integers into an array. public static int[] convertIntegers(List<Integer> integers) Provided code import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class MatrixSearch { // This method converts a list of integers to an array...

  • I have the following classes SearchMain.java import java.security.SecureRandom; import java.util.Arrays; import java.util.HashSet; import java.util.Set; /** *...

    I have the following classes SearchMain.java import java.security.SecureRandom; import java.util.Arrays; import java.util.HashSet; import java.util.Set; /** * * DO NOT CHANGE THIS CODE * */ public class SearchMain {    public static void main(String[] args) {        ArraySearcher arraySearch = new ArraySearchImp();        SecureRandom secureRandom = new SecureRandom();        int[] sortedArray = generateRandomSortedIntArray(10);        System.out.println(Arrays.toString(sortedArray));        Set<Integer> intSet = new HashSet<Integer>();               for(int i = 0; i < 5; i++) {       ...

  • Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and...

    Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and implementation HighArray class and note the attributes and its methods.    Create findAll method which uses linear search algorithm to return all number of occurrences of specified element. /** * find an element from array and returns all number of occurrences of the specified element, returns 0 if the element does not exit. * * @param foundElement   Element to be found */ int findAll(int...

  • pls help, idk whats wrong with this Add the reverse() method which reverses the content of...

    pls help, idk whats wrong with this Add the reverse() method which reverses the content of array without using additional array and the rotate(k) method which rotates left the content of array without using additional array by k elements. import java.util.*; * Implementation of the ADT List using a fixed-length array. * * if insert is successful returns 1, otherwise 0; * for successful insertion: * list should not be full and p should be valid. * * if delete...

  • Modify the program that you wrote for the last exercise in a file named Baseball9.java that...

    Modify the program that you wrote for the last exercise in a file named Baseball9.java that uses the Player class stored within an array. The program should read data from the file baseball.txt for input. The Player class should once again be stored in a file named Player.java, however Baseball9.java is the only file that you need to modify for this assignment. Once all of the input data from the file is stored in the array, code and invoke a...

  • Write a Java application that implements the recursive Binary Search alogrithm below: Write a Java application...

    Write a Java application that implements the recursive Binary Search alogrithm below: Write a Java application that implements the recursive Binary Search alogrithm below:/** * Performs the standard binary search using two comparisons * per level. This is a driver that calls the recursive method * @return index where item is found or NOT FOUND if not found */public static <AnyType extends Comparable<? super AnyType>> int binarySearch(AnyType [] a, AnyType x) {return binarySearch(a, x, 0, a.length -1);}/** * Hidden recursive...

  • I need to modify the code below to perform a binary search instead of a linear...

    I need to modify the code below to perform a binary search instead of a linear search. #include <iostream> using namespace std; int searchList(int[], int, int); int main() {    const int NUMS = 10;    int Picks[NUMS] = { 13579, 26791, 26792, 33445, 55555,                        62483, 77777, 79422, 85647, 93121 };    int WinNums,        Search;      cout << "Enter this week's winning five-digit number: ";    cin >> WinNums;    Search =...

  • Add reverse() method which reverses the content of array without using additional array. rotate(k) method which...

    Add reverse() method which reverses the content of array without using additional array. rotate(k) method which rotates left the content of array without using additional array by k elements. import java.util.*; * Implementation of the ADT List using a fixed-length array. * * if insert is successful returns 1, otherwise 0; * for successful insertion: * list should not be full and p should be valid. * * if delete is successful returns 1, otherwise 0; * for successful deletion:...

  • Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to...

    Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to both classes: public void reverseThisList(), This method will reverse the lists. When testing the method: print out the original list, call the new method, then print out the list again ------------------------------------------------------------------------- //ARRAY LIST class: public class ArrayList<E> implements List<E> { /** Array of elements in this List. */ private E[] data; /** Number of elements currently in this List. */ private int size; /**...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT