題目
描述
實現(xiàn)一個矩陣類Rectangle,包含如下的一些成員變量與函數(shù):
- 兩個共有的成員變量 width 和 height 分別代表寬度和高度铡买。
- 一個構(gòu)造函數(shù)模孩,接受2個參數(shù) width 和 height 來設(shè)定矩陣的寬度和高度莺掠。
- 一個成員函數(shù) getArea,返回這個矩陣的面積牍汹。
樣例
Rectangle rec = new Rectangle(3, 4);
rec.getArea(); // should get 12
解答
太簡單了。不多說
代碼:
public class Rectangle {
/*
* Define two public attributes width and height of type int.
*/
// write your code here
public int width;
public int height;
/*
* Define a constructor which expects two parameters width and height here.
*/
// write your code here
public Rectangle(int width, int height){
this.width = width;
this.height = height;
}
/*
* Define a public method `getArea` which can calculate the area of the
* rectangle and return.
*/
// write your code here
public int getArea(){
return (this.width * this.height);
}
}