java 面向对象——Matrix软工一

Matrix java面向对象的层层深入

层层深入,由结构化编程逐渐转向面向对象编程

结构化编程

package edu.nju;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
/**
 * 实现矩阵的加法、乘法以及控制台输出
 * 其中加法和乘法需要有两种实现方式
 * 1.传入一个矩阵进行2个矩阵的操作
 * 2.从控制台(console)读入一个矩阵,再进行操作
 * 所有的数据均为int型
 * 输入数据均默认为正确数据,不需要对输入数据进行校验
 * @author Ray Liu & Qin Liu
 */
public class MatrixCalculation {
	
	/**
	 * 实现矩阵加法,返回一个新的矩阵
	 * @return result matrix = A + B
	 */
	public int[][] plus(int[][] A, int[][] B){
		// TODO
		if (A.length!=0 && A[0].length!=0) {
			int[][] result = new int[A.length][A[0].length];
			for (int i = 0; i < A.length; i++)
				for (int j = 0; j < A[0].length; j++) {
					result[i][j] = A[i][j] + B[i][j];
				}
			return result;
		}
		else
			return A;
	}
	
	/**
	 * 实现矩阵乘法,返回一个新的矩阵
	 * @return result matrix = A * B
	 */

	//注意times数组结果的范围
	public int[][] times(int[][] A, int[][] B){
		// TODO
		if (A.length!=0 && A[0].length!=0) {
			int[][] result = new int[A.length][B[0].length];
			for (int i = 0; i < A.length; i++)
				for (int j = 0; j < B[0].length; j++) {
					int sum=0;
					for(int k=0;k<A[0].length;k++){
						sum +=A[i][k]*B[k][j];
					}
					result[i][j]=sum;
				}
			return result;
		}
		else
			return A;
	}
	
	/**
	 * 从控制台读入矩阵数据,进行矩阵加法,读入数据格式如下:
	 * m n
	 * m * n 的数据方阵,以空格隔开
	 * 连续读入2个矩阵数据
	 * example:
	 * 4 3
	 * 1 2 3
	 * 1 2 3
	 * 1 2 3
	 * 1 2 3
	 * 4 3
	 * 1 2 3
	 * 1 2 3
	 * 1 2 3
	 * 1 2 3
	 * 返回一个新的矩阵
	 */
	public int [][] plusFromConsole(){
		// TODO
		Scanner in=new Scanner(System.in);
		int row=in.nextInt();
		int  col=in.nextInt();

		//读入A
		int [][]A=new int[row][col];
		for(int i=0;i<row;i++)
			for(int j=0;j<col;j++)
			{
				A[i][j]=in.nextInt();
			}


		int row_second=in.nextInt();
		int col_second=in.nextInt();

		//读入B
		int [][]B=new int[row_second][col_second];
		for(int i=0;i<row_second;i++)
			for(int j=0;j<col_second;j++)
			{
				B[i][j]=in.nextInt();
			}
		int [][]C=plus(A,B);
			return C;

	}

	/**
	 * 输入格式同上方法相同
	 * 实现矩阵的乘法
	 * 返回一个新的矩阵
	 */
	public int[][] timesFromConsole(){
		// TODO
		Scanner in=new Scanner(System.in);
		int row=in.nextInt();
		int col=in.nextInt();

		//读入A
		int [][]A=new int[row][col];
		for(int i=0;i<row;i++)
			for(int j=0;j<col;j++)
			{
				A[i][j]=in.nextInt();
			}


		int row_second=in.nextInt();
		int col_second=in.nextInt();

		//读入B
		int [][]B=new int[row_second][col_second];
		for(int i=0;i<row_second;i++)
			for(int j=0;j<col_second;j++)
			{
				B[i][j]=in.nextInt();
			}
		int [][]C=times(A,B);
		return C;
	}

	/**
	 * 打印出该矩阵的数据
	 * 起始一个空行,结束一个空行
	 * 矩阵中每一行数据呈一行,数据间以空格隔开
	 * example:
	 *
	 * 1 2 3
	 * 1 2 3
	 * 1 2 3
	 * 1 2 3
	 *
	 */
	public void print(int[][] A) {
		// TODO
		System.out.println();
		for (int i = 0; i < A.length; i++) {
			String K = new String("");
			for (int j = 0; j < A[0].length - 1; j++) {
				K += A[i][j] + " ";
			}
			System.out.println(K + A[i][A[0].length - 1]);
		}
	}
}

这是第一版,完全的结构化编程
从控制台读入进行的时候,就要调用plus times方法即可

乘法

主要注意矩阵的算法(大一没有学高代的我)

public int[][] times(int[][] A, int[][] B){
		// TODO
		if (A.length!=0 && A[0].length!=0) {
			int[][] result = new int[A.length][B[0].length];
			for (int i = 0; i < A.length; i++)
				for (int j = 0; j < B[0].length; j++) {
					int sum=0;
					for(int k=0;k<A[0].length;k++){
						sum +=A[i][k]*B[k][j];  //三层循环 ik kj顺序容易搞错
					}
					result[i][j]=sum;
				}
			return result;
		}
		else
			return A;
	}

第二次



import java.util.Arrays;

/**
 * 矩阵类,实现矩阵的加法,矩阵乘法
 * 1.传入一个int[][]进行2个矩阵的操作
 * 2.返回一个int[][]
 * 所有的数据均为int型
 * 输入数据均默认为正确数据,不需要对输入数据进行校验
 * @author Qin Liu
 *
 */
public class BadMatrix {
	private int[][] data;
	
	/**
	 * 构造函数,参数为2维int数组
	 * a[i][j]是矩阵中的第i+1行,第j+1列数据
	 * @param a
	 */
	public BadMatrix(int[][] a){
		this.data = a;
	}

	public int[][] getData() {
		return data;
	}

	
	/**
	 * 实现矩阵加法,返回一个新的矩阵
	 * @param b
	 * @return
	 */
    public int[][] plus(int[][] b){
    	int[][] a = {
				{0, 0, 0},
				{0, 0, 0},
				{0, 0, 0}
		};
		int [][]A=getData();
		if(A.length!=0 && A[0].length!=0) {
			int [][]result=new int[A.length][A[0].length];
			for (int i = 0; i < A.length;i++)
				for(int j=0;j<A[0].length;j++)
				{
					result[i][j]=A[i][j]+b[i][j];
				}
				return result;
		}
		else
			return A;
	}
        
	
	/**
	 * 实现矩阵乘法,返回一个新的矩阵
	 * @param b
	 * @return
	 */
	public int[][] times(int[][] b){
		int[][] a = {
				{3, 3, 3},
				{3, 3, 3},
				{3, 3, 3}
		};
		int [][]A=getData();
		if(A.length!=0 && A[0].length!=0) {
			int [][]result=new int[A.length][b[0].length];
			for (int i = 0; i < A.length;i++)
				for(int j=0;j<b[0].length;j++) {
					int sum=0;
					for (int m = 0; m < A[0].length; m++) {
						sum += A[i][m] * b[m][j];
					}
					result[i][j]=sum;
				}
			return result;
		}
		else
			return A;
	}

	//不要修改下面print方法
	/**
	 * 打印出该矩阵的数据
	 * 
	 */
	public void print(){
		System.out.print(this.toString());
	}

	/**
	 * 实现toString方法
	 * 起始一个空行,结束一个空行
	 * 矩阵中每一行数据呈一行,数据间以空格隔开
	 * example:
	 *
	 * 1 2 3
	 * 1 2 3
	 * 1 2 3
	 * 1 2 3
	 *
	 */
	public String toString(){
		String lineBreak=System.getProperty("line.separator");
		String temp=new String("");
		for(int i=0;i<this.getData().length;i++)
		{
			temp=temp+lineBreak;
			temp=temp+this.getData()[i][0];
			for(int j=1;j<this.getData()[0].length;j++)
			{
				temp=temp+" "+this.getData()[i][j];
			}
		}
		temp= temp + lineBreak;
		return temp;
		/*
		System.out.println();
		for(int i=0;i<data.length;i++)
			for(int j=0;j<data[0].length;j++)
			{
				if(j!=data[0].length-1)
					System.out.print(data[i][j]);
				else
					System.out.println(data[i][j]);
			}
		return "";*/
	}

	//不要修改下面equals方法
	public boolean equals(Object o){
		if(this.toString().equals(((BadMatrix)o).toString()))
			return true;
		else
			return false;
	}
}

这是对结构化编程的升级
这是内部已经存在了一个矩阵,通过new构造函数 通过getData的到里面的成员变脸 再进行赋值

MyMatrix

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 矩阵类,实现矩阵的加法,矩阵乘法,点乘以及转置方法
 * 其中加法和点乘方法需要有两种实现方式
 * 1.传入一个MyMatrix对象进行2个矩阵的操作
 * 2.从控制台(console)读入一个矩阵数据,再进行操作
 * 所有的数据均为int型
 * 输入数据均默认为正确数据,不需要对输入数据进行校验
 * @author Ray Liu & Qin Liu
 *
 */
public class MyMatrix {
	private int[][] data;
	private int rows;
	private int columns;

	/**
	 * 构造函数,参数为2维int数组
	 * a[i][j]是矩阵中的第i+1行,第j+1列数据
	 * @param a
	 */


	public MyMatrix(int[][] a){
		this.data = a;
		this.rows=a.length;
		if(a!=null)
		{
			this.columns=a[0].length;
		}
	}

	public  int getRows(){
		return this.rows;
	}

	public int getColumns(){
		return this.columns;
	}

	public int[][] getData() {
		return data;
	}

	/**
	 * 实现矩阵加法,返回一个新的矩阵
	 * @param B
	 * @return
	 */
	public MyMatrix plus(MyMatrix B){
		int [][] dateofB=B.getData();
		int [][] result=new int[rows][columns];
		for(int i=0;i<rows;i++){
			for(int j=0;j<columns;j++){
				result[i][j]=dateofB[i][j]+data[i][j];
			}
		}
		return new MyMatrix(result);
	}


	/**
	 * 实现矩阵乘法,返回一个新的矩阵
	 * @param B
	 * @return
	 */
	public MyMatrix times(MyMatrix B){
		int [][] dateofB=B.getData();
		int newColumns=B.getColumns();
		int [][]result=new int[rows][newColumns];

		for(int i=0;i<rows;i++){
			for(int j=0;j<newColumns;j++){
				int sum=0;
				for(int k=0;k<this.columns;k++){
					sum+=data[i][k]*dateofB[k][j];
				}
				result[i][j]=sum;
			}
		}
		return new MyMatrix(result);
	}

	/**
	 * 实现矩阵的点乘,返回一个新的矩阵
	 * @param b
	 * @return
	 */
	public MyMatrix times(int b){
		int [][]result=new int [rows][columns];
		for(int i=0;i<rows;i++)
			for(int j=0;j<columns;j++)
				result[i][j]=b*data[i][j];
		return new MyMatrix(result);
	}

	/**
	 * 实现矩阵的转置,返回一个新的矩阵
	 * @return
	 */
	public MyMatrix transpose(){
		int [][]result=new int [rows][columns];
		for(int i=0;i<rows;i++)
			for(int j=0;j<columns;j++)
				result[i][j]=data[j][i];
		return new MyMatrix(result);
	}

	/**
	 *从控制台读入一个MyMatrix
	 */
	public MyMatrix getMyMatrixFromConsole(){
		int row=0;
		int column=0;
		try{
			BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
			String oneLine=br.readLine();
			String []temp=oneLine.split(" ");
			row=Integer.parseInt(temp[0]);
			column=Integer.parseInt(temp[1]);

			int [][] result=new int[row][column];
			for(int i=0;i<row;i++) {

				oneLine = br.readLine();
				temp = oneLine.split(" ");
				for (int j = 0; j < column; j++) {
					result[i][j] = Integer.parseInt(temp[j]);
				}
			}

			return new MyMatrix(result);

		} catch (IOException e){
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 从控制台读入矩阵数据,进行矩阵加法,读入数据格式如下:
	 * m n
	 * m * n 的数据方阵,以空格隔开
	 * example:
	 * 4 3
	 * 1 2 3 
	 * 1 2 3
	 * 1 2 3
	 * 1 2 3
	 * 返回一个新的矩阵
	 * @return
	 */
	public MyMatrix plusFromConsole(){
		MyMatrix B=getMyMatrixFromConsole();
		return this.plus(B);
	}

	/**
	 * 输入格式同上方法相同
	 * 实现矩阵的乘法
	 * 返回一个新的矩阵
	 * @return
	 */
	public MyMatrix timesFromConsole(){
		MyMatrix B=getMyMatrixFromConsole();
		return this.times(B);
	}

	/**
	 * 打印出该矩阵的数据
	 * 起始一个空行,结束一个空行
	 * 矩阵中每一行数据呈一行,数据间以空格隔开
	 * example:
	 *
	 * 1 2 3
	 * 1 2 3
	 * 1 2 3
	 * 1 2 3
	 *
	 */
	public void print(){
		System.out.println();
		for(int i=0;i<rows;i++){
			for (int j=0;j<columns;j++){
				System.out.print(data[i][j]);
				if(j<columns-1)
					System.out.print(" ");
			}
			System.out.println();
		}
		System.out.println();
	}
}

面向对象的产物
结构化编程思想 A+B
而面向对象是 **A.plus(B)**根据这个对象 通过他的方法得到数值进行计算
语义也更加清楚,并且得到的是一个新的矩阵对象,不会随着里面的某些特定值的变化而轻易改变。
传入得到的都是对象本身,那么相比起badMatrix只是这个对象本身与输入的二维数组运算。
但是虽然已经有数据了,但是与对外的接口还是显得生硬
相比起来第三个就直接创建两个对象,对象之间直接进行操作,更加清晰。

猜你喜欢

转载自blog.csdn.net/m0_46243503/article/details/107495657
今日推荐