Java I/O and exception handling

In this homework you will be modifying the Matrix class and test program you implemented in the previous homework.

1. User defined exceptions

The add and multiply methods should now throw exceptions if the shapes of the matrices do not allow the matrices to be combined according to the rules of matrix arithmetic. Declare a new, suitably named, exception class extending Exception, and have your methods throw this exception when an error is detected.

The test program should be modified to catch these exceptions, and take suitable action (e.g. print an explanatory message).

2. User defined output

Override the method
    public String toString() {...}
from the java.lang.Object class in your Matrix class. It should return a formatted String representation of all the elements, laid out in a way suitable for displaying on a terminal (for example). This string might include newline characters, "\n", to separate rows. (Note toString should not do any output internally--it should just return a string).

The test program should be modified to use this method whenever a matrix is to be output (you can call your toString method explicitly, or you can use "string conversion" to call it implicitly).

3. Input

Define a new "utility class", with static methods for keyboard input. Include methods
    static public int readInt(BufferedReader in) {...}
    static public float readFloat(BufferedReader in) {...}
for reading ints and floats. Detailed implementation may follow the example given in the lectures.

Add a method

    public void keyboardRead(BufferedReader in) {...}
to the Matrix class, which initializes elements of a matrix by reading them from the keyboard.

Modify your test program to use these methods to get initial data.