java矩形类的实现.doc

java矩形类的实现.doc

ID:57646078

大小:63.00 KB

页数:5页

时间:2020-08-30

java矩形类的实现.doc_第1页
java矩形类的实现.doc_第2页
java矩形类的实现.doc_第3页
java矩形类的实现.doc_第4页
java矩形类的实现.doc_第5页
资源描述:

《java矩形类的实现.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、附页:(1)源代码及结果:importjava.io.*;classRectangle{intwidth;intheight;Rectangle(){}Rectangle(intw,inth){width=w;height=h;}publicintgetArea(){returnwidth*height;}publicintgetPerimeter(){returnwidth*2+height*2;}}classsquareextendsRectangle{intwidth;intheight;square(intw,inth){width=w;height=

2、h;}publicintgetPerimeter(){returnwidth*4;}publicintgetArea(){returnwidth*width;}}publicclassUseValue{publicstaticvoidmain(Stringargs[]){intm,n;try{System.out.print("请输入长:");BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));Stringp=br.readLine();m=Integer.parseInt(p

3、);System.out.print("请输入宽:");BufferedReaderbr1=newBufferedReader(newInputStreamReader(System.in));Stringq=br1.readLine();n=Integer.parseInt(q);if(m==n){squaresquare1=newsquare(m,n);System.out.println("该图形为正方形");System.out.print("其面积为:"+square1.getArea()+";");System.out.print("其周长为:"+

4、square1.getPerimeter()+";");}else{Rectanglerect1=newRectangle(m,n);System.out.print("该图形为矩形");System.out.print("其面积为:"+rect1.getArea()+";");System.out.print("其周长为:"+rect1.getPerimeter()+";");}}catch(IOExceptione){}}}(2)源代码及结果:importjava.io.*;interfacePrintable{publicvoidprintItMyWay

5、();publicvoidprintItMyWay(chara);}classRectangleimplementsPrintable{intwidth;intheight;Rectangle(){}Rectangle(intw,inth){width=w;height=h;}publicintgetArea(){returnwidth*height;}publicintgetPerimeter(){returnwidth*2+height*2;}publicvoidprintItMyWay(){System.out.print("其长为:"+width+";

6、");System.out.print("其宽为:"+height+";");System.out.print("其面积为:"+(width*height)+";");System.out.println("其周长为:"+(width*2+height*2)+";");}publicvoidprintItMyWay(chara){for(inti=1;i<=height;i++){for(intj=1;j<=width;j++){System.out.print(a);}System.out.println("");}}}classsquareextendsR

7、ectangle{intwidth;intheight;square(intw,inth){width=w;height=h;}publicintgetPerimeter(){returnwidth*4;}publicintgetArea(){returnwidth*width;}publicvoidprintItMyWay(){System.out.print("其边长为:"+width+";");System.out.print("其面积为:"+(width*width)+";");System.out.println("其周长为:"+(width*4)+

8、";");}publicvoidpri

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。