Data Structures, Algorithms, & Applications in Java
Chapter 8, Exercise 15

The code for the new methods is given below.
package dataStructures;

import java.lang.reflect.*;
import utilities.*;
import exceptions.*;
import wrappers.*;

public class ExtendedMatrix extends Matrix
{
   // constructor
   public ExtendedMatrix(int theRows, int theColumns)
      {super(theRows, theColumns);}

   
   /** decrement all elements of this by x */
   public void decrement(Object x)
   {
      for (int i = 0; i < rows * cols; i++)
          ((Computable) element[i]).decrement(x);
   }
   
   /** multiply all elements of this by x */
   public void multiplyByConstant(Object x)
   {
      for (int i = 0; i < rows * cols; i++)
          element[i] = ((Computable) element[i]).multiply(x);
   }
   
   /** divide all elements of this by x */
   public void divideByConstant(Object x)
   {
      for (int i = 0; i < rows * cols; i++)
          element[i] = ((Computable) element[i]).divide(x);
   }

   /** input a matrix from the given input stream */
   public void input(Object theZero, MyInputStream inStream)
   {
      Method inputMethod;
      Object [] inputMethodArgs = {inStream};
      Class [] parameterTypes = {inStream.getClass()};
      try
      {
         // get the proper method to be used to read in the values
         inputMethod = theZero.getClass().
                          getMethod("input", parameterTypes);
   
         // input matrix characteristics
         System.out.println("Enter number of rows and columns");
         rows = inStream.readInteger();
         cols = inStream.readInteger();
         // validate rows and cols
         if (rows < 0 || cols < 0)
            throw new MyInputException
                  ("number of rows and columns must be >= 0");
         if ((rows == 0 && cols != 0) ||
             (rows != 0 && cols == 0))
            throw new MyInputException
                  ("both the number of rows and columns must equal " +
                   "zero or both must be > 0");
   
         // create the element array
         element = new Object [rows * cols];
      
         // input the elements
         for (int i = 1; i <= rows; i++)
            for (int j = 1; j <= cols; j++)
               set(i, j, inputMethod.invoke(null, inputMethodArgs));
      }
      catch (Exception e)
      {
         System.out.println(e);
         throw new IllegalArgumentException
                   ("must have the static method input() defined");
      }
   }
}



The test pogram, input, and output are in the files ExtendedMatrix.*