美文网首页
LintCode入门级-1

LintCode入门级-1

作者: ___shin | 来源:发表于2017-11-01 21:15 被阅读0次

描述:
实现一个矩阵类Rectangle,包含如下的一些成员变量与函数:
1.两个共有的成员变量 width 和 height 分别代表宽度和高度。
2.一个构造函数,接受2个参数 width 和 height 来设定矩阵的宽度和高度。
3.一个成员函数 getArea,返回这个矩阵的面积。

样例:

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

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

实现:

public class Rectangle {
    /*
     * Define two public attributes width and height of type int.
     */
    // write your code here
    private int width;
    private int height;
    private int area;

    /*
     * 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(){
        this.area=width*height;
        return area;
    }
}

相关文章

  • LintCode入门级-1

    描述:实现一个矩阵类Rectangle,包含如下的一些成员变量与函数:1.两个共有的成员变量 width 和 he...

  • LintCode入门级-3

    描述:给一组整数,按照升序排序,使用选择排序,冒泡排序,插入排序或者任何 O(n2) 的排序算法。样例:对于数组 ...

  • LintCode入门级-2

    描述:在二叉树中寻找值最大的节点并返回。样例:给出如下一棵二叉树: 返回值为 3 的节点。实现:

  • LintCode入门级-4

    描述:删除链表中等于给定值val的所有节点。样例:给出链表 1->2->3->3->4->5->3, 和 val ...

  • LintCode入门级-5

    描述:查找斐波纳契数列中第 N 个数。 所谓的斐波纳契数列是指:前2个数是 0 和 1 。第 i 个数是第 i-1...

  • 程序员常用的刷题网站

    1、Lintcode Lintcode.com——LintCode网站是国内较大的在线编程&测评网站。此网站提供各...

  • [入门级]lintcode--刷题

    第一题 第二题 第三题 第四题 第五题 第六题

  • LintCode问题图解-1

    本文准备讲解1个简单的算法编程问题, 这个算法编程问题来自LintCode平台。不了解.LintCode平台的读...

  • LintCode问题图解-61

    本文准备讲解1个简单的算法编程问题, 这个算法编程问题来自LintCode平台。不了解.LintCode平台的读者...

  • LintCode问题图解-49

    本文准备讲解1个算法编程问题, 这个算法编程问题来自LintCode平台。不了解.LintCode平台的读者可以阅...

网友评论

      本文标题:LintCode入门级-1

      本文链接:https://www.haomeiwen.com/subject/zfcnpxtx.html