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
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
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