Exercise 8: ArrayList & Reading From Files

Create a file named Dataset.java, in the exercises/ex8 directory of your repository.

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 below.

UML representation of Dataset class

Name your fields and methods exactly as shown in this diagram, and do not add any other fields or methods to the 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 exercises/ex8 directory. 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

When you are confident that Dataset is working as required, go to Submit My WorkExercises 6-10Exercise 8 in Minerva to submit your 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.