2017.09.03
實(shí)現(xiàn)一個(gè)矩陣類Rectangle柱恤,包含如下的一些成員變量與函數(shù):
兩個(gè)共有的成員變量 width 和 height 分別代表寬度和高度藏雏。
一個(gè)構(gòu)造函數(shù)求豫,接受2個(gè)參數(shù) width 和 height 來設(shè)定矩陣的寬度和高度。
一個(gè)成員函數(shù) getArea,返回這個(gè)矩陣的面積
java代碼實(shí)現(xiàn)
public class Rectangle {
/*
* Define two public attributes width and height of type int.
*/
// write your code here
/*
* Define a constructor which expects two parameters width and height here.
*/
// write your code here
/*
* Define a public method `getArea` which can calculate the area of the
* rectangle and return.
*/
// write your code here
int width;
int height;
Rectangle(int x1,int x2) {
width=x1;
height=x2;
}
public int getArea() {
return width*height;
}
public static void main(String args[]) {
Rectangle rec=new Rectangle(4,4);
int area=rec.getArea();
System.out.print(area);
}
}