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: