Note: This is an introduction course for Java. Please be considerate. Problem :
ID: 3844523 • Letter: N
Question
Note: This is an introduction course for Java. Please be considerate.
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
Please do not use any advanced syntax. This is an introduction course. Thank you.
Explanation / Answer
Keeping in mind you are new to java, this is the code i have for you
import java.util.Random;
import java.util.Scanner;
/**
*
* @author Sam
*/
public class ArrayShift {
int []arr;
public void setArr(int size) { //create and populate array
arr = new int[size];
Random random = new Random();
for (int i = 0; i<size; i++)
arr[i] = random.nextInt(100);
}
void rotateLeft() { //rotate array left once
int first = arr[0]; //copy the 1st elemt
for (int i = 0; i < arr.length-1; i++) //shift left all element
arr[i]=arr[i+1];
arr[arr.length-1]=first; //replace last elemt
}
void rotateRight() { //rotate array right once
int last = arr[arr.length-1]; //strore last elemmt
for (int i = arr.length-1; i > 0; i--) //shift right
arr[i]=arr[i-1];
arr[0]=last; //replace first
}
String getContent() { //convert array to strig
String out ="";
for (int i=0; i<arr.length; i++)
out += arr[i] + " ";
return out;
}
public static void main(String[] args) {
ArrayShift as = new ArrayShift();
Scanner sc = new Scanner(System.in) ;
System.out.print("Enter the number of slots needs in the array: ");
int n = sc.nextInt(); //accept number
if (sc.hasNextLine()) //to consume extra character like new line feed
sc.nextLine();
as.setArr(n); //generate arry
System.out.println("This is element of your array: " + as.getContent()); //print content
System.out.print("Which direction to shift R/L : ");
char dir = sc.nextLine().charAt(0); //accept direction
System.out.print("How many times: "); //and accept no of rotation required
int times = sc.nextInt();
for (int i = 0; i < times; i++) //rotate accodingly
if (dir == 'R')
as.rotateRight();
else
as.rotateLeft();
System.out.println("This is element of your array: " +as.getContent()); // print final results
}
}
I have also commented the code for you. please let me know if are still facing difficulty understanding the code. I shall be glad to help you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.