1.Write a Java program to solve the following problem using modularity. Write a method that rotates a one-dimensional array with one position to the right in such a way that the last element becomes the first element. Your program will invoke methods to initialize the array (random values between 1 and 15) print the array before the rotation, rotate the array, and then print the array after rotation. Use dynamic arrays and ask the user for the array size. Write your program so that it will generate output similar to the sample output below:

Respuesta :

Answer:

Explanation:

The following code is written in Java and it asks the user for the size of the array. Then it randomly populates the array and prints it. Next, it rotates all the elements to the right by 1 and prints the new rotated array.

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Random;

import java.util.Scanner;

class Brainly {

   public static void main(String[] args) {

       Random r = new Random();

       Scanner in = new Scanner(System.in);

       System.out.println("Enter Size of the Array: ");

       int arraySize = in.nextInt();

       ArrayList<Integer> myList = new ArrayList<>();

       for (int x = 0; x < arraySize; x++) {

           myList.add(r.nextInt(15));

       }

       System.out.println("List Before Rotation : " + Arrays.toString(myList.toArray()));

       for (int i = 0; i < 1; i++) {

           int temp = myList.get(myList.size()-1);

           for (int j = myList.size()-1; j > 0; j--) {

               myList.set(j, myList.get(j - 1));

           }

           myList.set(0, temp);

       }

       System.out.println("List After Rotation :  " + Arrays.toString(myList.toArray()));

   }

}

Ver imagen sandlee09