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.sort
so 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started