## Pages

### Mean and Median : Java : BlueJ

Objective:

The BlueJ Program finds the mean and median values from the Array of values inputted by the User by simple formula on Statistics. The mean is the sum of the values divided by the number of values. The mean of a set of numbers x1x2, ..., xn is typically denoted by $\bar{x}$, pronounced "x bar". This mean is a type of arithmetic mean. If the data set were based on a series of observations obtained by sampling a statistical population, this mean is termed the "sample mean" ($\bar{x}$) to distinguish it from the "population mean" (μ or μx). The mean is often quoted along with the standard deviation: the mean describes the central location of the data, and the standard deviation describes the spread. An alternative measure of dispersion is the mean deviation, equivalent to the average absolute deviation from the mean. It is less sensitive to outliers, but less mathematically tractable.
The arithmetic mean is the "standard" average, often simply called the "mean".

$\bar{x} = \frac{1}{n}\cdot \sum_{i=1}^n{x_i}$
The mean may often be confused with the median, mode or range. The mean is the arithmetic average of a set of values, or distribution; however, for skewed distributions, the mean is not necessarily the same as the middle value (median), or the most likely (mode). For example, mean income is skewed upwards by a small number of people with very large incomes, so that the majority have an income lower than the mean. By contrast, the median income is the level at which half the population is below and half is above. The mode income is the most likely income, and favors the larger number of people with lower incomes. The median or mode are often more intuitive measures of such data.
Nevertheless, many skewed distributions are best described by their mean – such as the exponential and Poisson distributions.
For example, the arithmetic mean of six values: 5, 10, 13, 7, 25, 31 is
$\frac{5+10+13+7+25+31}{6} = \frac{91}{6} \approx 15.$

### Geometric mean (GM)

The geometric mean is an average that is useful for sets of positive numbers that are interpreted according to their product and not their sum (as is the case with the arithmetic mean) e.g. rates of growth.
$\bar{x} = \left ( \prod_{i=1}^n{x_i} \right ) ^\tfrac1n$
For example, the geometric mean of six values: 34, 27, 45, 55, 22, 34 is:
$(34 \cdot 27 \cdot 45 \cdot 55 \cdot 22 \cdot 34)^{1/6} = 1,699,493,400^{1/6} \approx 34.545.$

### Harmonic mean (HM)

The harmonic mean is an average which is useful for sets of numbers which are defined in relation to some unit, for example speed (distance per unit of time).
$\bar{x} = n \cdot \left ( \sum_{i=1}^n \frac{1}{x_i} \right ) ^{-1}$
For example, the harmonic mean of the six values: 34, 27, 45, 55, 22, and 34 is
$\frac{6}{\frac{1}{34}+\frac{1}{27}+\frac{1}{45} + \frac{1}{55} + \frac{1}{22}+\frac{1}{34}} = \frac{60588}{1835} \approx 33.0179836.$

Median is described as the numerical value separating the higher half of a sample, a population, or a probability distribution, from the lower half. The median of a finite list of numbers can be found by arranging all the observations from lowest value to highest value and picking the middle one.

Screenshot:

Program Source Code:

/**
* The Program calculates the Mean and Median of the data provided by the user.
* @ shantanukhan1995@gmail.com
* @website 0code.blogspot.com
* Program Type : BlueJ Program - Java
*/
import java.io.*;
import java.util.*;
public class Mean_Median
{
private int[]arr;
private int size;
static Scanner sc= new Scanner(System.in); //Declaration of Scanner Class
public Mean_Median(int n) // Constructor
{
arr=new int[size=n];
}
public void Input() //Taking input
{
for(int i=0;i<size;i++)
{
System.out.print("Enter the Integer No. "+(i+1)+" : ");
arr[i]= sc.nextInt();
}
}
public void bubbleSort() //Sorting the Data Provided
{
for(int i=1;i<size;i++)
{
for(int j=0;j<size-i;j++)
{
if(arr[j]>arr[j+1])
{
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
public double Mean() //Calculate the Mean
{
double sum=0;
for(int i=0;i<size;i++)
{
sum+=arr[i];
}
return sum/size;
}
public double Median() //Calculate the Median
{
int mid = size/2;
if(size%2!=0)
return((double)(arr[mid]));
else
return(arr[mid-1]+arr[mid])/2.0;
}
public static void main(String []args) // Main Method
{
System.out.print(" Enter size of array : ");
int n =sc.nextInt();
Mean_Median obj = new Mean_Median(n) ; // Object Creation
obj.Input();
System.out.println("Mean = "+obj.Mean());
obj.bubbleSort();
System.out.println("Median = "+obj.Median());
}
}



© Shantanu Khan 0code  ® Mean and Median