课外天地 李树青学习天地Java程序语言课件 → 作业之二:矩形类


  共有16719人关注过本帖树形打印复制链接

主题:作业之二:矩形类

帅哥哟,离线,有人找我吗?
admin
  1楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1940 积分:26616 威望:0 精华:34 注册:2003/12/30 16:34:32
作业之二:矩形类  发帖心情 Post By: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编辑过]

 回到顶部