Question: Write a method called median that returns the median value in the array. If the length of the array is odd, the median is the

Write a method called median that returns the median value in the array.

If the length of the array is odd, the median is the value in the center of the array.

If the length of the array is even, the median is the average of the two numbers in the center.

You may assume the array passed into this method will never be empty.

Sample output:

The median value of the EVEN array is 19.5 The median value of the ODD array is 22.0

Hint: Weve imported java.util.* for you, so you have a handy static method on Arrays that you can use called Arrays.sortso you can sort your method BEFORE finding the median.

Code given below to change:

import java.util.*; public class Median { public static void main(String[] args) { int[] numbers1 = {12, 75, 3, 17, 65, 22}; System.out.print("The median value of the EVEN array is " + median(numbers1)); int[] numbers2 = {12, 75, 3, 17, 65, 22, 105}; System.out.print(" The median value of the ODD array is " + median(numbers2)); } public static double median(int[] arr) { // your code goes here! } }


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!