Question: I NEED TO FILL IN THE 'ADD CODE' SECTIONS. HELP! import java.util.Arrays; class Main { public static void main (String args[]) { int numbers[]= {1,5,-9,12,-3,89,

I NEED TO FILL IN THE 'ADD CODE' SECTIONS. HELP!

import java.util.Arrays; class Main { public static void main (String args[]) { int numbers[]= {1,5,-9,12,-3,89, 18,23,4,-6}; // Find minimum (lowest) value in array using loop System.out.println("Minimum Value = " + getMinValue(numbers)); // Find maximum (largest) value in array using loop System.out.println("Maximum Value = " + getMaxValue(numbers)); // ADD CODE TO CALL getAvgValue AND PRINT THE AVERAGE // Don't forget to call the avg method below ... call it just like // the two methods above ... // Add any challenge method calls here .... // ADD CODE TO SORT THE NUMBERS IN THE ARRAY, PRINT THE SORTED ARRAY // Use the built-in array sort method on the array // ... then add a loop here (no need to call a method) to print // the now sorted array. See our lecture notes for examples.

}

// Find maximum (largest) value in array using loop

public static int getMaxValue(int[] numbers) {

int maxValue = numbers[0]; // set the first array element (index of 0) as the max // ... remember that arrays start with an index of 0 // ADD CODE TO ADD A LOOP, CHECK EACH ARRAY ELEMENT // AGAINST THE CURRENT maxValue (USE AN IF STATEMENT)

// The key is to use a loop with the length property of the array // to access and check every element after the first element. The if statement // each time will check the current max value against the current // array value. At the end of the loop, maxValue will contain // the largest number in the array. // Access each array element starting with the second element (i = 1, not 0)

for (int i = 1; i < numbers.length; i++) { if (numbers[i] > maxValue) { // is the current element greater than the current max value? maxValue = numbers[i]; // the current element is now the max value } // end if } // end for return maxValue; // return the largest array element value

} // getMaxValue // Find minimum (lowest) value in array using loop

public static int getMinValue(int[] numbers) {

int minValue = numbers[0];

// ADD CODE TO ADD A LOOP, CHECK EACH ARRAY ELEMENT // AGAINST THE CURRENT minValue (USE AN IF STATEMENT)

// Hint: This is just like the for loop in the max function above, just revise to check for min value instead of max value

return minValue;

} // getMinValue

// Find the average of an array of integers

public static double getAvgValue(int[] numbers) {

// ADD CODE TO SET UP NEEDED VARIABLES, ONE TO SUM ALL ARRAY ELEMENT VALUES, AND // AND ANOTHER ONE, average, TO BE USED TO CALCULATE THE AVERAGE OF ALL ARRAY ELEMENT VALUES

double average = 0; double sum = 0;

// ADD CODE TO ADD A LOOP TO COMPUTE A RUNNING TOTAL OF ALL ARRAY ELEMENTS

// use a loop like in the max function, but instead of an if, add the current // array element value to sum. This will keep a running total as each element value is added // to sum as it is accessed in the loop: sum = sum + numbers[i] ;

// ADD CODE TO COMPUTE THE AVERAGE

// The average just takes the sum variable and divides it by the total number of array items (i.e., numbers.length)

return average;

} // getAvgValue

// Add any Challenge methods here ...

} // Main Class

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!