Providing statistical data

exercise No. 161

Adding support to retrieve statistical data.

Q:

Extend Allow for variable capacity holding integer values by providing:

  1. A method double getAverage() providing statistical data on a given set of integer values.

  2. A method void clear() enabling a user to insert new sets of values.

Do not forget to extend your Junit tests. You may want to import the skeleton project for getting started.

Caution

When testing for equality of double values you may find the Junit method assertEquals()to be marked as deprecated. Give a reason why this decision has been taken. The answer may guide you in finding a more appropriate test.

A:

Testing for equality of two double variables is generally a bad idea. Consider:

double b= 1./3.;
b++;
b--;
System.out.println(3 * b);

This yields:

0.9999999999999998

Thus assertEquals(1., b) will fail due to limited arithmetic precision. Junit provides a method assertEquals(double expected, double actual, double delta) addressing this problem:

@Test
public void testApp() {

   double b= 1./3.;
   b++;
   b--;

   Assert.assertEquals(1., 3 * b, 1.E-15);
}

The last parameter represents the allowed bias with respect to the expected result. In the above example every value within the range [ 1 - 10 - 15 , 1 + 10 - 15 ] will show up as a positive test result.