Question
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
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, 6 ].
Write a small Java program that uses Math.random() to generate an array of 15 random integers in the range 113. 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.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started