Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Recommended Textbook for

Securing SQL Server Protecting Your Database From Attackers

Authors: Denny Cherry

2nd Edition

1597499471, 978-1597499477

More Books

Students also viewed these Databases questions

Question

Describe three other types of visual aids.

Answered: 1 week ago