Exercise Set 4

Put your solutions under the set4 directory in your repository.

Don't forget to commit your changes locally and then push them up to GitLab.

Ex9

Copy the file BankAccount.java that you created for Exercise 8 into the set4 directory. Edit the file and modify the class so that

  • The constructors throw an IllegalArgumentException if supplied with an empty string for account number
  • The second constructor throws an IllegalArgumentException if supplied with an initial balance < 0
  • The deposit and withdraw methods throw an IllegalArgumentException if supplied with an invalid amount, instead of returning a boolean value

Next, make a BankAccount printable by implementing a suitable toString method in the class. You can format this string any way you like, but make sure that it includes the name of the class and the current values of the fields.

Copy the test program Ex8.java that you created for Exercise 8 to a new file named Ex9.java, in the set4 directory. Modify this program so that it prints the string representation of the account object, instead of merely printing the balance of the account. Run the program to check that it throws the expected exception when the invalid withdrawal is attempted. (Don't try to intercept the exception - just let it halt the program.)

Ex10

In this exercise, you'll create a class Dataset that represents a collection of numbers and is able to compute statistics such as the arithmetic mean of those numbers:

UML representation of Dataset class

Start by creating the class in a file named Dataset.java. Give it an ArrayList field to use for data storage. Implement the constructor so that it reads numbers from a file whose filename is given by the constructor parameter, adding them to the ArrayList. You can use the Scanner and File to achieve this. Do not catch any exceptions within this constructor. This means that you will need to use an exception specification, declaring that the constructor can throw FileNotFoundException.

Implement the size method so that it returns the number of values stored in the ArrayList. 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.

Download Ex10.java. This program is supposed to use the Dataset class, but it is incomplete. The program expects a filename to be supplied on the command line, and exits with an error if this is not the case. Finish the implementation by adding code that creates a Dataset object using the filename provided on the command line. You code should also display the number of values stored in the Dataset and their mean value (the latter to 3 decimal places). Use try and catch to intercept any exceptions thrown by the code you've added.

Finally, create a file containing at least 10 numbers, one per line. Then test the program using this file. Here's some examples of what interaction with the program should look like:

  $ java Ex10
  Error: filename must be specified on command line
  $ java Ex10 file-that-does-not-exist
  Error: cannot access 'file-that-does-not-exist'
  $ java Ex10 test.data
  Dataset size = 12
  Mean value = 15.274