L12

Due by the end of Tuesday, December 5

Starter Files:


SUBMIT / CHECK

Today's Activities

Create a new project L12 for the program TestStats.

Today we will be creating a class named ArrayStats. This class will be like Arrays -- a class that contains several helpful methods for working with arrays. The ArrayStats class contains several static methods that compute some statistic values of integer arrays:

You will create the class ArrayStats so that its methods work as intended.

You may use a different name for the package, or not use a package at all, but you will have to modify/delete the package line of TestStats.

For the purposes of this lab, you do not need to do any error checking on the arguments to the methods. Just get the practice creating and manipulating the arrays.

Activity 1
Implement createRandomArray. It takes two int values: size (how many elements the new array should have); and max (the largest random value to use). It creates an integer array of the given size then fills it with random number from the range 1 to max.

For example,

a = ArrayStats.createRandomArray(5, 10);
would create an array with 5 elements containing random numbers from 1 to 10 -- possibly [5, 9, 10, 1, 1].
Hint: there's a method very much like this in the slides!
Activity 2
Implement getMax and getMin, which respectively return the largest and smallest values in the array. Do not change the array!

Assume that the array has at least one element.

Activity 3
Implement getSum and getMean, which return the sum and average of the elements of the array.

Remember that to get the average of int values, you need to cast the sum to be a double.

Don't copy the code for summing the array. You can get the sum of the array by calling the getSum method.

Activity 4
Implement getMedian, which returns the median value of the array.
(From Wikipedia -- red text added) "The median is the value separating the higher half of a data sample, a population, or a probability distribution, from the lower half. In simple terms, it may be thought of as the "middle" value of a data set when it's sorted. For example, in the data set {1, 3, 3, 6, 7, 8, 9}, the median is 6, the fourth number in the sample," while another dataset {1, 2, 3, 3, 3, 3, 4, 5, 6} has the median value of 3 (5th value).

"If the array contains an even number of elements, the median is the average of the two middle order statistics. For example, for the data set (1,2,3,4), the median is (2+3)/2.0 = 2.5, while for the data set (1,2,3,3,5,5), the median is 3."

Note: The array getMedian is given might not be sorted. The median is still the value that would be in the middle if it were sorted. So the median of [6, 1, 3, 3, 7, 8, 9] is 6.

Do not change the array! If the array was not sorted beforehand, it should not be sorted after calling this method.

Hint: you're not allowed to modify the array, but you are allowed to modify a copy of the array. Check out the Arrays helper class for methods that allow you to copy and sort arrays.

Run your program several times to make sure your methods are correct.

Submit this/these files:

You will be graded on the following:

  1. ArrayStats.java passed in revised, it compiles on its own
  2. ... createRandomArray is implemented
  3. ... ... and it functions properly using only material from class
  4. ... getMax and getMin are implemented
  5. ... ... and they function properly using only material from class
  6. ... getSum is implemented
  7. ... ... and it functions properly using only material from class
  8. ... getMean is implemented
  9. ... ... and it functions properly using only material from class
  10. ... getMedian is implemented
  11. ... ... and it functions properly for odd-length arrays using only material from class
  12. ... ... and it functions properly for even-length arrays using only material from class
  13. Al methods declared public and static
  14. All code meets the usual style requirements

SUBMIT   /   CHECK