Java Reference
In-Depth Information
80 System.out.print(m3[i][j] + " " );
81
82 System.out.println();
83 }
84 }
85 }
Listing 19.11 implements the IntegerMatrix class. The class extends
GenericMatrix<Integer> in line 1. After the generic instantiation, the add method in
GenericMatrix<Integer> is now Integer add(Integer o1, Integer o2) . The
add , multiply , and zero methods are implemented for Integer objects. These methods
are still protected, because they are invoked only by the addMatrix and multiplyMatrix
methods.
L ISTING 19.11
IntegerMatrix.java
1 public class IntegerMatrix extends GenericMatrix<Integer> {
2 @Override /** Add two integers */
3
extends generic type
protected Integer add(Integer o1, Integer o2) {
implement add
4
return o1 + o2;
5 }
6
7 @Override /** Multiply two integers */
8
protected Integer multiply(Integer o1, Integer o2) {
implement multiply
9
return o1 * o2;
10 }
11
12 @Override /** Specify zero for an integer */
13
protected Integer zero() {
implement zero
14
return 0 ;
15 }
16 }
Listing 19.12 implements the RationalMatrix class. The Rational class was introduced
in Listing 13.13 Rational.java. Rational is a subtype of Number . The RationalMatrix
class extends GenericMatrix<Rational> in line 1. After the generic instantiation, the
add method in GenericMatrix<Rational> is now Rational add(Rational r1,
Rational r2) . The add , multiply , and zero methods are implemented for Rational
objects. These methods are still protected, because they are invoked only by the addMatrix
and multiplyMatrix methods.
L ISTING 19.12
RationalMatrix.java
1 public class RationalMatrix extends GenericMatrix<Rational> {
2 @Override /** Add two rational numbers */
3
extends generic type
protected Rational add(Rational r1, Rational r2) {
implement add
4
return r1.add(r2);
5 }
6
7 @Override /** Multiply two rational numbers */
8
protected Rational multiply(Rational r1, Rational r2) {
9
return r1.multiply(r2);
implement multiply
10 }
11
12
@Override /** Specify zero for a Rational number */
13
protected Rational zero() {
implement zero
14
return new Rational( 0 , 1 );
15 }
16 }
 
 
Search WWH ::




Custom Search