利用二维数组(double[])实现一个矩阵类:Matrix。要求提供以下方法:(1)set(int row, int col, double value):将第row行第col列的元素赋值为value;(2)get(int row,int col):取第row行第col列的元素;(3)width():返回矩阵的列数;(4)height():返回矩阵的行数;(5)Matrix add(Matrix b):返回当前矩阵与矩阵b相加后的矩阵;(6)Matrix multiply(Matrix b):返回当前矩阵与矩阵b相乘后的矩阵。(7)Matrix transpose():返回当前矩阵的转置矩阵;(8)getMax():返回矩阵中的最大值及其所在行和列;(9)print():以行和列的形式打印出当前矩阵。
代码
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
DecimalFormat d = new DecimalFormat("#");
Scanner cin = new Scanner(System.in);
Matrix matrix = new Matrix(cin.nextInt(), cin.nextInt());
Matrix t;
matrix.input(cin);
System.out.println("row:" + matrix.height() + " " + "column:" + matrix.width());
matrix.set(cin.nextInt(), cin.nextInt(), cin.nextDouble());
System.out.println("after set value:");
System.out.print(matrix.toString());
int r = cin.nextInt();
int c = cin.nextInt();
System.out.println("value on (" + r + "," + c + "):" + d.format(matrix.get(r,c)));
Matrix tmp = new Matrix(cin.nextInt(),cin.nextInt());
tmp.input(cin);
System.out.println("after add:");
t = matrix.add(tmp);
t.print();
System.out.println("矩阵中的最大值:" + t.getMax());
System.out.println("after multiply:");
tmp = new Matrix(cin.nextInt(),cin.nextInt());
tmp.input(cin);
t = matrix.multiply(tmp);
t.print();
System.out.println("矩阵中的最大值:" + t.getMax());
System.out.println("after transpose:");
t = matrix.transpose();
t.print();
System.out.println("矩阵中的最大值:" + t.getMax());
}
}
class Matrix{
private int row, col;
private double[][] d;
public Matrix(int row, int col) {
this.row = row;
this.col = col;
d = new double[row][col];
}
public void input(Scanner cin) {
for(int i = 1; i <= this.row; i++) {
for(int j = 1; j <= this.col; j++) {
double val = cin.nextDouble();
this.set(i,j,val);
}
}
}
public void set(int row, int col, double value){
d[--row][--col] = value;
}
public double get( int row, int col ){
return d[--row][--col];
}
public int width(){
return col;
}
public int height(){
return row;
}
public Matrix add( Matrix b){
Matrix t = new Matrix(this.row, this.col);
for(int i = 1; i <= this.row; ++ i){
for(int j = 1; j <= this.col; ++ j ){
t.set(i, j, this.get(i, j) + b.get(i, j));
}
}
return t;
}
public Matrix multiply( Matrix b){
Matrix t = new Matrix(this.row, b.width());
for(int i = 1; i <= this.row; ++ i){
for(int j = 1; j <= b.width(); ++ j ){
double tt = 0;
for(int k = 1; k <= this.col; k ++ ){
tt += this.get(i, k) * b.get(k, j);
}
t.set(i, j, tt);
}
}
return t;
}
public Matrix transpose(){
Matrix t = new Matrix(this.row, this.col);
for( int i = 1; i <= this.row; i ++ ){
for ( int j = 1; j <= this.col; j ++ ){
t.set(j, i , this.get(i, j));
}
}
return t;
}
public String getMax(){
DecimalFormat d = new DecimalFormat("#");
double max = this.get(1,1);
int maxr = 1;
int maxc = 1;
for(int i = 1; i <= this.row; ++ i){
for( int j = 1; j <= this.col ; ++ j ){
if( max < this.get(i, j) ){
max = this.get(i, j);
maxr = i;
maxc = j;
}
}
}
return "max: [ " + maxr + ", " + maxc + " ] = " + d.format(max);
}
public void print(){
DecimalFormat d = new DecimalFormat("#");
for(int i = 1; i <= this.row; ++ i){
for( int j = 1; j <= this.col - 1; ++ j ){
System.out.print(d.format(this.get(i, j)) + " ");
}
System.out.println(d.format(this.get(i, col)));
}
}
}