Ads

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.

Generate students marks randomly.

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

Ads

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

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:

Tutorials

Java
Spring
Hibernate
Struts
Training
java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient
How do I resolve this Java Class not found exception?
httpclient java.lang.NoClassDefFoundError
Apache Commons ioutils maven dependency
Read/Convert an inputStream to a String
What is the meaning of Java Platform?
Why Java is a platform independent language?
What is the benefits of learning Core Java?
Which technology should I learn after Java?
What is array in java with example?
How to Convert ArrayList to Array?
How to substring in Java?
How to format number in Java?
What is instance variable in Java?
How to download MySQL JDBC driver?
What is Calendar class in Java?
Which is the best Java tutorials for beginners?
How to rename a file in Java?
How to delete file in Java code?
How to get day from date in Java using Calendar?
How to get day of week in Java?
How to calculate Date Difference in Java?
How to compare date in Java?
How to declare array in Java?
How to calculate average of array in Java?
What is Array in Java?
write a java program to find the summation of all the integers entered on command line
Sum of two numbers using command line arguments in Java
How to create and use Array in Java?
How to pass command line arguments in Java?
How to create Applet Hello World?
Appending String efficiently in Java
How to append String in Java?
How to list even numbers between 1 and 100?
How to add BigDecimal in Java?
What is Abstraction In Java?
Which is best Beginners Java Tutorial?
What is java.util package?
Create list from array in Java
Filter collection in Java 8
What is the best way to filter a Java Collection?
Easy way to transform Collection to Array?
How to convert Collection to Array in Java?
What are Basic Java Language Elements?
Advanced Java Tutorials in 2017
Java brief history
Best Reasons to learn Java
Java Example Codes and Tutorials in 2017
How do I read a large file quickly in Java?
Is learning Java worthwhile?

Ads

- Java Tutorials
- Java Code example
- Java Programming
- Java Beginners Examples
- Applet Tutorials
- Awt Tutorials
- Java Certification
- Interview Question
- Java Servlets Tutorial
- Jsp Tutorials
- Java Swing Tutorials
- JDBC Tutorial
- EJB Tutorials
- Java Server Faces (JSF) Tutorial
- WAP Tutorial
- Struts Tutorial
- JAXB Tutorial
- Spring FrameWork Tutorial
- SOA&Web Services Tutorials
- Bioinformatics Tutorials
- MySQL Tutorials
- JAVA DOM Tutorial
- XML Tutorial
- EAI Articles
- Many Programming Tutorials Links
- Tutorials Books
**Java Script Tutorial****Ajax Tutorial****Dojo Tutorials****Programming Books****Trainings****Flex****Ant****RDF**

Ads