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

1. Define a Java method named distribute) that takes two arguments: an array of

ID: 3590390 • Letter: 1

Question

1. Define a Java method named distribute) that takes two arguments: an array of positive integer values and an integer value indicating a valid index within that array. public static void distribute (int [] values, int start_index) This method does the following: Saves the value contained in the specified array index into a temporary variable (call it count for the purposes of this description) Sets the value at index count to 0 Starting at the next index and wrapping around if necessary, adds 1 to each subsequent array value until count elements have been updated The distribute() method does not return any value; instead, it directly modifies its array parameter. For example, consider the initial array [ 3, 1, 6, 2, 4, 1, 5 ], which has a length of 7. Our starting index is 3, which holds the value 2. We will set the value of index 3 to 0, and add 1 to each of the next 2 array values, to get the final result [ 3, 1, 6, 0, 5, 2, 5 If we had a sufficiently large starting value, we would "wrap around" the end of the array to continue incrementing the array values. For example, if our starting index had been 2 (which holds the value 6) instead, we would have incremented indices 3, 4, 5, 6, 0, and 1, in that order, to get [4, 2, 0, 3, 5, 2, 61 Write a small Java program that uses Math.random() to generate an array of 15 random integers in the range 1-13. Your program should display the starting array, read in a starting index from the user, call distribute() with your array and the user input, and then print the modified array.

Explanation / Answer

//java program for creating random numbers and modify the array

import java.util.Scanner;

public class Randomnos {

// to display the value in the array
public static void print(int value[]) {
for (int i = 0; i < 15; i++) {
System.out.print(value[i] + " ");
}
}

// to modify the array values
public static void distribute(int[] value, int start_index) {
value[start_index] = 0;
//checks whether start_index in the range
if ((start_index > 0) && (start_index < 16)) {

//performs the loop until all the array values are updated
for (int count = 0; count < 14; count++) {
start_index++;
if (start_index == 15) {
start_index = 0;
}
  
value[start_index] = value[start_index] + 1;
  
}

} else {
System.out.println("The index number invalid");
}

}

public static void main(String[] args) {

int value[] = new int[15];
int index;
Scanner in = new Scanner(System.in);
  
System.out.println("An array of 15 elements :");
//creates the random nos from 1 to 13 and stores 15 elements in value array

for (int i = 0; i < 15; i++) {
value[i] = (int)(Math.random()*13);
}
  
// displays the array
print(value);
  
//reads the index from the user
System.out.print(" Enter the index :");
index = in.nextInt();
  
// calls the procedure to modify the array
distribute(value, index);
  
//displays the modified array
System.out.println("Modified Array :");
print(value);

}

}
Sample run:
run:
An array of 15 elements :
6 6 12 9 8 10 4 12 6 8 12 10 3 2 3  
Enter the index :3
Modified Array :
7 7 13 0 9 11 5 13 7 9 13 11 4 3 4 BUILD SUCCESSFUL (total time: 7 seconds)