Define a method for each array processing (e.g. arraySum, arrayMax, and etc.) in
ID: 3708407 • Letter: D
Question
Define a method for each array processing (e.g. arraySum, arrayMax, and etc.) in the starter file ArrayProcessing.java. Replace the code in the main method (from 7 to 13, except for 10) into method invocations.
public class ArrayProcessing {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter the size of the array to be created: ");
int size = input.nextInt();
//(1) Question to ponder: Can you create an array that can store data of several different data types?
//Answer: No.
//(2) Create an int array (name it myList) that has the size the user just entered.
int[] myList = new int[size];
//(3) Question to ponder: Once an array is created, can its size be changed?
//Answer: No.
//(4) Fix the error below
myList[0] = 5;
//(5) Replace 0 below with the first array element
System.out.println("The first array element is " + myList[0]);
//(6) Fix the error below
System.out.println("The last array element is " + myList[size-1]);
System.out.println("The last array element is " + myList[myList.length-1]);
//(7) Write a loop to get user enter values that will be stored in the array
System.out.print("Enter " + myList.length + " values: ");
for (int i = 0; i < myList.length; i++)
myList[i] = input.nextInt();
//(8) Write a loop to print all the values stored in the array
for (int i = 0; i < myList.length; i++)
System.out.print(myList[i] + " ");
System.out.println();
//(9) Write a loop to calculate the sum of all array elements.
// Then print the sum after the loop
int total = 0;
for (int i = 0; i < myList.length; i++) {
total += myList[i];
}
System.out.println("total = " + total);
//(10) Calculate and print the average of all the array elements
double average = (double) total/myList.length;
System.out.println("average = " + average);
//(11) Write a loop before to calculate the maximum value among all array elements.
// Then print the maximum after the loop
int max = myList[0];
for (int i = 1; i < myList.length; i++) {
if (myList[i] > max)
max = myList[i];
}
System.out.println("max = " + max);
//(12) Write a loop before to calculate the minimum value among all array elements.
// Then print the minimum after the loop
int min = myList[0];
for (int i = 1; i < myList.length; i++) {
if (myList[i] < min)
min = myList[i];
}
System.out.println("min = " + min);
//(13) Write a loop to left shift the array
int temp = myList[0];
for (int i=0; i <= myList.length - 2; i++)
myList[i] = myList[i+1];
myList[myList.length-1] = temp;
System.out.println("After left shift: ");
//(14) Print the array again
for (int i = 0; i < myList.length; i++)
System.out.print(myList[i] + " ");
System.out.println();
} //end of main
} //end of class ArrayProcessing
Explanation / Answer
ArrayProcessing .java
import java.util.Scanner;
public class ArrayProcessing {
public static void main(String[] args) {
Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter the size of the array to be created: ");
int size = input.nextInt();
// (1) Question to ponder: Can you create an array that can store data of
// several different data types?
// Answer: No.
// (2) Create an int array (name it myList) that has the size the user just
// entered.
int[] myList = new int[size];
// (3) Question to ponder: Once an array is created, can its size be changed?
// Answer: No.
// (4) Fix the error below
myList[0] = 5;
// (5) Replace 0 below with the first array element
System.out.println("The first array element is " + myList[0]);
// (6) Fix the error below
System.out.println("The last array element is " + myList[size - 1]);
System.out.println("The last array element is " + myList[myList.length - 1]);
// 7
fillArray(myList, input);
//8 // Print Array
printArray(myList);
//9
double sum = arraySum(myList);
System.out.println("total = " + sum);
// (10) Calculate and print the average of all the array elements
double average = (double) sum / myList.length;
System.out.println("average = " + average);
//11
int max = findMax(myList);
System.out.println("max = " + max);
//12
int min = findMin(myList);
System.out.println("min = " + min);
//13
leftShift(myList);
System.out.println("After left shift: ");
// (14) Print the array again
for (int i = 0; i < myList.length; i++)
System.out.print(myList[i] + " ");
System.out.println();
} // end of main
private static void leftShift(int[] myList) {
int temp = myList[0];
for (int i = 0; i <= myList.length - 2; i++)
myList[i] = myList[i + 1];
myList[myList.length - 1] = temp;
}
// Find minimum
private static int findMin(int[] myList) {
int min = myList[0];
for (int i = 1; i < myList.length; i++) {
if (myList[i] < min)
min = myList[i];
}
return min;
}
// Method to calculate Max
private static int findMax(int[] myList) {
int max = myList[0];
for (int i = 1; i < myList.length; i++) {
if (myList[i] > max)
max = myList[i];
}
return max;
}
// Calculate Sum
private static double arraySum(int[] myList) {
// (9) Write a loop to calculate the sum of all array elements.
// Then print the sum after the loop
int total = 0;
for (int i = 0; i < myList.length; i++) {
total += myList[i];
}
return total;
}
// This method will be used to print the array elements
private static void printArray(int[] myList) {
// (8) Write a loop to print all the values stored in the array
for (int i = 0; i < myList.length; i++)
System.out.print(myList[i] + " ");
System.out.println();
}
private static void fillArray(int[] myList, Scanner input) {
// (7) Write a loop to get user enter values that will be stored in the array
System.out.print("Enter " + myList.length + " values: ");
for (int i = 0; i < myList.length; i++)
myList[i] = input.nextInt();
}
} // end of class ArrayProcessing
Output:
Enter the size of the array to be created: 5
The first array element is 5
The last array element is 0
The last array element is 0
Enter 5 values: 40
25
41
20
10
40 25 41 20 10
total = 136.0
average = 27.2
max = 41
min = 10
After left shift:
25 41 20 10 40
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.