Question
Please complete the following method. It is a recursive linear search method with a recursive step that finds the last occurrence of a target in
Please complete the following method. It is a recursive linear search method with a recursive step that finds the last occurrence of a target in an array, not IT 179: Introduction to Data Structures: Assignment 05 2 the first. The corresponding wrapper method is available. You will need to modify the linear search method, so that the search position is from the last element instead of the first one. Note: If the search failed, return -1. You may dont need the statement return -1 in the following methods.
public class RecursiveMethods {
/**
* Recursive linear search method (in RecursiveMethods.java).
* @param items The array being searched
* @param target The item being searched for
* @param posFirst The position of the current first element
* @return The subscript of target if found; otherwise -1
*/
private static int linearSearch(Object[] items, Object target, int posFirst) {
if (posFirst == items.length) {
return -1;
} else if (target.equals(items[posFirst])) {
return posFirst;
} else {
return linearSearch(items, target, posFirst + 1);
}
}
/**
* Wrapper for recursive linear search method (in
* RecursiveMethods.java).
* @param items The array being searched
* @param target The object being searched for
* @return The subscript of target if found; otherwise -1
*/
public static int linearSearch(Object[] items, Object target) {
return linearSearch(items, target, 0);
}
//Please complete this method for A05_Q3
/**
* Recursive linear search method
* @param items The array being searched
* @param target The item being searched for
* @param posLast The position of the current last element
* @return The subscript of target if found; otherwise -1
*/
private static int linearSearchReverse(Object[] items, Object target, int posLast) {
return -1;
}
/**
* Wrapper for recursive linear search method
* @param items The array being searched
* @param target The object being searched for
* @return The subscript of target if found; otherwise -1
*/
public static int linearSearchReverse(Object[] items, Object target) {
return linearSearchReverse(items, target, items.length - 1);
}
}
_______________________________
import java.util.Arrays;
public class RecursiveMethodsTest {
public static void main(String[] args) {
String[] kidNames = {"Elliot", "Jonathan", "Caryn", "Debbie", "Dustin", "Jonathan",
"Jacquie", "Rich"};
System.out.println(Arrays.toString(kidNames));
int posKidName = RecursiveMethods.linearSearchReverse(kidNames, "Jonathan");
System.out.println("The last index of the sought item is: " + posKidName);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started