import java.util.Scanner;
public class RotateArray
{
// Create a scanner to read from keyboard
Scanner scanner = new Scanner (System.in);
/**
* Main method to allow execution from the command line
*/
public static void main(String[] args)
{
RotateArray instance = new RotateArray(); // default constructor
instance.start();
}
/**
* declare the arrays, prompt the user for the amount of times to rotate, rotate
* the arrays, and finally print them out.
*/
public void start()
{
//*******************************Part 1***********************************
char[] arrayToRotate = {
'#', 'G', 'N', 'U', 'A', 'H', 'O', 'V', 'B', 'I', 'P', 'W', 'C', 'J',
'O', 'X', 'D', 'K', 'R', 'Y', 'E', 'L', 'S', 'Z', 'F', 'M', 'T'
};
//prompt for the amount to rotate
System.out.println("ROTATES TO THE LEFT, NOT TO THE RIGHT");
System.out.println("Enter an amount by which to rotate the array");
int amount = scanner.nextInt();
System.out.println();
System.out.println("Original array is:");
printArray(arrayToRotate);
System.out.println("For 2 points, rotated array is:");
rotateArray(arrayToRotate, amount);
printArray(arrayToRotate);
System.out.println();
}// end start()
/**
* rotates the elements in an array amount times to the right. The last
* element wraps around to the first element
*/
public void rotateArray(char[] array, int amount)
{
for( int j=0; j<amount; j++) {
char a = array[0];
int i;
for(i = 0; i < array.length-1; i++)
array[i] = array[i+1];
array[i]= a;
}
}//end rotateArray()
/**
* send this method an array and it will print it out like A B C D...
*/
public void printArray(char[] array) {
//loop through the array
for(int x=0; x<array.length; x++) {
//display each index
System.out.print(array[x] + " ");
}
System.out.println();
}
} |