Question: 1.14 StdArray.reverse Create a generic version of the reverse() function you wrote in assignment 2 (lab 1.10), in a class called StdArray. As with lab
1.14 StdArray.reverse
Create a generic version of the reverse() function you wrote in assignment 2 (lab 1.10), in a class called StdArray. As with lab 1.10, you need to supply two versions, one that takes 1 argument (just the array), and one that takes 3 (the array and a [lo,hi) range). As a reminder, here are the signatures of the non-generic functions:
public static void reverse(int[] a) public static void reverse(int[] a, int lo, int hi)
Remember, you need to change the signatures to make the functions generic. I've posted the StdArray class we worked on in class to D2L, in case you need a reminder of what changes need to be made.
I've provided a template that contains a main() function which does some minimal testing.
The template also contains a generic version of swap() for you to use if you want.
public class StdArray {
// Your reverse() functions go here.
public static
T t = a[idx1];
a[idx1] = a[idx2];
a[idx2] = t;
}
public static void main(String[] args) {
Integer[] a = { 1, 11, 21, 1211, 111221, 312211 };
Integer[] result = { 1, 1211, 21, 11, 111221, 312211 };
reverse(a, 1, 4);
for (int i = 0; i < a.length; i++)
if (!a[i].equals(result[i]))
StdOut.printf("Error at index %d; expected %d, found %d ", i, result[i], a[i]);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
