So I have a project with a tester that is not displaying what I thought it would display. The project description and everything is as follows:
Given an array of integers, how can we make some basic statistical calculations on the data? Specifically, we want to be able to determine the mean, median, and mode of the array of integers.
Mean is the arithmetic average of a series of numbers. It is found by summing all of the numbers and then dividing by the number of elements (numbers) in the series.
Median is the middle number (of a sorted set). Given a sorted array, it is found by finding the number that is in the middle of the array.
Mode is the number that appears the most frequently. Given a sorted array, all of the repeated numbers will be consecutive (in other words, repeated numbers are next to each other). To find the mode you must keep track of which number is repeated the most (has the highest frequency).
To get started on this assignment, download, extract, and save the Array Stats project to your computer.
Open up the project by clicking on the BlueJ package file icon. You will notice that there is only class in this project, namely ArrayStatsTest. You can run the ArrayStatsTest class anytime you like to see how much of the assignment you have completed.
Now go ahead and create a new ArrayStats class in your Array Stats project. Your assignment is to write an ArrayStats class with the following three methods:
public static double findMean(int[] a)
public static int findMedian(int[] a)
public static int findMode(int[] a)
Each method will utilize an integer array as a passed parameter. The passed array may be of any length, but you may assume that it is already sorted in ascending order. Each method will then perform the appropriate calculation and return the proper result.
-
Start by writing the method findMean. You will need to iterate through the entire array, adding up all of the values, and then divide the total by the total number of elements in the array. The result is returned as a double because it will probably be a value with a remainder (decimals).
-
Next, write the method findMedian. Again, you may assume the array is already sorted for you, so you just need to pick the middle element. Note that it does not matter if there are an odd number or an even number of elements in the array. Just take the length of the array and divide by two to find the index for the middle element. Here is another example where integer division works to our advantage (we'll always get an integer index when dividing the array length by 2).
- Finally, write the method findMode. This will probably be the most difficult to do. You must keep track of the number of times a given number is repeated (in other words, find its frequency). Note that you are not being asked for the frequency of all the numbers, just the one that is the highest (repeated the most). Think about how you can do this. Remember that the array is already sorted in ascending order, so numbers that appear more than once are right next to each other. You probably want to keep track of the number of times a given element is the same value as the one either before or after it. Be careful here, however, as you now are looking at two values of an array at the same time, so it is easy to generate range-bound errors (subscripts out-of-bounds) when you run the program. Writing a simple but effective algorithm to count these values is a challenging task, but it can be done.
Tester says: Now testing your ArrayStats class:
Found method findMean(int[] a)
Failed: mean is not 10.0
Found method findMedian(int[] a)
Failed: median is not 1
Found method findMode(int[] a)
Failed: mode is not 5
Failed: mean is not 10.0
Failed: median is not 1
Passed mode test 2
Failed: mean is not 10.0
Failed: median is not 1
Passed mode test 3
Bummer, try again
/**
* Write a description of class ArrayStats here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class ArrayStats
{public static double findMean(int[] a)
{
int[] array;
array = new int[10];
array [0] = 10;
array [1] = 10 ;
array [2] = 10;
array [3] = 10;
array [4] = 10;
array [5] = 10;
array [6] = 10;
array [7] = 10;
array [8] = 10;
array [9] = 10;
double sum = 0;
for (int i = 0; i < array.length; i++) {
sum += array[i];
}
return sum / array.length;
}
public static int findMode(int[] a)
{
int index = 0 ;
int soFar = 1 ;
int count = 1 ;
for(int k =1; k < a.length; k++){
if (a[k-1] == a[k]){
count++ ; }
if(count > soFar)
{
soFar = count ;
index = k ;
}
else {
count = 1 ;
}
}
return a[index] ;
}
public static int findMedian(int[] a)
{
int[] array;
array = new int[10];
array [0] = 1;
array [1] = 1 ;
array [2] = 1;
array [3] = 1;
array [4] = 1;
array [5] = 1;
array [6] = 1;
array [7] = 1;
array [8] = 1;
array [9] = 1;
int median = array.length /2 ;
return array [median] ;
}
}