Rectangle Area

题目:

Implement a Rectangle class which include the following attributes and methods:

Two public attributes width and height.
A constructor which expects two parameters width and height of type int.
A method getArea which would calculate the size of the rectangle and return.

分析:

类的成员有四种public > default > protected > private

其中什么都不写就是default,是在同一个package中可以访问到

解法:

public class Rectangle {
    /*
     * Define two public attributes width and height of type int.
     */
    // write your code here
    int width;
    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 width * height;
    }
}

results matching ""

    No results matching ""