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

Please write this Java program as simple as possible and with comments throughou

ID: 3730205 • Letter: P

Question

Please write this Java program as simple as possible and with comments throughout explaining what is happening. Thank you!
Exercise 5: Design and implement a Java program (name it ArrayMethods), that defines 4 methods as follows: int arrayMax (inttl arr) detemines and returns the largest value within an array int arrayMin(int[l arr) determines and returns the smallest value within an array void arrayReverse (int [ arr) reverses the array (for example: 7812 becomes 2 1 87) void arraySquared (intl arr) changes every value within the array to value* Test your methods by creating an array of length 5 within your main () method and filling it with random integers between 1 and 1000. Your program should then invoke the methods in the order listed above to display the original array, the largest number in the array, the smallest number in the array, the reversed array, and the square of each value in the array. Your main () method should invoke each method exactly once, with each invocation use the original array (modified by a previous invocation of a method, if the array was modified by a method) as the actual parameter. All methods must be designed to handle arrays with any number of elements. Use only one array in your program All printing must be done by the main) method. Document your code and organize the output using appropriate formatting techniques.

Explanation / Answer


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

public class ArrayMethods {

//arrayMax method to find the maximum element in the array
public static int arrayMax(int[] arr) {
int max = arr[0];//inititalize the first value to max
for (int i = 1; i < arr.length; i++) {//performs until the last element
if (arr[i] > max) {//if array element is >max element then store in max
max = arr[i];//max stores the greatest value
}
}
return max;//returns the max value
}

public static int arrayMin(int[] arr) {
//arrayMin method to find the maximum element in the array
int min = arr[0];//inititalize the first value to min
for (int i = 1; i < arr.length; i++) {//performs until the last element
if (arr[i] < min) {//if array element is <min element then store in min
min = arr[i];//min stores the minimum value
}
}
return min;//returns the minimum
}

public static void arrayReverse(int[] arr) {
int j = arr.length - 1; // last element
int i = 0; // first element
int temp;
//interchanges the element in the array
while (i < j) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}

}

//arraySquared method squares the value of the array element
public static void arraySquared(int[] arr) {
for (int i = 0; i < arr.length; i++) {
arr[i] = arr[i] * arr[i];
}
}

public static void main(String args[]) {
int m;
Random rand = new Random();
Scanner sc = new Scanner(System.in);

//prompt the user for number of elements
System.out.print(" Enter the number of elements to sort :");
m = sc.nextInt();

//creates the array location and stores the random number in array
int[] arr = new int[m];
for (int i = 0; i < m; i++) {
arr[i] = rand.nextInt(1000); // generate random number
}

//prints the original array
System.out.println(" Elements of the array are :");
for (int i = 0; i < m; i++) {
System.out.print(" " + arr[i]);
}

//calls arrayMax and prints the maximum number
System.out.print(" Maximum value is :" + arrayMax(arr));

//calls arrayMin and prints the minimum number
System.out.print(" Minimum value is :" + arrayMin(arr));

//calls arrayReverse to reverse the array and print the array
arrayReverse(arr);
System.out.print(" Reverse of Array : ");
for (int i = 0; i < m; i++) {
System.out.print(" " + arr[i]);
}

// calls arraySquared and prints the array value squared
arraySquared(arr);
System.out.print(" Squared Array : ");
for (int i = 0; i < m; i++) {
System.out.print(" " + arr[i]);
}
}

run:

Enter the number of elements to sort :5

Elements of the array are :

862 851 157 629 205

Maximum value is :862

Minimum value is :157

Reverse of Array :

205 629 157 851 862

Squared Array :

42025 395641 24649 724201 743044BUILD SUCCESSFUL (total time: 3 seconds)


}

Sample run:

Enter the number of elements to sort :5

Elements of the array are :

862 851 157 629 205

Maximum value is :862

Minimum value is :157

Reverse of Array :

205 629 157 851 862

Squared Array :

42025 395641 24649 724201 743044

BUILD SUCCESSFUL (total time: 3 seconds)