




免费预览已结束,剩余11页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
精选文库实验课程名称:Java语言程序设计A实验项目名称实验3:接口实验成绩实 验 者专业班级组 别同 组 者无开始日期第一部分:实验预习报告(包括实验目的及意义,实验基本原理与方法,主要仪器设备及耗材,实验内容及要求,实验方案与技术路线等)一实验目的及意义1自定义接口。2自定义类实现接口。3接口及实现类的多态处理。二实验基本原理与方法1接口的概念。2接口对多态的支持。三主要仪器设备及耗材1PC及其兼容机。2计算机操作系统。3程序编辑器EditPlus/Eclipse。4Java开发工具JDK。四实验内容及要求自定义形状接口Shape,该接口声明了计算面积、周长的方法。然后,分别编写三角形类Triangle、六边形类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)接口Shape2) 三角形类Triangle3) 六边形类4) 椭圆形类第二部分:实验过程记录(可加页)(代码、运行结果、实验中出现的问题及解决过程)n Shape接口:import java.util.List;public interface Shapepublic double culArea(List list);public double culGirth(List list);n 六边形类Hexagon:import java.util.*;public class Hexagon implements Shape private double a;List listData=new ArrayList();public Hexagon(double a) this.a = a;listData.add(a); Overridepublic double culArea(List list) double s=0;s=Math.sqrt(3)*3*Math.pow(list.get(0), 2)/2;return s;Overridepublic double culGirth(List list) double l=0;l=list.get(0)*6;return l;public List 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 listData=new ArrayList();public Triangle(double 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(a);listData.add(b);listData.add(c);listData.add(h);public List getListData()return listData;public void setListData(List listData) this.listData = listData;Overridepublic double culArea(List list)double s=0;if(list.get(0)=1.0)s=Math.sqrt(3)*Math.pow(list.get(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 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.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(选择需要计算面积和周长的图形形状。);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: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(请选择需要三角形的类型。);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(请输入等边三角形的边长:);double aIn=sc1.nextDouble();if(aIn0)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.println(输入错误,请输入大于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(aIn0&bIn0)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的数值!); 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(请输入三角形一条侧边边长:);double bIn=sc1.nextDouble();System.out.print(请输入三角形另一条侧边边长:);double cIn=sc1.nextDouble();if(aIn0&bIn0&cIn0&hIn0)if(aIn+bIn)cIn&(aIn+cIn)bIn&(bIn+cIn)aIn)Triangle triangle1=new Triangle(aIn,bIn,cIn,hIn);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的数值!); 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);String c=reChoice();if(c.equals(1)try for(;)System.out.print(请输入正六边形的边长:);double aIn=sc2.nextDouble();if(aIn0)Hexagon hexagon=new Hexagon(aIn);double area=hexagon.culArea(hexagon.getListData();double girth=hexagon.culGirth(hexagon.getListData();System.out.println(此正六边形的面积为:+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.equals(1)try for(;)System.out.print(请输入椭圆长半轴长:);double aIn=sc3.nextDouble();System.out.print(请输入椭圆短半轴长:);double bIn=sc3.nextDouble();if(aIn0&bIn0)if(aInbIn)Ellipse ellipse=new Ellipse(aIn,bIn);double area=ellipse.culArea(ellipse.getListData();double 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(输入错误,请重新输入!);option3();else/返回主菜单menuStrip();private s
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 疫源地消毒管理办法
- 云南招投标管理办法
- 桌前椅配备管理办法
- 浙江混凝土管理办法
- 中石油保密管理办法
- 生鲜rdc管理办法
- 税务学会费管理办法
- 班委对班级管理办法
- 爱心伞借用管理办法
- 盾构机维修管理办法
- 无精子症的诊疗策略
- 初中校本课程-《海洋教育》云雨的故乡教学课件设计
- GB/T 3098.15-2023紧固件机械性能不锈钢螺母
- 英语字贴冀教版小学英语三年级上册单词和重点句型衡水体练习字帖
- 二升三暑期奥数培优(学生教材)
- 宁波寰球“新扩改建”项目三查四定培训
- 泌尿系统疾病,常见症状体征护理
- GB/T 3956-2008电缆的导体
- GB/T 28789-2012视频交通事件检测器
- GB/T 18380.11-2022电缆和光缆在火焰条件下的燃烧试验第11部分:单根绝缘电线电缆火焰垂直蔓延试验试验装置
- GB/T 14502-1993水中镍-63的分析方法
评论
0/150
提交评论