Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN JAVA PROGRAMMING!! Problem : Write a program that allows users to input an in

ID: 3776012 • Letter: I

Question

IN JAVA PROGRAMMING!!

Problem : 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 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

import java.util.Random;
import java.util.Scanner;


public class ArrayRotate {

   public static void main(String[] args) {
       int size;
       Scanner scan= new Scanner(System.in);
       System.out.println("Enter the size of the array");
       size=scan.nextInt();//taking the size from the user
       int arr[]= new int[size];
       Random rand = new Random();//to generate random number
       for(int i=0;i<size;i++){
           arr[i]=rand.nextInt(100);//random number from 0-99
       }
       printArray(arr);
       System.out.println("Which direction to shift R/L :");
       String direction=scan.next();
       System.out.println("How many times:");
       int time=scan.nextInt();
       //switch case to determine the direction
       switch(direction){
       case "L": leftRotate(arr,time);
           break;
       case "R": RightRotate(arr,time);
           break;
       default: System.out.println("Invalid choose R/L ");
           break;
       }
       System.out.println("After "+direction+" Rotation of "+time+". The new array is");
       printArray(arr);
      
   }
   //Utility method to print the array
   private static void printArray(int[] arr) {
       for(int i=0;i<arr.length;i++){
           System.out.print(arr[i]+" ");
       }
      
   }
   //method to do left shifting
   static void leftRotate(int arr[], int ntime)
{
int i;
for (i = 0; i < ntime; i++)
leftRotatebyOne(arr);
}

static void leftRotatebyOne(int arr[])
{
int i, temp;
temp = arr[0];
for (i = 0; i < arr.length - 1; i++)
arr[i] = arr[i + 1];
arr[i] = temp;
}
//method to do right shifting
static void RightRotate(int arr[], int ntime)
{
int i;
for (i = 0; i < ntime; i++)
   RightRotatebyOne(arr);
}

static void RightRotatebyOne(int arr[])
{
int i, temp;
temp = arr[arr.length-1];
for (i = arr.length-1; i >0; i--)
{  
   arr[i] = arr[i - 1];
  
}
arr[0] = temp;
}

}

--------------output------------------
Enter the size of the array
8
45 34 98 69 23 11 57 17 Which direction to shift R/L :
R
How many times:
2
After R Rotation of 2. The new array is
57 17 45 34 98 69 23 11

Enter the size of the array
8
23 48 6 62 93 26 37 89 Which direction to shift R/L :
L
How many times:
2
After L Rotation of 2. The new array is
6 62 93 26 37 89 23 48

//Note: Please feel free to ask doubts. God bless you!!