LintCode-454.Rectangle Area

来源:互联网 发布:添加windows凭据 编辑:程序博客网 时间:2024/05/20 16:12

URL:http://www.lintcode.com/en/problem/rectangle-area/

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

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

Rectangle rec = new Rectangle(3, 4);rec.getArea(); // should get 12
考查 Java面向对象的特征

public class Rectangle {    public int height;    public int width;    public Rectangle(int height, int width) {        this.height = height;        this.width = width;    }    public int getArea(){        return this.height * this.width;    }    public static void main(String[] args) {        Rectangle rectangle = new Rectangle(3, 4);        int area = rectangle.getArea();        System.out.println(area);    }}


原创粉丝点击