Mean, Median and Mode

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









Related Tutorials/Questions & Answers:
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
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
Advertisements
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
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 Liabilities
Mean by Liabilities  hello, What is Meant by Liabilities ?   hii,ADS_TO_REPLACE_1 It the debt of the company to repay the amount to its creditors. Liability=Asset-Capital
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
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
javascript null mean
javascript null mean  What does javascript null mean
hibernate.connection.release_mode
hibernate.connection.release_mode
what is mean by setValidating() - Java Beginners
what is mean by setValidating()  what is mean by setValidating
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
undefined value mean in javascript
undefined value mean in javascript  What does undefined value mean in javascript?   There can be multiple reasons of having undefined values Object does not exist. Like you create an object of a control which does
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
What do you mean by Deadlock?
What do you mean by Deadlock?  What do you mean by Deadlock
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 do you mean by Serialized?
What do you mean by Serialized?  What do you mean by Serialized?   Hi, It is the part of the isolation level , Its bassically use for the Data read by the current transaction cannot be changed by another transaction
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 by Goldilocks effect?
What does it mean by Goldilocks effect?  Hi, I am beginner in Data...: What does it mean by Goldilocks effect? Try to provide me good examples or tutorials links so that I can learn the topic "What does it mean
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 '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 'strict-mode'
ModuleNotFoundError: No module named 'strict-mode'  Hi, My Python... 'strict-mode' How to remove the ModuleNotFoundError: No module named 'strict-mode' error? Thanks   Hi, In your python environment
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
What do you mean by docker container
What do you mean by docker container  Hi, I want to know more about Docker container. How to use this for running my projects? Can any one tell me what do you mean by docker container? Thanks
What do you mean by platform independence?
What do you mean by platform independence?   Hi, What do you mean.../java-features.shtml   Hi, Platform indipendance mean a program... code(.class) is system indipendence. it mean it is same in every operating system
vi command mode without esc
vi command mode without esc  Hi, What is the alternative key combination to esc in vi command? In other words how to go to the vi command mode... to the esc vi command mode: Ctrl+left arrow key Thanks
What do you mean by Read Uncommitted?
What do you mean by Read Uncommitted?  What do you mean by Read Uncommitted?   Hi, This situation occurs when transactions read data that has not been committed.In this isolation level, dirty reads are allowed. One
What do you mean by Read Committed?
What do you mean by Read Committed?  What do you mean by Read Committed?   Hi, Data records retrieved by a query are not prevented from modification by some other transactions. Non-repeatable reads may occur, meaning
What do you mean by Repeatable Read?
What do you mean by Repeatable Read?  What do you mean by Repeatable Read?   Hi, The Repeatable Read isolation level allows a transaction to acquire read locks on all rows of data it returns to an application
open a connection to a database with exclusive mode with JDBC
open a connection to a database with exclusive mode with JDBC  Is possible to open a connection to a database with exclusive mode with JDBC
open a connection to a database with exclusive mode with JDBC
open a connection to a database with exclusive mode with JDBC  Is possible to open a connection to a database with exclusive mode with JDBC
StringIndexOutOfBoundException problem, What is mean in this code | first.charAt(i)) |
StringIndexOutOfBoundException problem, What is mean in this code | first.charAt(i)) |  import java.io.*; public class Sort{ public static void main(String[]args)throws IOException{ BufferedReader br=new BufferedReader(new
StringIndexOutOfBoundException problem, What is mean in this code | first.charAt(i)) |
StringIndexOutOfBoundException problem, What is mean in this code | first.charAt(i)) |  import java.io.*; public class Sort{ public static void main(String[]args)throws IOException{ BufferedReader br=new BufferedReader(new

Ads