import java.util.Scanner; /** * This program calls rotateOneDown to move each array * element down one position and the last to the top. * * * * @author Dave Slemon * @version 1.02 */ public class TestProgram { public static void main() { Scanner input = new Scanner(System.in); int N; //make a long array long [] grp = new long[21]; //fill the array with squared values for(int i=0; i< grp.length; i++) grp[i] = i * i; //print the array System.out.println("ARRAY BEFORE ROTATING 1 ELEMENT"); printArray (grp); System.out.println("\n--------------------------------------------------------------------"); //call rotateOneDown method grp = rotateOneDown(grp); //print the array System.out.println("AFTER ROTATION"); printArray (grp); System.out.println("\n--------------------------------------------------------------------"); System.out.println("moveDown"); System.out.print("How many positions do you want moved? "); N = input.nextInt(); grp = moveDown(N, grp); //print the array System.out.println("AFTER MOVING DOWN "+ N + " POSITIONS"); printArray (grp); } /** * Outputs the array to the console */ public static void printArray( long[] x ) { for(int i=0; i< x.length; i++) { if (i % 10 == 0) System.out.println(); System.out.printf("%5d ", x[i]); } } /** * Rotates each element down one position. The * last element moves to the top */ public static long[ ] rotateOneDown( long[] x ) { long temp; long [ ] b = new long[x.length]; temp = x[x.length-1]; for(int i=1; i< x.length; i++) b[i] = x[i-1]; b[0] = temp; return b; } public static long[] moveDown( int N, long[] x ) { for(int i=0; i< N; i++) x = rotateOneDown( x ); return x; } }