收藏 分销(赏)

Java语言程序设计A实验3:接口.doc

上传人:丰**** 文档编号:3107274 上传时间:2024-06-18 格式:DOC 页数:14 大小:199KB
下载 相关 举报
Java语言程序设计A实验3:接口.doc_第1页
第1页 / 共14页
Java语言程序设计A实验3:接口.doc_第2页
第2页 / 共14页
Java语言程序设计A实验3:接口.doc_第3页
第3页 / 共14页
Java语言程序设计A实验3:接口.doc_第4页
第4页 / 共14页
Java语言程序设计A实验3:接口.doc_第5页
第5页 / 共14页
点击查看更多>>
资源描述

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 list);public double culGirth(List list);n 六边形类Hexagon:import java.util.*;public class Hexagon implements Shape private double a;List listData=new Arra

4、yList();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

5、: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.

6、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 listD

7、ata) 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

8、(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(); pub

9、lic 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

10、 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

11、(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(triangle

12、1.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(请输入一条直角边长:)

13、;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此三角形的周长为:+girt

14、h);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 b

15、In=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.getListDat

16、a();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()

17、;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 g

18、irth=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

19、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.g

20、etListData();System.out.println(此椭圆形的面积为:+area+n此椭圆的周长为:+girth);break;elseSystem.err.println(输入错误,长半轴长度小于短半轴,请重新您输入!);elseSystem.err.println(输入错误,请输入大于0的数值!); catch (Exception e) System.err.println(输入错误,请重新输入!);option3();else/返回主菜单menuStrip();private static String reChoice()Scanner sc4=new Scanner(S

21、ystem.in);String tempSelect;for (;) System.out.println(是否要继续计算?n + 1继续计算. 2返回主菜单.n+ 请输入选择【1-2】:);tempSelect = sc4.next();if(tempSelect.equals(1)|tempSelect.equals(2)break;elseSystem.err.println(错误选项!请重新选择!);/继续循环 return tempSelect;运行结果如下:三角形的计算计算六边形:计算椭圆形:教师签字_第三部分 结果与讨论(可加页)一、 实验结果分析(包括数据处理、影响因素讨论、

22、综合分析和结论等) 本例中的测试类与之前做的银行账号中的测试类运用的思想相同。实验中因为考虑各种形状计算面积和周长参数个数不同的问题,所以想的比较复杂,其实可以忽略这个问题,在接口Shape的方法中不定义参数,在三角形类,椭圆形类以及六边形类中可以直接使用类中的实例变量进行计算,这样在实现计算的同时也可减小代码的复杂度。因为三角形有多种计算方式,所以程序中对于不同的三角形,在创建实例的时候使用了不同的构造函数。二、小结、建议及体会 虽然只是一个简单的程序设计题,但在做的过程中要尽量考虑现实中的情况,考虑多种输入可能,考虑各种可能存在的找错(比如输入的不是数值、或输入的数值小于0、或输入的三条边不能构成三角形等等)。

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 通信科技 > 开发语言

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服