Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Assignment 7 - Java Using Template create methods for min, max, and average and call them from main to print out their values. Use IDEOne

Assignment 7 - Java

Using Template create methods for min, max, and average and call them from main to print out their values. Use IDEOne and make sure you select Java as your language. You can also use Java7, as that is just Java, Version 7. Also add a method to determine the median and print that value.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.

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

   // 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];  // 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   } // Main Class 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

=+4 How does one acquire a global mindset?

Answered: 1 week ago

Question

=+2 How can the effectiveness of global virtual teams be improved?

Answered: 1 week ago