In: Computer Science
For this question we will be using arrays and classes in Java to compute the min, max, and average value of items for a given array of integers. Complete the following using the base template provided below:
-Create methods for min, max, and average and call them from main to print out their values.
-Add a method to determine the median (http://www.mathsisfun.com/median.html) and print that value. This method is currently not in the template, so you will need to add and call it.
-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.
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
Code For Above Problem:
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
System.out.println("Average Value = " + getAvgValue(numbers));
// ADD CODE TO SORT THE NUMBERS IN THE ARRAY, PRINT THE SORTED ARRAY
// USED ALGORITM IS INSERTION SORT
for (int i = 1; i < numbers.length; i++) {
int key = numbers[i];
int j = i - 1;
while (j >= 0 && numbers[j] > key) {
numbers[j + 1] = numbers[j];
j = j - 1;
}
numbers[j + 1] = key;
}
// PRINTING SORTED ARRAY
System.out.println("Sorted array:");
System.out.println(Arrays.toString(numbers));
}
//Find maximum (largest) value in array using loop
public static int getMaxValue(int[] numbers) {
int maxValue = numbers[0];
try {
// ADD CODE TO ADD A LOOP, CHECK EACH ARRAY ELEMENT
// AGAINST THE CURRENT maxValue (USE AN IF STATEMENT)
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] > maxValue) {
maxValue = numbers[i];
}
}
} catch (Exception e) {
e.printStackTrace();
}
return maxValue;
}
// Find minimum (lowest) value in array using loop
public static int getMinValue(int[] numbers) {
int minValue = numbers[0];
try {
// ADD CODE TO ADD A LOOP, CHECK EACH ARRAY ELEMENT
// AGAINST THE CURRENT minValue (USE AN IF STATEMENT)
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] < minValue) {
minValue = numbers[i];
}
}
} catch (Exception e) {
e.printStackTrace();
}
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
double sum = 0;
// Used to store the average of all elements in an array
double average = 0;
try {
// ADD CODE TO ADD A LOOP TO COMPUTE A RUNNING TOTAL OF ALL ARRAY ELEMENTS
for (int i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
// ADD CODE TO COMPUTE THE AVERAGE
average = sum / numbers.length;
} catch (Exception e) {
e.printStackTrace();
}
return average;
}
} // Main
Sample Run Results:
Minimum Value = -9
Maximum Value = 89
Average Value = 13.4
Sorted array:
[-9, -6, -3, 1, 4, 5, 12, 18, 23, 89]
Images Of Code:
Image Of Sample Run Results: