Write the definition of a recursive class (static) method \"hop\". Do not use lo
ID: 3908154 • Letter: W
Question
Write the definition of a recursive class (static) method "hop". Do not use loops. Your hop method has the following signature:
public static boolean hop(int[] data, int index, int limit)
Write two base cases:
If index is negative or limit is less than one then return false.
Otherwise, if index is greater than equal to the length of the array return true.
If index is non-negative and less than the array length then use tail-recursion: Recursively call hop with a new index value equal to data[index] and the limit value reduced by one. You can assume the data array has at least one entry and that following the data values eventually leads to a negative or large valued entry.
JUST WRITE THE METHOD-- NOT THE WHOLE Link CLASS.
Explanation / Answer
Dear Student,
public static boolean hop(int[] data, int index, int limit){
if (index <0 || limit<1)
return false;
if (index >= data.length)
return true;
if (index>0 && index<data.length)
return hop(data,data[index],limit--);
}
If you like this answer, give a thumbs up! If you have any doubt feel free to revert back, would be happy to help! Happy Learning :) :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.