以文本方式查看主题

-  课外天地 李树青  (http://www.njcie.com/bbs/index.asp)
--  Java程序语言课件  (http://www.njcie.com/bbs/list.asp?boardid=17)
----  作业之二:矩形类  (http://www.njcie.com/bbs/dispbbs.asp?boardid=17&id=591)

--  作者:admin
--  发布时间:2008/11/4 10:49:47
--  作业之二:矩形类

class Matrix {
        private int rows;

        private int cols;

        private int values[][];

        public Matrix() {
        }

        public Matrix(int rows, int cols) {
                this.rows = (rows > 0) ? rows : 1;
                this.cols = (cols > 0) ? cols : 1;
                values = new int[this.rows][this.cols];
        }

        public int getRows() {
                return rows;
        }

        public void setRows(int rows) {
                this.rows = (rows > 0) ? rows : 1;
                values = new int[this.rows][this.cols];
        }

        public int getCols() {
                return cols;
        }

        public void setCols(int cols) {
                this.cols = (cols > 0) ? cols : 1;
                values = new int[this.rows][this.cols];
        }

        public void setElement(int row, int col, int value) {
                values[row][col] = value;
        }

        public String toString() {
                String result = "";
                for (int row = 0; row < rows; row++) {
                        for (int col = 0; col < cols; col++) {
                                result += values[row][col] + "\\t";
                        }
                        result += "\\n";
                }
                return result;
        }
}

public class Exec {
        public static void main(String args[]) {
                Matrix m1 = new Matrix(3, 4);
                m1.setElement(0, 0, 12);
                System.out.print(m1);
        }
}

[此贴子已经被作者于2010-12-12 08:28:00编辑过]