Java Write a program that allows users to input an integer for the size of an ar
ID: 3855769 • Letter: J
Question
Java
Write a program that allows users to input an integer for the size of an array. Randomly generate an integer for each element of the array. Next, create the function to rotate the array . Rotation of the array means that each element is shifted right or left by one index, and the last element of the array is also moved to the first place.
For example:
Enter the number of slots needs in the array: 8
This is element of your array: 91 57 18 96 16 49 31 83
Which direction to shift R/L: R
How many times: 2
This is element of your array: 31 83 91 57 18 96 16 49
For example:
Enter the number of slots needs in the array: 3
This is element of your array: 31 83 91
Which direction to shift R/L: L
How many times: 2
This is element of your array: 91 31 83
Explanation / Answer
//Program
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Random;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner reader= new Scanner(System.in);
System.out.printf("Enter the number of slots needs in the array: ");
Scanner in = new Scanner(System.in);
int n = in.nextInt();
System.out.printf("Which direction to shift R/L: ");
char Choice = reader.next().charAt(0);
//declare array
Integer[] anArray;
anArray = new Integer[n];
Random r = new Random();
for(int i=0;i<anArray.length;i++)
{
anArray[i] = r.nextInt(100);
}
System.out.printf("How many times: ");
Integer shiftTimes = in.nextInt();
//print Array before shifting
System.out.println(" Before Shifting Array is : ");
for(int i=0;i<anArray.length;i++)
{
System.out.printf("%d ",anArray[i]);
}
if( Choice == 'R' || Choice == 'r')
{
for(int i = 0 ; i < shiftTimes; i++)
{
int last = anArray[anArray.length-1];
for(int j = anArray.length-2; j >= 0;j--)
{
anArray[j+1] = anArray[j];
}
anArray[0] = last;
}
}
if( Choice == 'L' || Choice == 'l')
{
for(int i = 0 ; i < shiftTimes; i++)
{
int first = anArray[0];
for(int j = 0; j < anArray.length-1;j++)
{
anArray[j] = anArray[j+1];
}
anArray[anArray.length-1] = first;
}
}
//print Array after shifting
System.out.printf(" After Shifting Array %d to %c is : ",shiftTimes,Choice);
for(int i=0;i<anArray.length;i++)
{
System.out.printf(" %d ",anArray[i]);
}
System.out.println(" ");
}
}
--------------------------------------------
//output
Enter the number of slots needs in the array: 8
Which direction to shift R/L: R
How many times: 2
Before Shifting Array is :
52 67 83 95 16 96 14 78
After Shifting Array 2 to R is :
14
78
52
67
83
95
16
96
-----------------------------
//output2
Enter the number of slots needs in the array: 8
Which direction to shift R/L: L
How many times: 2
Before Shifting Array is :
12 33 99 2 19 4 74 51
After Shifting Array 2 to L is :
99
2
19
4
74
51
12
33
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.