欢迎来到天天文库
浏览记录
ID:50826809
大小:123.00 KB
页数:8页
时间:2020-03-15
《java入门程序题及源码.doc》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、一.实验目的了解接口,类,抽象类之间的不同,能够在其之间进行继承与实现掌握文件的输入输出流,包括scanner类,字符流,字节流等等的运用,能够对文件进行基本的读出写入二.实验内容实验题目一: 设计(1)形状接口、(2)积木块抽象类、(3)三角形类、正方形类、圆形类和矩形类,其中(2)实现了(1),(3)继承了(2),然后编写主函数分别定义它们的对象,并显示其值。1.程序源代码publicclasstitle4_1{publicstaticvoidmain(String[]args){Shapecircle=newCircle(20),rect=newRectangle
2、(100,50);Shapesquare=newSquare(10),tri=newTriangle(10,20,25);circle.showArea();circle.showCircle();rect.showArea();rect.showCircle();square.showArea();square.showCircle();tri.showArea();tri.showCircle();}}//形状接口interfaceShape{publicdoublegetArea();//求面积publicdoublegetCircle();//求周长public
3、voidshowArea();publicvoidshowCircle();}//积木块抽象类abstractclassBuildingBlockimplementsShape{publicvoidshowArea(){}publicvoidshowCircle(){}}//圆类classCircleextendsBuildingBlock{privatedoubler;publicCircle(doubler){this.r=r;}//求面积publicdoublegetArea(){returnMath.PI*this.r*this.r;}//求周长publicdo
4、ublegetCircle(){return2*this.r*Math.PI;}publicvoidshowArea(){System.out.println("圆的半径为:"+this.r);System.out.println("圆面积:"+this.getArea());}publicvoidshowCircle(){System.out.println("圆周长:"+this.getCircle());System.out.println();}}//定义长方形classRectangleextendsBuildingBlock{privatedoublel,w
5、;publicRectangle(doublel,doublew){this.w=w;this.l=l;}publicdoublegetArea(){returnthis.w*this.l;}publicdoublegetCircle(){return2*(this.w+this.l);}publicvoidshowArea(){System.out.println("长方形的长为:"+this.l+"宽为:"+this.w);System.out.println("长方形面积:"+this.getArea());}publicvoidshowCircle(){Syst
6、em.out.println("长方形周长:"+this.getCircle());System.out.println();}}//定义正方形classSquareextendsBuildingBlock{privatedoublea;publicSquare(doublea){this.a=a;}publicdoublegetArea(){returnthis.a*this.a;}publicdoublegetCircle(){return4*this.a;}publicvoidshowArea(){System.out.println("正方形的边长为:"+thi
7、s.a);System.out.println("正方形面积:"+this.getArea());}publicvoidshowCircle(){System.out.println("正方形周长:"+this.getCircle());System.out.println();}}//定义三角形classTriangleextendsBuildingBlock{privatedoublea,b,c,q;publicTriangle(doublea,doubleb,doublec){this.a=a;this.b=b;this.c=c;t
此文档下载收益归作者所有