[java]
/*
* 程序頭部註釋開始
* 程序的版權和版本聲明部分
* Copyright (c) 2011, 煙臺大學計算機學院學生
* All rights reserved.
* 文件名稱:創建一個矩陣類
* 作 者:薛廣晨
* 完成日期:2011 年 09 月 27 日
* 版 本號:x1.0
* 對任務及求解方法的描述部分
* 輸入描述:
* 問題描述:封裝一類矩陣對象,該類對象具有初始化矩陣的功能、修改矩陣元素的功能。
* 程序輸出:
* 程序頭部的註釋結束
*/
package xue;
public class MatrixPlus {
/**
* @param args
*/
int [][]M;
int column;
int row;
MatrixPlus() {
this.column = 5;
this.row = 5;
M = new int [column][row];
for(int i = 0; i < column; i++)
{
for(int j = 0; j < M[i].length; j++)
{
M[i][j] = 1;
}
}
}
MatrixPlus(int column, int row) {
this.column = column;
this.row = row;
M = new int [column][row];
for(int i = 0; i < column; i++)
{
for(int j = 0; j < M[i].length; j++)
{
M[i][j] = 1;
}
}
}
public void set_Column(int column) {
this.column = column;
M = new int [column][row];
for(int i = 0; i < column; i++)
{
for(int j = 0; j < M[i].length; j++)
{
M[i][j] = 1;
}
}
}
public void set_Row(int row) {
this.row = row;
M = new int [column][row];
for(int i = 0; i < column; i++)
{
for(int j = 0; j < M[i].length; j++)
{
M[i][j] = 1;
}
}
}
public void change_Plus(int column, int row, int a)
{
M[column][row] = a;
}
public void display() {
System.out.println("這是一個行為:" + column + "列為: " + row + "的矩陣 ");
for(int i = 0; i < column; i++)
{
for(int j = 0; j < M[i].length; j++)
{
System.out.print(M[i][j] + " " );
}
System.out.println();
}
}
}
//測試類
package xue;
public class TestMatrixPlus {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MatrixPlus M = new MatrixPlus();
M.display();
M.set_Column(6);
M.set_Row(6);
M.change_Plus(3, 4, 5);
M.display();
}
}
運行結果: