您现在的位置是:首页 > 文章详情

2-1 类的创建

日期:2018-07-12点击:291

创建一个简单的表示矩形的Rectangle类,满足以下条件:
1、定义两个成员变量height和width,表示矩形的长和宽,类型为整型 2、定义一个getArea方法,返回矩形的面积 3、定义一个getPerimeter方法,返回矩形的周长 4、在main函数中,利用输入的2个参数分别作为矩形的长和宽,调用getArea和getPermeter方法,计算并返回矩形的面积和周长

输入:
输入2个正整数,中间用空格隔开,分别作为矩形的长和宽,例如:5 8

输出:
输出2个正整数,中间用空格隔开,分别表示矩形的面积和周长,例如:40 26

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int height = in.nextInt(); int width = in.nextInt(); Rectangle r = new Rectangle(height, width); System.out.println(r.getArea() + " " + r.getPermeter()); } } class Rectangle{ int height; int width; Rectangle(int h, int w){ height = h; width = w; } int getArea() { return height * width; } int getPermeter() { return height * 2 + width * 2; } } 
原文链接:https://yq.aliyun.com/articles/620720
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章