2022年Java语言程序设计A实验3:接口_第1页
2022年Java语言程序设计A实验3:接口_第2页
2022年Java语言程序设计A实验3:接口_第3页
2022年Java语言程序设计A实验3:接口_第4页
2022年Java语言程序设计A实验3:接口_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、精选精选.精选.实验课程名称:Java语言程序设计A实验工程名称实验3:接口实验成绩实 验 者专业班级组 别同 组 者无开始日期第一局部:实验预习报告包括实验目的及意义,实验根本原理与方法,主要仪器设备及耗材,实验内容及要求,实验方案与技术路线等一实验目的及意义1自定义接口。2自定义类实现接口。3接口及实现类的多态处理。二实验根本原理与方法1接口的概念。2接口对多态的支持。三主要仪器设备及耗材1PC及其兼容机。2计算机操作系统。3程序编辑器EditPlus/Eclipse。4Java开发工具JDK。四实验内容及要求自定义形状接口Shape,该接口声明了计算面积、周长的方法。然后,分别编写三角形

2、类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接口S

3、hape精选精选.精选.三角形类Triangle六边形类椭圆形类第二局部:实验过程记录可加页代码、运行结果、实验中出现的问题及解决过程Shape接口:精选精选.精选.import java.util.List;public interface Shapepublic double culArea(List list);public double culGirth(List list);六边形类Hexagon:import java.util.*;public class Hexagon implements Shape private double a;List listData=new Ar

4、rayList();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;三角形类

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

6、;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 voi

7、d 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(

8、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; 测试类ShapesDemo:public class ShapesDemo public static void mai

9、n(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】);c

10、hoice = 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.pr

11、intln(请选择需要三角形的类型。);精选精选.精选.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 Triang

12、le(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(tem

13、pChoice.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.getListD

14、ata();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(请输入高:);doubl

15、e 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(trian

16、gle1.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

17、.equals(0)menuStrip();else精选精选.精选.System.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 hexa

18、gon=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/返

19、回主菜单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

20、,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 static String reChoice()Scanner sc4=new Scanne

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论