欢迎来到天天文库
浏览记录
ID:62033594
大小:123.50 KB
页数:8页
时间:2021-04-15
《java入门程序题及源码4.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、一.实验目的了解接口,类,抽象类之间的不同,能够在其之间进行继承与实现ﻩﻩ掌握文件的输入输出流,包括scanner类,字符流,字节流等ﻩ等的ﻩ运用,能够对文件进行基本的读出写入二.实验内容ﻩ实验题目一: 设计(1)形状接口、(2)积木块抽象类、(3)三角形类、正方形类、圆形类和矩形类,ﻫ其中(2)实现了(1),(3)继承了(2),然后编写主函数分别定义它们的对象,并显示其值。1.程序源代码 publicclasstitle4_1{ publicstaticvoidmain(String[]args){ Shapecircle=newCircle(20),rect
2、=newRectangle(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();}}//形状接口interface Shape{public doublegetArea();//求面积 publicdoubl
3、egetCircle();//求周长 publicvoidshowArea();publicvoidshowCircle();} //积木块抽象类 abstractclass BuildingBlockimplements Shape{publicvoid showArea(){ } publicvoidshowCircle(){ }} //圆类 classCircleextendsBuildingBlock{private double r; publicCircle(doubler){this。r=r;} //求面积 publicdoublegetArea(
4、) { returnMath。PI*this。r*this。r;} //求周长public doublegetCircle(){ return2*this.r*Math。PI; }publicvoidshowArea(){System.out.println("圆的半径为:"+this。r); System.out。println(”圆面积:”+this.getArea()); } publicvoid showCircle(){ System。out.println("圆周长:"+this。getCircle());System.out。println();} }
5、//定义长方形 classRectangleextends BuildingBlock{private doublel,w;public Rectangle(doublel,doublew){ this.w=w;this.l=l;}publicdoublegetArea(){ returnthis。w*this.l;}public double getCircle(){return 2*(this.w+this.l); }publicvoidshowArea(){ System.out。println(”长方形的长为:"+this。l+"宽为:"+this.w);
6、 System.out。println("长方形面积:"+this.getArea());}publicvoidshowCircle(){ System.out.println("长方形周长:"+this。getCircle()); System。out。println();} }//定义正方形classSquareextendsBuildingBlock{privatedoublea; publicSquare(doublea){ this.a=a;} publicdoublegetArea(){return this.a*this.a; }public dou
7、blegetCircle(){return4*this.a;}public void showArea(){ System.out.println(”正方形的边长为:"+this。a); System.out.println("正方形面积:"+this.getArea());} publicvoidshowCircle(){System。out.println("正方形周长:”+this.getCircle()); System。out.println();}} //定义三角形classTriangleextendsBuildingBlock{ privated
此文档下载收益归作者所有