Home Answers Viewqa Java-Beginners Mean, Median and Mode

Ashish
Mean, Median and Mode
1 Answer(s)      3 years and 2 months ago
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

Related Pages:
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
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
hibernate.connection.release_mode
hibernate.connection.release_mode
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
javascript null mean
javascript null mean  What does javascript null mean
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
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
What do you mean by Deadlock?
What do you mean by Deadlock?  What do you mean by Deadlock
Mean - average
Java NotesMean - average Computing the arithmetic average (mean) can.... //================================================= mean public static double mean(double[] p) { double...++) { sum += p[i]; } return sum / p.length; }//end method mean Question
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
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
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
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 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
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
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
what does mean of method("1") in below struts.xml file
what does mean of method("1") in below struts.xml file  Hi, here is my code /register.jsp /register.jsp /success.jsp
Unable to open Java in full screen mode in Vista - Java Beginners
Unable to open Java in full screen mode in Vista  Hi iam facing a serious problem as my operating system is Windows-Vista.The editor always opens in minimized mode.If i try Alt+enter it shows win32 error.i googled it and found
XML- read Text Mode - Java Error in Windows - reg. - Java Beginners
XML- read Text Mode - Java Error in Windows - reg.  Dear All I'm creating the code as read the XML file in BufferedReader method. Here I pasted the code below:. (*) First read the XML file using FileInputStream
Opening IE from Java Swing Application in Kiosk mode
window is opening. But I want to open the IE window in Kiosk mode or without address
Design Express Realization Mode
Design Express Realization Mode       When we want to express our realization, we need a expression mode. Many expression mode images are present in the current time. It is a step
JDBC Autocommit
JDBC Autocommit Mode In this tutorial we are discussing about jdbc autocommit mode. Setting the true or false of the JDBC autocommit mode allows the developer to take control on the JDBC transaction. By default, JBDC autocommit mode
Example to show Enumeration exception in java
Example to show Enumeration exception in java       Enumeration is mean of retaining all the series of element at a time. An object implement interface generates a series of all
Hibernate Criteria Lockmode
Hibernate Criteria Lockmode setLockMode is a method of Hibernate Criteria API. You can use a lock mode in the following way Criteria criteria = session.createCriteria(Student.class); criteria.add(Restrictions.eq("name", "Ramesh
Wicket
Wicket       Wicket is a mixed mode Java framework with built in support for Ajax, Just Java' object oriented programming Read full Description
XMLEspresso
; The default edit mode in XML document source view is Text Mode...; XML Mode with restricted editing capabilities is also available. We recommend Text Mode for normal editing during development; XML Mode
Mysql Date Convert
Mysql Date Convert       The 'Mysql Date Convert' show you the mean to format the Date. Understand with Example The Tutorial describe you an example from 'Date Convert in Mysql
Rendering Example
setTextRenderMode(int mode,float strokeWidth,Color strokeColor).We can gives various color and int mode.    Code Description:  In this example we are using setTextRenderMode(int mode,float strokeWidth,Color
Horizontal Frameset in HTML
Horizontal Frameset in HTML       The Frameset in HTML is mean for implementing multiple pages in a single window. Each frame in a HTML page presents a separate web
JavaScript array processing
JavaScript array processing       By processing array we simply mean that processing of the outputs of an array. Here in this example we have created three array objects