Rss & SiteMap

课外天地 李树青 http://www.njcie.com

李树青 论坛 南京 财经 课外天地
共1 条记录, 每页显示 10 条, 页签: [1]
[浏览完整版]

标题:作业之二:矩形类

1楼
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编辑过]
共1 条记录, 每页显示 10 条, 页签: [1]

Copyright ©2002 - 2016 课外天地.Net
Powered By Dvbbs Version 8.3.0
Processed in .01563 s, 2 queries.