Question
Questions: Java Rotate Right Do a modified version of Chapter 7 Practice Program 3: Create and test a class RotateRight with a method rotateRight() that
Questions: Java Rotate Right
Do a modified version of Chapter 7 Practice Program 3: Create and test a class RotateRight with a method rotateRight() that takes an intarray and rotates the contents of the array to the right in place (that is, the input array is changed) by the number of places passed as a parameter.
The rotateRight() method header is:
public static void rotateRight(int[] a, int places)
Numbers that fall off the right should cycle back to the left. For example, if the input array numbers is { 1, 3, 5, 7 } and the call of rotateRight() is rotateRight(numbers, 2) then the numbers array should contain { 5, 7, 1, 3 } after that call.
Also write or copy a display() method to print the elements in an int array, and use it in main to test rotateRight() by displaying the elements of various arrays before and after calling the method (make it clear which is before and which is after, and what you expect the result to be). The display() method should print the header String, if any, and then print the elements of the incoming array across the output line with one space between each element.
The display() method header is:
public static void display(String header, int[] a)
Hints:
If the length of the input array is 0 or 1 just return immediately (in that case the order of the array elements will not change).
Set places = places % a.length; If places is now 0, just return (rotating multiples of the length of the array, including rotating 0 places, does not change it).
If places is negative, set places = a.length + places; to turn it into a positive rotation. In the above numbers example, rotating right -1 positions is the same as rotating right +3.
Create a new int array b the same length as a, and in a for loop copy the elements of a, starting at 0, into the b array:
b[places] = a[i]; // i is the for loop index places = (places + 1) % a.length; // end of for loop - this cycles back to the start
When this loop ends, run another for loop to copy all the elements in b back into a. Now as elements have been rotated.
Some possible tests:
{ 1, 3, 5, 7 } 2 produces { 5, 7, 1, 3 } - example above
{ 1, 3, 5, 7} 18 produces { 5, 7, 1, 3 } - 18 % 4 == 2
{ 1, 3, 5, 7 } -1 produces { 3, 5, 7, 1 } - 4 + -1 == 3
{ 1, 3, 5, 7 } 8 produces { 1, 3, 5, 7 } - 8 % 4 == 0
{ 1 } 2 produces { 1 } - places is ignored
Template:
public class RotateRight { // Rotate an int array right by a given number of places public static void rotateRight(int[] a, int places) { /* write the rotateRight method below this comment */ } private static void display(String header, int[] a) { /* write the display method below this comment */ } public static void main(String[] args) { // you can add additional tests here if you want ... /* DO NOT MODIFY MAIN BELOW THIS COMMENT */ int[] a1 = { 1, 3, 5, 7 }; display("array before rotation:", a1); rotateRight(a1, 2); System.out.println("element order should be: 5 7 1 3"); display("array after rotating 2:", a1); System.out.println(); int[] a2 = { 1, 3, 5, 7 }; display("array before rotation:", a2); rotateRight(a2, 18); System.out.println("element order should now be: 5 7 1 3"); display("array after rotating 18:", a2); System.out.println(); int[] a3 = { 1, 3, 5, 7 }; display("array before rotation:", a3); rotateRight(a3, -1); System.out.println("element order should now be: 3 5 7 1"); display("array after rotating -1:", a3); System.out.println(); int[] a4 = { 1, 3, 5, 7 }; display("array before rotation:", a4); rotateRight(a4, 0); System.out.println("element order should now be: 1 3 5 7"); display("array after rotating 0:", a4); System.out.println(); int[] a5 = { 1 }; display("array before rotation:", a5); rotateRight(a5, 2); System.out.println("element order should now be: 1"); display("array after rotating 2:", a5); System.out.println(); } }
Output should look like this:
array before rotation: 1 3 5 7
element order should be: 5 7 1 3
array after rotating 2: 5 7 1 3
array before rotation: 1 3 5 7
element order should now be: 5 7 1 3
array after rotating 18: 5 7 1 3
array before rotation: 1 3 5 7
element order should now be: 3 5 7 1
array after rotating -1: 3 5 7 1
array before rotation: 1 3 5 7
element order should now be: 1 3 5 7
array after rotating 0: 1 3 5 7
array before rotation: 1
element order should now be: 1
array after rotating 2: 1
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started