Matrix addition and multiplication are the same algorithms whether the elements are integers or exact fractions; only "add two elements" and "multiply two elements" differ. Write:
- an abstract class
GenericMatrix<E>with the abstract methodsadd(E, E),multiply(E, E)andzero(), and the concrete methodsaddMatrix(E[][] a, E[][] b)andmultiplyMatrix(E[][] a, E[][] b), written once; IntegerMatrix extends GenericMatrix<Integer>;- a small immutable class
Fraction(always in lowest terms) andFractionMatrix extends GenericMatrix<Fraction>.
The input gives the type (int or frac), the size n, then the n × n matrices A and B; fractions are written 3/4 (or 2). Print A + B and A × B, each element in a field of width 8. For the input
int
2
1 2
3 4
5 6
7 8
the program prints
A + B
6 8
10 12
A x B
19 22
43 50