Ashish
Mean, Median and Mode
Posted in : Java Beginners
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.

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

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
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
hibernate.connection.release_mode
hibernate.connection.release_mode
Mean by Liabilities
Mean by Liabilities  hello, What is Meant by Liabilities ?   hii, It the debt of the company to repay the amount to its creditors. Liability=Asset-Capital
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
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
Mean by Bean Scope
Mean by Bean Scope  What is Bean Scope
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
javascript null mean
javascript null mean  What does javascript null mean
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
what is mean by setValidating() - Java Beginners
what is mean by setValidating()  what is mean by setValidating
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 Deadlock?
What do you mean by Deadlock?  What do you mean by Deadlock
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
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 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
iPhone landscape mode
iPhone landscape mode In this series of iPhone application development tutorial, you'll learn launching your iPhone app in landscape mode. Believe me it's very simple to change your iphone app in landscape mode. Just follow
need a jar file to come out of sleep mode
then phone goes to out of sleep mode. can any body help me with this subject ? I... it to any address by email . But when the phone goes to sleep mode... mode .So I need a jar file to run and be hidden and check the incoming SMS
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
Excel sheet in read only mode - WebSevices
Excel sheet in read only mode  If am opening an excel sheet(through web application) to edit any information,Its opening in read-only mode... to open in read and write mode so that user can edit it. I am using Java
Show XOR Mode
Show XOR Mode       In this section, you will studied about the XOR mode.  XOR or Exclusive... the uncommon part. We are providing you an example which shows the XOR mode
open a bufferedwriter file in append mode - Java Beginners
open a bufferedwriter file in append mode  hi.. i jus want knw how to opena bufferedwriter file in append mode..  Hi friend, FileWriter The FileWriter is a class used for writing character files. This constructor
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
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 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
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
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
Tutorials