Exercise 8: ArrayList & Reading From Files

Create a new directory for this exercise. In this directory, create a file named Dataset.java. In this file, define a class named Dataset that represents a collection of numbers. The class should have the features shown on the UML diagram nbelow.

UML representation of Dataset class

The data field in this class is an ArrayList that will hold numbers. The constructor should populate the list by reading numbers from a file whose name is given as the constructor parameter. There are various ways of achieving this in Java. We recommend using a Scanner with a Path object, as shown in Lecture 8; alternatively, you could use a Scanner with a File object, as shown in Section 11.2.1 of Eck’s book.

Do not catch any exceptions within the constructor. This means that you will need to include an exception specification in your definition of the constructor, declaring that it can throw IOException.

Implement the size() method so that it returns the number of values stored in the list. Then implement meanValue() so that it computes and returns the arithmetic mean of the stored values. If there are no stored values, this method should throw an instance of ArithmeticException rather than allowing an invalid calculation to be performed.

To test your class, download ComputeMean.java and data.txt into the same directory as Dataset.java. The file ComputeMean.java contains a program that uses Dataset to compute the mean value of numbers stored in a file. Study this program and then compile it. The command needed to run the program and the expected output are shown below.

$ java ComputeMean data.txt
Dataset size = 10
Mean value = 0.547