资源描述
实验课程名称:Java语言程序设计A
实验项目名称
实验3:接口
实验成绩
实 验 者
专业班级
组 别
同 组 者
无
开始日期
第一部分:实验预习报告(包括实验目的及意义,实验基本原理与方法,主要仪器设备及耗材,实验内容及要求,实验方案与技术路线等)
一.实验目的及意义
1.自定义接口。
2.自定义类实现接口。
3.接口及实现类的多态处理。
二.实验基本原理与方法
1.接口的概念。
2.接口对多态的支持。
三.主要仪器设备及耗材
1.PC及其兼容机。
2.计算机操作系统。
3.程序编辑器EditPlus/Eclipse。
4.Java开发工具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)接口Shape
2) 三角形类Triangle
3) 六边形类
4) 椭圆形类
第二部分:实验过程记录(可加页)(代码、运行结果、实验中出现的问题及解决过程)
n Shape接口:
import java.util.List;
public interface Shape
{
public double culArea(List<Double> list);
public double culGirth(List<Double> list);
}
n 六边形类Hexagon:
import java.util.*;
public class Hexagon implements Shape
{
private double a;
List<Double> listData=new ArrayList<Double>();
public Hexagon(double a)
{
this.a = a;
listData.add(a);
}
@Override
public double culArea(List<Double> list) {
double s=0;
s=Math.sqrt(3)*3*Math.pow(list.get(0), 2)/2;
return s;
}
@Override
public double culGirth(List<Double> list) {
double l=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(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<Double> getListData()
{return listData;}
public void setListData(List<Double> listData)
{
this.listData = listData;
}
@Override
public double culArea(List<Double> 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;}
@Override
public 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.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(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;
}
else
{System.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(aIn>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;}
else
{System.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(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);
double area=triangle1.culArea(triangle1.getListData());
double girth=triangle1.culGirth(triangle1.getListData());
System.out.println("此三角形的面积为:"+area+"\n此三角形的周长为:"+girth);
break;}
else
{System.err.println("输入错误!不能构成三角形!请重新输入数.");
}}
else
{System.err.println("输入错误,请输入大于0的数值!");
}}
} catch (Exception e)
{
System.err.println("输入错误,请重新输入!");
option1();
}}
else if(tempChoice.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(aIn>0)
{
Hexagon hexagon=new Hexagon(aIn);
double area=hexagon.culArea(hexagon.getListData());
double girth=hexagon.culGirth(hexagon.getListData());
System.out.println("此正六边形的面积为:"+area+"\n此正六边形的周长为:"+girth);
break;}
else
{System.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(aIn>0&&bIn>0)
{if(aIn>bIn){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;}
else
{System.err.println("输入错误,长半轴长度小于短半轴,请重新您输入!");
}}
else
{System.err.println("输入错误,请输入大于0的数值!");}}
} catch (Exception e)
{System.err.println("输入错误,请重新输入!");
option3();}}
else{//返回主菜单
menuStrip();}}
private static String reChoice()
{
Scanner sc4=new Scanner(System.in);
String tempSelect;
for (;;) {
System.out.println("是否要继续计算?\n" + "[1]继续计算. [2]返回主菜单.\n"
+ "请输入选择【1-2】:");
tempSelect = sc4.next();
if(tempSelect.equals("1")||tempSelect.equals("2"))
{break;}
else{System.err.println("错误选项!请重新选择!");
//继续循环
}
}
return tempSelect;
}
}
运行结果如下:
三角形的计算
计算六边形:
计算椭圆形:
教师签字__________
第三部分 结果与讨论(可加页)
一、 实验结果分析(包括数据处理、影响因素讨论、综合分析和结论等)
本例中的测试类与之前做的银行账号中的测试类运用的思想相同。实验中因为考虑各种形状计算面积和周长参数个数不同的问题,所以想的比较复杂,其实可以忽略这个问题,在接口Shape的方法中不定义参数,在三角形类,椭圆形类以及六边形类中可以直接使用类中的实例变量进行计算,这样在实现计算的同时也可减小代码的复杂度。因为三角形有多种计算方式,所以程序中对于不同的三角形,在创建实例的时候使用了不同的构造函数。
二、小结、建议及体会
虽然只是一个简单的程序设计题,但在做的过程中要尽量考虑现实中的情况,考虑多种输入可能,考虑各种可能存在的找错(比如输入的不是数值、或输入的数值小于0、或输入的三条边不能构成三角形等等)。
展开阅读全文