Mean, Median and Mode

Ads
 

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.
Generate students marks randomly.

Please include comments in the program so that I can understand what is being done.
View Answers

March 22, 2010 at 4:02 PM

Hi Friend,

Try the following code:

import java.util.*;
class Find {
//This method stores the sum of all the elements of array in variable sum and returns the average by dividing the sum with the length of array.
public static int findMean(int array[]){
int sum = 0;
int average=0;
int length=array.length;
for(int j = 0; j < length; j++){
sum += array[j];
average = sum / length;
}
return average;
}
//This method firstly sort the array and returns middle element if the length of arrray is odd.Otherwise it will return the average of two miidele elements.
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];
}
}
//This method counts the occurrence of each element of array and return the lement which has the maximum count.
public static int findMode(int array[]) {
int max=0, maxCount=0;
int length=array.length;
for (int i = 0; i <length; ++i) {
int count = 0;
for (int j = 0; j <length; ++j) {
if (array[j] == array[i]) ++count;
}
if (count > maxCount) {
maxCount = count;
max = array[i];
}
}
return max;
}
public static void main(String[] args)
{
int min = 30;
int max = 50;
int[] ranNum = new int[5];
System.out.println("Students Marks are:");
for ( int i = 0; i < ranNum.length; i++) {
ranNum[i] = (int)(Math.random() * (max - min + 1) ) + min;
System.out.println(ranNum[i]);
}
int mean=Find.findMean(ranNum);
System.out.println("Mean= "+mean);
int median=Find.findMedian(ranNum);
System.out.println("Median= "+median);
int mode=Find.findMode(ranNum);
System.out.println("Mode= "+mode);
}
}

Thanks

Ads









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
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
Advertisements
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 '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-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
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... the middle value in a set of numbers. The median is actually the middle value
Mean by Bean Scope
Mean by Bean Scope  What is Bean Scope
ModuleNotFoundError: No module named 'mean'
ModuleNotFoundError: No module named 'mean'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'mean' How to remove the ModuleNotFoundError: No module named 'mean' error
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
hibernate.connection.release_mode
hibernate.connection.release_mode
Sort the element find mean
the mean of 314.48,309.11,398.98,318.64,377.44. Because my client want the mean... are in the range of 300 to 400, that's why the mean of 314.48,309.11,398.98,318.64,377.44 One..., 65, 63, 33, In this example we need mean of 65,52,50,68,65,63. I am
Connected Mode and Dissconnect mode coding concept.
Connected Mode and Dissconnect mode coding concept.  Tell Me about Connected Mode and Dissconnect mode coding concept. also give me some syantax
ModuleNotFoundError: No module named 'mean-package'
ModuleNotFoundError: No module named 'mean-package'  Hi, My Python... 'mean-package' How to remove the ModuleNotFoundError: No module named 'mean-package' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'mode'
ModuleNotFoundError: No module named 'mode'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'mode' How to remove the ModuleNotFoundError: No module named 'mode' error
ModuleNotFoundError: No module named 'mode'
ModuleNotFoundError: No module named 'mode'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'mode' How to remove the ModuleNotFoundError: No module named 'mode' error
what does business analyst mean
what does business analyst mean  What is mean by business analyst? And how a business analyst can help any organisation to improve? Is there any career for a Business Analyst in IT (Information Technology) Sector? Also explain me
What does it mean that a class or member is final?
What does it mean that a class or member is final?   hi, What does it mean that a class or member is final? Thanks
SQL Mode
SQL Mode       SQL Mode is a Function that returns the frequent occurring value of a numeric expression. When there are no duplicate values in the records, the mode returns NA
ModuleNotFoundError: No module named 'mean-reversion-util'
ModuleNotFoundError: No module named 'mean-reversion-util'  Hi, My... named 'mean-reversion-util' How to remove the ModuleNotFoundError: No module named 'mean-reversion-util' error? Thanks   Hi
ModuleNotFoundError: No module named 'mean-reversion-utilities'
ModuleNotFoundError: No module named 'mean-reversion-utilities'  Hi...: No module named 'mean-reversion-utilities' How to remove the ModuleNotFoundError: No module named 'mean-reversion-utilities' error? Thanks  
ModuleNotFoundError: No module named 'mean-variance-portfolio'
ModuleNotFoundError: No module named 'mean-variance-portfolio'  Hi...: No module named 'mean-variance-portfolio' How to remove the ModuleNotFoundError: No module named 'mean-variance-portfolio' error? Thanks   
Types of content mode
Types of content mode  Please let me know the types of content mode available for UIView in Objective C.   UIViewContentModeScaleToFill, UIViewContentModeScaleAspectFit, // contents scaled to fit with fixed
ModuleNotFoundError: No module named 'a-la-mode'
ModuleNotFoundError: No module named 'a-la-mode'  Hi, My Python... 'a-la-mode' How to remove the ModuleNotFoundError: No module named 'a-la-mode' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'auto-mode'
ModuleNotFoundError: No module named 'auto-mode'  Hi, My Python... 'auto-mode' How to remove the ModuleNotFoundError: No module named 'auto-mode' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'compose-mode'
ModuleNotFoundError: No module named 'compose-mode'  Hi, My Python... 'compose-mode' How to remove the ModuleNotFoundError: No module named 'compose-mode' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'god-mode'
ModuleNotFoundError: No module named 'god-mode'  Hi, My Python...-mode' How to remove the ModuleNotFoundError: No module named 'god-mode... to install padas library. You can install god-mode python with following
ModuleNotFoundError: No module named 'god-mode'
ModuleNotFoundError: No module named 'god-mode'  Hi, My Python...-mode' How to remove the ModuleNotFoundError: No module named 'god-mode... to install padas library. You can install god-mode python with following
ModuleNotFoundError: No module named 'god-mode'
ModuleNotFoundError: No module named 'god-mode'  Hi, My Python...-mode' How to remove the ModuleNotFoundError: No module named 'god-mode... to install padas library. You can install god-mode python with following
ModuleNotFoundError: No module named 'presentation-mode'
ModuleNotFoundError: No module named 'presentation-mode'  Hi, My... named 'presentation-mode' How to remove the ModuleNotFoundError: No module named 'presentation-mode' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'compose-mode'
ModuleNotFoundError: No module named 'compose-mode'  Hi, My Python... 'compose-mode' How to remove the ModuleNotFoundError: No module named 'compose-mode' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'god-mode'
ModuleNotFoundError: No module named 'god-mode'  Hi, My Python...-mode' How to remove the ModuleNotFoundError: No module named 'god-mode... to install padas library. You can install god-mode python with following

Ads