fork download
/********************Java Code************/

/******************Main.java****************/
import java.util.Arrays;

public 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 ...

		System.out.println("Average Value = " + getAvgValue(numbers));

		// Add any challenge method calls here ....

		// ADD CODE TO SORT THE NUMBERS IN THE ARRAY, PRINT THE SORTED ARRAY
		Arrays.sort(numbers);

		System.out.println("Array after sorting");
		for (int i = 0; i < numbers.length; i++) {
			System.out.print(numbers[i]+ " ");
		}
		// 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.
		// using sorted array calling median
		System.out.println("\nMedian Value = " + getMedian(numbers));

	}

	// 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)
		try {
			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
		} catch (ArrayIndexOutOfBoundsException e) { //catching ArrayIndexOutOfBoundsException exception
			System.out.println("Array Index out of bound exception"+e.getMessage());
		}
		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
		try {
			for (int i = 1; i < numbers.length; i++) {   
				if (numbers[i] < minValue) { // is the current element less than the current min value?
					minValue = numbers[i]; // the current element is now the min value
				} // end if
			} // end for

		} catch (ArrayIndexOutOfBoundsException e) {  //catching ArrayIndexOutOfBoundsException exception
			System.out.println("Array Index out of bound exception"+e.getMessage());
		}

		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)

		try {
			for (int i = 1; i < numbers.length; i++) {   
				sum = sum + numbers[i]; // add the current element value into the sum
			} // end for

			average = sum/numbers.length; //to find average divide total sum with total elements
		} catch (ArithmeticException e) {  //catching ArithmeticException exception
			System.out.println("Dividing by zero exception");
		} catch (ArrayIndexOutOfBoundsException e) {  //catching ArrayIndexOutOfBoundsException exception
			System.out.println("Array Index out of bound exception"+e.getMessage());
		}

		return average;

	} // getAvgValue

	// Add any Challenge methods here ...

	public static double getMedian(int[] numbers) {
		//this will hold median value
		double median = 0;
		//assuming numbers array will come in sorted order then only this method works
		try {
			//middle index of array
			int middleIndex = numbers.length/2;

			if(numbers.length% 2 == 1) { //checking if length of array is odd
				//set median as middleIndex value
				median = numbers[middleIndex];
			} else { //if length of array is even
				//set median as sum of middle and middle + 1 index divide by 2
				median = ((numbers[middleIndex] + numbers[middleIndex + 1]) /2);
			}
		} catch (ArithmeticException e) { //catching ArithmeticException exception
			System.out.println("Dividing by zero exception");
		} catch (ArrayIndexOutOfBoundsException e) { //catching ArrayIndexOutOfBoundsException exception
			System.out.println("Array Index out of bound exception"+e.getMessage());
		}

		return median;
	}

} // Main Class
Success #stdin #stdout 0.15s 52612KB
stdin
Standard input is empty
stdout
Minimum Value = -9
Maximum Value = 89
Average Value = 13.3
Array after sorting
-9 -6 -3 1 4 5 12 18 23 89 
Median Value = 8.0