Java程序设计(六)----矩阵(具有初始化矩阵的功能、修改矩阵元素的功能)

/* (程序头部注释开始)

/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2012, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:

* 作 者: 刘镇
* 完成日期: 2012 年 10 月 09 日
* 版 本 号: 2.006

* 对任务及求解方法的描述部分
* 输入描述: ......

* 问题描述:封装一类矩阵对象,该类对象具有初始化矩阵的功能、修改矩阵元素的功能。

* 程序输出: ......

* 程序头部的注释结束
*/

Matrix.java:

 

 

package lz_4w;

public class Matrix {

	/**
	 * @param args
	 */
	protected int[][]M;
	protected int column;
	protected int row;
	
	public Matrix()
	{
		this.column = 1;
		this.row = 1;
		M = new int[column][row];
		
		for(int i = 0; i < this.column; i++)
		{
			for(int j = 0; j < this.row; j++)
			{
				M[i][j] = 0;
			}
		}
	}
	
	public Matrix(int column, int row, int value)
	{
		this.column = column;
		this.row = row;
		M = new int[column][row];
		
		for(int i = 0; i < this.column; i++)
		{
			for(int j = 0; j < this.row; j++)
			{
				this.M[i][j] = value;
			}
		}
	}
	
	public void setM(int column, int row, int value)
	{
		M[column][row] = value;
	}
	
	public void displayMatrix()
	{
		for(int i = 0; i < this.column; ++i)
		{
			for(int j = 0; j < this.row; ++j)
			{
				System.out.print(M[i][j] + "\t");
			}
			
			System.out.println();
		}
		
		System.out.println();
	}
}


测试类 Test_Matrix:

 

package lz_4w;

public class Test_Matrix {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		Matrix m = new Matrix(8, 8, 10);
		Matrix n = new Matrix(8, 8, 5);
		//MatrixOperation M = new MatrixOperation();
		
		m.setM(3, 2, 60);
		n.setM(5, 4, 40);
		m.displayMatrix();
		n.displayMatrix();
		
	/*M.changeMatrix(m, 2, 2, 1, 1);
		M.changeMatrix(n, 3, 3, 2, 2);
		M.MatrixAdd(m, n);
		M.MatrixAdd(m, n);
		*/
	}

}


 

 

 

 

 

 

成果展示:

 

 

 

 

 

经验心得:

 

挺不错的封装,个人觉得在编写一个矩阵类时,明白属性有哪些,矩阵就是记录行和列,而其中要用到有关功能,又要有有一个二维数组,因此确定了封装的属性;接着是有关构造方法,做了两个,带参数和和无参数的,在测试类中用到了有参数的,切记在构造函数访问修饰符!public,接下来就是照着要求将修改数据的功能通过方法实现,还有一点比较重要就是在初始化时不仅要初始化行和列还有对开辟的二维数组每一个元素都赋值。

 

微笑

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值