LintCode 454. 矩阵面积

论坛 期权论坛 脚本     
已经匿名di用户   2022-5-29 19:04   579   0

描述

实现一个矩阵类Rectangle,包含如下的一些成员变量与函数:

  1. 两个共有的成员变量 widthheight 分别代表宽度和高度。
  2. 一个构造函数,接受2个参数 width 和 height 来设定矩阵的宽度和高度。
  3. 一个成员函数 getArea,返回这个矩阵的面积。

样例

样例1:
Java:
    Rectangle rec = new Rectangle(3, 4);
    rec.getArea(); // should get 12,3*4=12

Python:
    rec = Rectangle(3, 4)
    rec.getArea()

样例2:
Java:
    Rectangle rec = new Rectangle(4, 4);
    rec.getArea(); // should get 16,4*4=16

Python:
    rec = Rectangle(4, 4)
    rec.getArea()

没啥好说的,直接上代码

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 w,int h)
    {
        width=w;
        height=h;
    }
    /*
     * 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;
    }
}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP