Create a file named
Dataset.java, in the
exercises/ex8 directory of
In this file, create a class named
Dataset that represents a collection
of numbers. The class should have the features shown on the UML diagram
Name your fields and methods exactly as shown in this diagram, and do not add any other fields or methods to the class!
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
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
When you are confident that
Dataset is working as required, go to
Submit My Work → Exercises 6-10 → Exercise 8 in Minerva to
Dataset.java file (and only that file) for grading. You can
improve your solution and resubmit as many times as you like until the
deadline. Also, don’t forget to commit all your work to your Git repository
and push those commits up to GitLab.