I'm doing an assignment for a class, but I'm not sure why the code I wrote for these two methods do not work.
For the first method, I'm trying to compare the current position in the array with the next one and if the next one is larger, it becomes the largest. Other wise, the int at the current position becomes the largest. After going through the array with recursive method call, it will return the largest int in the array.
// This method takes an integer array as well as an integer (the starting index) and returns the largest number in the array.
public int largestRec(int[] arr, int pos)
{
// TODO: implement this method
int largest = arr[pos];
if(pos == arr.length-1)
{
return largest;
}
else
{
if(arr[pos] < arr[pos+1])
{
largest = arr[pos+1];
}
else
{
largest = arr[pos];
}
pos++;
largestRec(arr, pos);
}
return largest; // replace this statement with your own return
}
Second method. What I'm trying to do is have it pass a smaller version of the string through a recursive method call and then when the test class calls the method, it will print out the reverse of the string.
// This method reads a string and returns the string in the reversed order.
public String reverseStringRec(String s)
{
// TODO: implement this method
String reverse;
int pos = 0;
if(s=="" || s.length() <= 1)
{
return s;
}
else
{
reverse = reverseStringRec(s.substring(1)) + s.charAt(0);
}
return reverse; // replace this statement with your own return
}
I'm not sure how to write the code to make it do that(for the assignment, I can only modify the methods, not outside variables/methods/classes allowed), so I'd appreciate any advice/help you can offer. If you need more info, I'll be glad to provide it. Thanks.
Edit: My problem is that the first method does not return the largest. For my test array, it usually prints the first int, or the second one(if it is larger than the first, but does not check the rest). For the second one, my test class(made by my professor), gives the message 'String index out of range" and I don't know how to fix that. I looked at Jason's suggestion, and put the suggested solution in, but it doesn't seem to be working for my case.
Edit2: The new version of reverseStringRec() now works. Now I need to fix largestRec(). Question is still open if anyone can offer any help.
Edit3: Although I fixed reverseStringRec(), someone gave an answer containing a for loop. I failed to mention that I cannot use loops for this assignment, so I apologize for the inconvenience. I'll put the output of the current largestRec() below if you need to see what results it yields as it is now.
Test 3: largest(10) ==> [Passed] Expected: 10 Yours: 10
Test 4: largest(10, 20, 30, 40, 50, 60) ==> [Failed] Expected: 60 Yours: 20
Test 5: largest(70, 20, 30, 40, 50, 10) ==> [Passed] Expected: 70 Yours: 70
Test 6: largest(70, 20, 100, 40, 50, 10) ==> [Failed] Expected: 100 Yours: 70
Edit4: Solution found for both methods. For largestRec(), please look at the solutions offered below. For reverseStringRec(), you can use the one in this post or one of the suggested ones below.