Create a new directory for this exercise. In this directory, create a
Dataset.java. In this file, define a class named
that represents a collection of numbers. The class should have the
features shown on the UML diagram nbelow.
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
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
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
than allowing an invalid calculation to be performed.
To test your class, download
data.txt into the same directory as
ComputeMean.java contains a program that uses
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