Java Reference
In-Depth Information
2. Extend the
logging
package to support a null device in which messages
are thrown away.
3. Continuing from Exercise 1, introduce the following matrix-multiplication
method into
Matrix
:
Matrix multiply(Matrix m)
{
Matrix
result
=
new
Mat-
rix(matrix.length, matrix[0].length);
for (int i = 0; i < matrix.length;
i++)
for
(int
j
=
0;
j
<
m.matrix[0].length; j++)
for
(int
k
=
0;
k
<
m.matrix.length; k++)
result.matrix[i][j]
=
result.matrix[i][j]+
mat-
rix[i][k]*m.matrix[k][j];
return result;
}
Next,declarea
void rotate(double angle)
methodin
G2D
.This
method'sfirsttaskistonegateits
angle
argument(toensurecounterclock-
wiserotation),whichspecifiesarotationangleindegrees.Itthencreatesa
3-by-3rotation
Matrix
andinitializesthefollowing(row,column)entries:
(0, 0) to the cosine of the angle, (1, 0) to the sine of the angle, (0, 1) to
the negative of the angle's sine, (1, 1) to the cosine of the angle, and (2,
2)to1.0.Staticallyimportallnecessary
Math
classmethods.Finally,
ro-
tate()
multipliestheidentitymatrixcreatedin
G2D
'sconstructorbythis
rotation matrix, and invokes
dump()
to dump the result. Test
rotate()
from the
main()
method by executing
G2D g2d = new G2D();
g2d.rotate(45);
. You should observe the following output:
1.0 0.0 0.0
0.0 1.0 0.0
0.0 0.0 1.0
0.7071067811865476 0.7071067811865475 0.0
-0.7071067811865475 0.7071067811865476 0.0
0.0 0.0 1.0