Question: Assignment 7 - Java Create methods for min, max, and average and call them from main to print out their values. It contains a main
Assignment 7 - Java
Create methods for min, max, and average and call them from main to print out their values. It contains a main function that calls three methods to figure out the min, max, and average of an array of integers. Add a method to determine the median and print that value. Add a try/catch exception handler to each method to make sure you don't divide by zero or use an out of bounds array index.
See Template Below
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));
// Determine the average of all element values in array using loop
// ADD CODE TO CALL getAvgValue AND PRINT THE AVERAGE
// ADD CODE TO SORT THE NUMBERS IN THE ARRAY, PRINT THE SORTED ARRAY
}
//Find maximum (largest) value in array using loop
public static int getMaxValue(int[] numbers){
int maxValue = numbers[0];
// ADD CODE TO ADD A LOOP, CHECK EACH ARRAY ELEMENT
// AGAINST THE CURRENT maxValue (USE AN IF STATEMENT)
return maxValue;
}
//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)
return minValue;
}
//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
// Used to store the average of all elements in an array
double average = 0;
// ADD CODE TO ADD A LOOP TO COMPUTE A RUNNING TOTAL OF ALL ARRAY ELEMENTS
// ADD CODE TO COMPUTE THE AVERAGE
return average;
}
} // Main
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
