CS174: OOP - Drills - Creating the reverse of an array
Developed by Professor Tralie and Professor Mongan.
Exercise Goals
The goals of this exercise are:- To do proper array indexing
- To use loops in concert with arrays
- To allocate and populate new arrays
getReverseArray
in ArrayUtils.java
to return a new array which is the reverse of a given array. For example, the array {0, 5, 2, 3} would turn into the array {3, 2, 5, 0}.
Enter your Ursinus netid before clicking run. This is not your ID number or your email. For example, my netid is ctralie
(non Ursinus students can simply enter their name to get this to run, but they won't get an e-mail record or any form of credit)
Netid |
ArrayUtils.java
public class ArrayUtils {
public static int[] getReverseArray(int[] arr) {
/** TODO: Fill in your code here to create the reverse array **/
return {};
}
public static void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
if (i < arr.length-1) {
System.out.print(",");
}
}
}
}
Tester.java
public class Tester {
public static void main(String[] args) {
int[] arr0 = {0, 5, 10, 0, 3, 4, 0, 1};
int[] arr0rev = ArrayUtils.getReverseArray(arr0);
int[] arr1 = {0, 0, 1, 1, 1, 1, 3};
int[] arr1rev = ArrayUtils.getReverseArray(arr1);
ArrayUtils.printArray(arr0rev);
System.out.print(".");
ArrayUtils.printArray(arr1rev);
}
}
Tester.main(null);