get median in java

get median in java

How to get the median value in Java from the given list.

View Answers

February 16, 2012 at 12:29 PM

import java.util.*;
class FindMedian {

public static int findMedian(int array[]) {
int length=array.length;
int[] sort = new int[length];
System.arraycopy(array, 0, sort, 0, sort.length);
Arrays.sort(sort);

if (length % 2 == 0) {
return (sort[(sort.length / 2) - 1] + sort[sort.length / 2]) / 2;
} else {
return sort[sort.length / 2];
}
}

public static void main(String[] args)
{
int min = 30;
int max = 50;
int[] ranNum = new int[5];
System.out.println("Enter Numbers:");
for ( int i = 0; i < ranNum.length; i++) {
ranNum[i] = (int)(Math.random() * (max - min + 1) ) + min;
System.out.println(ranNum[i]);
}
int median=Find.findMedian(ranNum);
System.out.println("Median= "+median);
}
}









Related Tutorials/Questions & Answers:
get median in java
get median in java  How to get the median value in Java from the given list.   import java.util.*; class FindMedian { public static int... median=Find.findMedian(ranNum); System.out.println("Median= "+median
Mean, Median and Mode - Java Beginners
Mean, Median and Mode  Write a java program that computes three measures of central tendency of a given set of students marks: mean, median, and mode...); System.out.println("Mean= "+mean); int median=Find.findMedian(ranNum
Advertisements
Java Find Median
Java Find Median In this tutorial, you will learn how to find the median of numbers. There is no built in method to find the median of a list of numbers in the Java framework. But you can create your own method when you need to find
ModuleNotFoundError: No module named 'bin-by-median'
ModuleNotFoundError: No module named 'bin-by-median'  Hi, My... 'bin-by-median' How to remove the ModuleNotFoundError: No module named 'bin-by-median' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'median_polish'
ModuleNotFoundError: No module named 'median_polish'  Hi, My... 'median_polish' How to remove the ModuleNotFoundError: No module named 'median_polish' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'median-voting'
ModuleNotFoundError: No module named 'median-voting'  Hi, My... 'median-voting' How to remove the ModuleNotFoundError: No module named 'median-voting' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'bin-by-median'
ModuleNotFoundError: No module named 'bin-by-median'  Hi, My... 'bin-by-median' How to remove the ModuleNotFoundError: No module named 'bin-by-median' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'median_filter_cpp'
ModuleNotFoundError: No module named 'median_filter_cpp'  Hi, My... named 'median_filter_cpp' How to remove the ModuleNotFoundError: No module named 'median_filter_cpp' error? Thanks   Hi, In your
get absolute time java
get absolute time java  How to get absolute time in java
java get file permission
java get file permission  How to check the get file permission in Java
How to get Java SDK
How to get Java SDK  Hi, I have purchased a new computer and installed windows 7. Now I have to get the Java SDK and install on my computer for developing and testing Java programs. So, can anyone tell me How to get Java SDK
Java get System Locale
Java get System Locale       In this section, you will learn how to obtain the locale. We are providing you an example which will obtain the locale of the system
get
get ( ) Method in PHP  what is get method in PHP   Please visit the following link: http://www.roseindia.net/php/php-Get-Post.shtml
Java Get Example
Java Get Example      ... examples to get the things fast and practice these code at your end to learn java... language.ADS_TO_REPLACE_1 Java Get Examples.. Get host name in Java
how to get this xml syntx in java??????
how to get this xml syntx in java??????  how to get this xml syntx in java?????? < comp xlink:type="new" xlink:actuate="onload"/>
how to get this xml syntx in java??????
how to get this xml syntx in java??????   how to get this xml syntx in java?????? < comp xlink:type="new" xlink:actuate="onload"/>
Java Get Example
Java Get Example      ... examples to get the things fast and practice these code at your end to learn java... language.ADS_TO_REPLACE_1 Java Get Examples.. Get host name in Java
Java get windows Username
Java get windows Username       In this section, you will learn how to obtain the window's username. We are providing you an example which will obtain the window's username
Java Get Method
Java Get Method   ... when and how to use get method in Java. ADS_TO_REPLACE_1 The example... and year from date in Java?": ADS_TO_REPLACE_2 Code for Java Get
Java Get Memory
Java Get Memory       In this section of Java Example, you will see how to get... can easily learn it. In this Java Get Memory Size example, we have created
How to get the current date in Java?
How to get the current date in Java?  Hi, I am new to Java... in Java? Thanks   Hi, Check the tutorial: Java program to get current date. Thanks   Hi, To get the current date and time in Java you can use
How to get enum value in Java
How to get enum value in Java  Hi all, i am looking for a solution to get an enum value from index in Java. Please suggest. Thanks
Java get Next Day
Java get Next Day       In this section, you will study how to get the next day in java... DateFormatSymbols in order to get the names of the days of the week. The method getWeekdays
How to get current timestamps in Java
How to get current timestamps in Java  Hi, I want to get the current Timestamp in Java and then use it in JDBC program. How to get current timestamps in Java? Thanks   Hi, In Java using get the current Timestamp
How to get length of string in Java?
How to get length of string in Java?  What is the method of String for getting the length? How to get length of string in Java? Tell me code fast... Tutorials"; int length = str.length(); You have to use the str.length() to get
how to get browser cookies in java
how to get browser cookies in java  Hi, How to get cookies in serverlet and then print? how to get browser cookies in java? Thanks   Hi, Use following code: Cookie[] cookies = request.getCookies(); for (int i = 0
Java get Free Memory
Java get Free Memory      ... to get the memory left of being allocated, the method getruntime() returns the runtime object associated with the Java application. freeMemory- This method returns
How to get day of week in Java?
How to get day of week in Java?  Hi, How to get day of week in Java? Thanks   Hi, Calendar class is very useful in Java. You can use this class for getting the day of the week. Here is sample code: int weekday
Get computer name in java
Get computer name in java       We can get the computer name by the java code program...:\javaexamples>javac GetComputerName.java C:\javaexamples>java
Java Get Month
Java Get Month       In the previous Java examples, we have discussed about date functions... the current month of the existing year. This Java get month example is simply
Java Get Host Name
Java Get Host Name       In this Example you will learn how to get host name in Java... Name in Java?": Java code to get host nameADS_TO_REPLACE_2
Get IP Address in Java
Get IP Address in Java In this example we will describe How to get ip address in java. An IP address is a numeric unique recognition number which is assigned to the devices participating in the network for communication
How to Get Started with Java?
A Java Beginners Tutorial - How to Get Started with Java? For a programmer Java offers some challenges that are uncommon with other environments and to get... system it is in use. So to get started with Java the first step would
how to get file extension in java
how to get file extension in java  Hi, I want to get the file extension in Java. how to get file extension in java? Thanks   Hi, Here is sample code to get file extension in Java: package examples; import
CORE JAVA get middle name
CORE JAVA get middle name  hello sir...how to get middle name using string tokenizer....??? eg..like name ANKIT it will select only K...!!!!   The given code accepts the name from the console and find the middle
Java get default Locale
Java get default Locale     ..., or cultural region. We can get and set our default locale through the java programs... GetDefaultLocale.java C:\javaexamples>java GetDefaultLocale
Unable the get SubFolder Direcotry in Java?:-
Unable the get SubFolder Direcotry in Java?:-   Unable the get SubFolder Direcotry in Java?:- try { String st1 = "C:\TestFolder; // Main Folder URL File folder = new File(st1); File[] listOfFiles = folder.listFiles(); for (File
Java get User Input
Java get User Input       In this section, you will learn how to get the user input from the command prompt. We are providing you a java application
Java file get name
Java file get name In this section, you will learn how to get the name of the file. Description of code: You can see in the given example, we have created... class, you can get the name of any file. Output:ADS_TO_REPLACE_2
Java file get size
Java file get size In this section, you will learn how to get the size of a file. Description of code: You can see in the given example, we have created... is: " + filesizeInKB + " KB"); } } Through the method length(), you can get
Java get User Home
Java get User Home       In this section, you will study how to get the user home. We... with Example In this section we want to describe a code that helps you to get
Java get available Font
Java get available Font       In this section, you will learn how to get the available...;How to get available fonts in Java?":ADS_TO_REPLACE_2 Here is the code
Java get middle character
Java get middle character In this tutorial, you will learn how to get middle... of the word entered by the user using Java. If you have a word string 'company' and you want to get the character 'p' from the word, then you can
Java Get Class Location
Java Get Class Location       In this section, you will learn how to get the class location. For this, we have used the classes URL and ClassLoader. The class
Java Get Mime Type
Java Get Mime Type       To get the mime type, we have defined FileNameMap interface... in Java.  Go through the given example to find out, how to get mime type.ADS
Java Get Time in MilliSeconds
Java Get Time in MilliSeconds   ..._TO_REPLACE_1     Java Syntax to get time in millisecondsADS... the particular time, go through the below given example that illustrate how to get
Java Get Class path
Java Get Class path       In this section, you will learn how to get the class path. The method System.getProperties() determines the current system properties. 
Java get File Type
Java get File Type       This section illustrates you how to get the file type. In the given example, we have create an instance of the class File and passed the file 'Hello.txt
In Java how to get size of class and object?
In Java how to get size of class and object?  In Java how to get size of class and object
In Java how to get size of class and object?
In Java how to get size of class and object?  In Java how to get size of class and object

Ads