




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实验课程名称:Java语言程序设计A实验项目名称实验3:接口实验成绩实 验 者专业班级组 别同 组 者无开始日期第一部分:实验预习报告(包括实验目的及意义,实验基本原理与方法,主要仪器设备及耗材,实验内容及要求,实验方案与技术路线等)一实验目的及意义1自定义接口。2自定义类实现接口。3接口及实现类的多态处理。二实验基本原理与方法1接口的概念。2接口对多态的支持。三主要仪器设备及耗材1PC及其兼容机。2计算机操作系统。3程序编辑器EditPlus/Eclipse。4Java开发工具JDK。四实验内容及要求自定义形状接口Shape,该接口声明了计算面积、周长的方法。然后,分别编写三角形类Trian
2、gle、六边形类Hexagon、椭圆形类Ellipse,它们都实现了Shape接口。最后,编写测试类ShapesDemo,多态地创建各种形状对象,计算面积、周长。五实验方案及技术路线(含各种形状面积、周长的计算公式,UML类图,注意事项) 因为每种形状的面积、周长计算所需要的参数个数不同,并且不同类型的三角形计算周长的面积的方法也不同,所以抽象类的参数就定为可变长度集合ArrayList,一般三角形的面积S=a*h/2,周长L=a+b+c;直角三角形面积S=a*b,周长L=a+b+,等边三角形的面积S=,周长L=3*a;六边形的面积S=,周长L=6*a。以下是简略的UML类图:1)接口Shap
3、e2) 三角形类Triangle3) 六边形类4) 椭圆形类第二部分:实验过程记录(可加页)(代码、运行结果、实验中出现的问题及解决过程)n Shape接口:import java.util.List;public interface Shapepublic double culArea(List<Double> list);public double culGirth(List<Double> list);n 六边形类Hexagon:import java.util.*;public class Hexagon implements Shape private dou
4、ble a;List<Double> listData=new ArrayList<Double>();public Hexagon(double a) this.a = a;listData.add(a); Overridepublic double culArea(List<Double> list) double s=0;s=Math.sqrt(3)*3*Math.pow(list.get(0), 2)/2;return s;Overridepublic double culGirth(List<Double> list) double l
5、=0;l=list.get(0)*6;return l;public List<Double> getListData() return listData;n 三角形类Triangle:import java.util.*;public class Triangle implements Shape private double a;private double b;private double c;private double h;List<Double> listData=new ArrayList<Double>();public Triangle(d
6、ouble a)this.a = a;listData.add(1.0);listData.add(a);public Triangle(double a, double b) this.a = a;this.b = b;listData.add(2.0);listData.add(a);listData.add(b);public Triangle(double a, double b, double c, double h) super();this.a = a;this.b = b;this.c = c;this.h = h;listData.add(3.0);listData.add(
7、a);listData.add(b);listData.add(c);listData.add(h);public List<Double> getListData()return listData;public void setListData(List<Double> listData) this.listData = listData;Overridepublic double culArea(List<Double> list)double s=0;if(list.get(0)=1.0)s=Math.sqrt(3)*Math.pow(list.get
8、(1), 2)/4;if(list.get(0)=2.0)s=list.get(1)*list.get(2)/2;if(list.get(0)=3.0)s=list.get(1)*list.get(4)/2;return s;Overridepublic double culGirth(List<Double> list) double l=0;if(list.get(0)=1.0)l=3*list.get(1);if(list.get(0)=2.0) l=list.get(1)+list.get(2)+Math.sqrt(Math.pow(list.get(1), 2)+Math
9、.pow(list.get(2), 2);if(list.get(0)=3.0) l=list.get(1)+list.get(2)+list.get(3); return l; n 测试类ShapesDemo:public class ShapesDemo public static void main(String args)menuStrip(); public static void menuStrip() Scanner sc = new Scanner(System.in);String choice = null;do System.out.println("选择需要计
10、算面积和周长的图形形状。");System.out.println("1.三角形");System.out.println("2.正六边形");System.out.println("3.椭圆形");System.out.println("4.退出");System.out.println("请输入选项【1-4】");choice = sc.next();switch (choice) case "1":option1();break;case "2&qu
11、ot;:option2();break;case "3":option3();break;case "4":System.exit(0);default:System.err.println("输入错误!"); menuStrip(); while (!(choice.equals("4");private static void option1() Scanner sc1=new Scanner(System.in);String tempChoice=null;System.out.println("
12、请选择需要三角形的类型。");System.out.println("1.等边三角形");System.out.println("2.直角形");System.out.println("3.普通");System.out.println("请输入选项【1-3】(返回上一级请输入'0')");tempChoice=sc1.next();if(tempChoice.equals("1") try for(;)System.out.print("请输入等边三角形的边
13、长:");double aIn=sc1.nextDouble();if(aIn>0)Triangle triangle1=new Triangle(aIn);double area=triangle1.culArea(triangle1.getListData();double girth=triangle1.culGirth(triangle1.getListData();System.out.println("此三角形的面积为:"+area+"n此三角形的周长为:"+girth);break;elseSystem.err.printl
14、n("输入错误,请输入大于0的数值!"); catch (Exception e) System.err.println("输入错误,请重新输入!");option1(); else if(tempChoice.equals("2")try for(;)System.out.print("请输入一条直角边长:");double aIn=sc1.nextDouble();System.out.print("请输入另一条直角边长:");double bIn=sc1.nextDouble();if(a
15、In>0&&bIn>0)Triangle triangle1=new Triangle(aIn,bIn);double area=triangle1.culArea(triangle1.getListData();double girth=triangle1.culGirth(triangle1.getListData();System.out.println("此三角形的面积为:"+area+"n此三角形的周长为:"+girth);break;elseSystem.err.println("输入错误,请输入大于0的
16、数值!"); catch (Exception e) System.err.println("输入错误,请重新输入!");option1(); else if(tempChoice.equals("3")try for(;)System.out.print("请输入三角形底边长:");double aIn=sc1.nextDouble();System.out.print("请输入高:");double hIn=sc1.nextDouble();System.out.print("请输入三角形一
17、条侧边边长:");double bIn=sc1.nextDouble();System.out.print("请输入三角形另一条侧边边长:");double cIn=sc1.nextDouble();if(aIn>0&&bIn>0&&cIn>0&&hIn>0)if(aIn+bIn)>cIn&&(aIn+cIn)>bIn&&(bIn+cIn)>aIn)Triangle triangle1=new Triangle(aIn,bIn,cIn,hIn)
18、;double area=triangle1.culArea(triangle1.getListData();double girth=triangle1.culGirth(triangle1.getListData();System.out.println("此三角形的面积为:"+area+"n此三角形的周长为:"+girth);break;elseSystem.err.println("输入错误!不能构成三角形!请重新输入数.");elseSystem.err.println("输入错误,请输入大于0的数值!"
19、); catch (Exception e) System.err.println("输入错误,请重新输入!");option1(); else if(tempChoice.equals("0")menuStrip();elseSystem.err.println("输入错误!");String c=reChoice();if(c.equals("1")option1();else/返回主菜单private static void option2()Scanner sc2=new Scanner(System.in
20、);String c=reChoice();if(c.equals("1")try for(;)System.out.print("请输入正六边形的边长:");double aIn=sc2.nextDouble();if(aIn>0)Hexagon hexagon=new Hexagon(aIn);double area=hexagon.culArea(hexagon.getListData();double girth=hexagon.culGirth(hexagon.getListData();System.out.println("
21、此正六边形的面积为:"+area+"n此正六边形的周长为:"+girth);break;elseSystem.err.println("输入错误,请输入大于0的数值!"); catch (Exception e) System.err.println("输入错误,请重新输入!");option2();else/返回主菜单menuStrip();private static void option3()Scanner sc3=new Scanner(System.in);String c=reChoice();if(c.equ
22、als("1")try for(;)System.out.print("请输入椭圆长半轴长:");double aIn=sc3.nextDouble();System.out.print("请输入椭圆短半轴长:");double bIn=sc3.nextDouble();if(aIn>0&&bIn>0)if(aIn>bIn)Ellipse ellipse=new Ellipse(aIn,bIn);double area=ellipse.culArea(ellipse.getListData();dou
23、ble girth=ellipse.culGirth(ellipse.getListData();System.out.println("此椭圆形的面积为:"+area+"n此椭圆的周长为:"+girth);break;elseSystem.err.println("输入错误,长半轴长度小于短半轴,请重新您输入!");elseSystem.err.println("输入错误,请输入大于0的数值!"); catch (Exception e) System.err.println("输入错误,请重新输入!");optio
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 工业模具设计版权转让与全球市场拓展、技术支持及售后服务补充合同
- 网红网红孵化与粉丝经济合作协议
- 电信网络设备维修人员派遣合作协议
- 拉美航空客运与货运代理合作协议
- 绿色建筑施工安全责任承诺协议
- 伊犁河谷小麦茎基腐病病原鉴定及其致病性研究
- 社交网络用户个人信息保密与网络安全保障合同
- 影视场刊印刷油墨租赁与快速响应服务合同
- 互联网公益项目多元交代研究
- 砖木古建筑结构的动力特性识别-Saat Tale Durbar为例
- 粮油配送项目服务承诺及售后服务
- 公司内部文件管理规定及办法
- 2024-2025中国服装行业科技创新白皮书
- 道路安全交通课课件
- 眼科住院及手术患者安全
- 数字化转型对企业人力资本的影响研究
- 保密基本知识培训材料范文
- 公开征集招标代理机构投标方案(技术方案)
- 信息系统安全等级保护等级测评报告模板【等保2.0】
- 《荣安地产公司财务风险研究与防范研究(定量论文)》8200字
- 【MOOC】理性思维实训-华南师范大学 中国大学慕课MOOC答案
评论
0/150
提交评论