write a write a write a write a write a Chapter 9 - Write a program that prompts
ID: 3709387 • Letter: W
Question
write a write a write a write a write a Chapter 9 - Write a program that prompts the user to enter the size an array is to be, then declare the array using this size (see page 557). The minimum size of the array should be 5 elements. Next, prompt the user to input enough numbers to exactly fill the array. The prompt should specify the number of numbers needed. Example: "Please enter 5 numbers: I suggest you use prompts displayed at the bottom of the jGrasp window to get user input, rather than using input boxes. Then, process the array in the following ways: Read the numbers into the array Print the array Find and print the sum and the average of the array Print the largest and smallest elements of the array . See pages 560-61 for examples of the code needed to complete this program. In addition to the code, document your program by inserting comments before each section of code that describe what that section of code accomplishes. Due Wednesday, April 18.Explanation / Answer
ArrayOperationsTest.java
import java.util.Scanner;
public class ArrayOperationsTest
{
public int getTotal(int[] array){
int sum=0;
for(int i=0; i<array.length; i++)
{sum+=array[i];}
return sum;}
public double getAverage(int[] array1){
double average=getTotal(array1)/array1.length;
return average;
}
public int getHighest(int[] array2){
int max = array2[0];
for(int i=0; i<array2.length; i++){
if(max < array2[i]){
max = array2[i];
}
}
return max;
}
public int getLowest(int[] array2){
int min = array2[0];
for(int i=0; i<array2.length; i++){
if(min > array2[i]){
min = array2[i];
}
}
return min;
}
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter the size of an array: ");
int n = scan.nextInt();
int arr[] = new int[n];
for(int i=0; i<arr.length; i++){
System.out.println("Enter the value "+(i+1)+": ");
arr[i] = scan.nextInt();
}
ArrayOperationsTest obj = new ArrayOperationsTest();
System.out.println("Sum: "+obj.getTotal(arr));
System.out.println("Average: "+obj.getAverage(arr));
System.out.println("Highest value: "+obj.getHighest(arr));
System.out.println("Lowest value: "+obj.getLowest(arr));
}
}
Output:
Enter the size of an array:
10
Enter the value 1:
3
Enter the value 2:
4
Enter the value 3:
5
Enter the value 4:
1
Enter the value 5:
2
Enter the value 6:
9
Enter the value 7:
10
Enter the value 8:
8
Enter the value 9:
7
Enter the value 10:
6
Sum: 55.0
Average: 5.5
Highest value: 10
Lowest value: 1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.