Programming Assignment 8
CS 12300-02 (TR)
Programming I: Java
Fall, 2019

This assignment is due Friday, December 6.

This assignment uses material from Sections 7.1, 7.2 and 7.3 of the textbook (pages 444-483). This assignment makes use of the files contained in this zip file.

In the zip file there is a file called RotateArray.java that contains the outlines of two methods,

   public static void rotateRight(int[] a, int n)
   public static int[] rotateLeft(int[] a, int n)

These are methods that you need to complete. When they are completed, you can compile and run the program TestRotate.java and it should produce output exactly like the file test_results.txt

The first method rotateRight() takes as its parameters a reference a to an integer array and a positive integer n and it "rotates" the contents of the array to the right n places. So each element a[i] of the array should be moved to location a[i+n]. If the index i+n is past the end of the array (that is, i+n >= a.length), then that index should "wrap" back to the beginning of the array.

For example, if the input array is a = {1, 2, 3, 4, 5}, then rotateRight(a, 2) should modify a to contain {4, 5, 1, 2, 3}.

Notice that rotateRight() is a void method since it does not return anything. The job of this method is to modify the input array. Since the method has a reference to the array, any changes this method makes to the array are noticed by the method's caller (you should draw a picture to see why this is true).

Write this method as a two step process. The first step is to copy the elements of the input array into a new "temporary" array. After everything from the input array has been copied to the temporary array, then the second step is to copy the contents of the temporary array back into the input array but put into their shifted positions.

The second method rotateLeft() takes in a reference a to an integer array and a positive integer n and it returns a new array whose contents is the contents of the input array rotated to the left n places. So each element a[i] of the input array should be placed at location b[i-n] of the returned array. If the index i-n is negative, then that index should "wrap" around to the end of the output array.

For example, if the input array is a = {1, 2, 3, 4, 5}, then rotateLeft(a, 2) should return an array containing {3, 4, 5, 1, 2}.

This method should not make any changes to the input array.

Turn in a zip file called CS123Hw8Surname.zip (where Surname is your last name) containing your version of RotateArray.java.

This assignment is due Friday, December 6.