用接口设计并实现面积与周长计算.docx
- 文档编号:4036707
- 上传时间:2022-11-27
- 格式:DOCX
- 页数:16
- 大小:104.12KB
用接口设计并实现面积与周长计算.docx
《用接口设计并实现面积与周长计算.docx》由会员分享,可在线阅读,更多相关《用接口设计并实现面积与周长计算.docx(16页珍藏版)》请在冰豆网上搜索。
用接口设计并实现面积与周长计算
【任务2】用接口设计并实现面积与周长计算
【要求】①定义一个接口,其中包含一个计算面积的抽象方法和一个计算周长的抽象方法;②输入数据为圆的半径、三角形的三条边长、矩形的长和宽,对于用户的输入要有友好提示;③程序能够排除输入过程中的非法数据(如:
输入的长度数据为负数或字符;输入的三角形三条边数据不能组成三角形;④计算圆、三角形、矩形的面积和周长,并输出原始数据和结算结果。
【该实验属于给定题目和要求,学生自己设计方案并加以实现的实验,故属于设计性实验。
】
【该实验涉及到的知识点】①输入与输出格式设计;②数据类型及其异常处理;③循环及其嵌套;④算法设计;⑤类;⑥方法;⑦接口;⑧包。
=====================================================================================
第一次实验:
计算半径为3的圆的周长,运行效果见图1。
①
②
③
④
图1
第二次实验:
计算边长分别为3、4、5的三角形的面积和周长,运行效果见图2。
①
②
③
④
⑤
⑥
图2
第三次实验:
计算边长为7和8的矩形的面积,运行效果见图3。
①
②
③
④
⑤
图3
源程序如下:
category包:
Shape.java文件:
/**
*category为类的设计与构造包
*设计日期:
2010年9月27日
*/
packagecategory;
publicinterfaceShape{
/*
*getArea()为计算面积的抽象方法
*getCircumference()为计算周长的抽象方法
*/
publicabstractdoublegetArea();//获取面积
publicabstractdoublegetCircumference();//获取周长
}
Circle.java文件:
packagecategory;
/**
*圆类的设计
*radius为圆的半径
*/
publicclassCircleimplementsShape{
privatedoubleradius=0;
publicCircle(doubler){
radius=r;
}
publicdoublegetRadius(){
returnthis.radius;
}
publicdoublegetArea(){
returnMath.PI*radius*radius;
}
publicdoublegetCircumference(){
return2*Math.PI*radius;
}
}
Triangle.java文件:
packagecategory;
/**
*三角形类的设计
*a、b、c为三角形的三条边
*/
publicclassTriangleimplementsShape{
privatedoublea=0;
privatedoubleb=0;
privatedoublec=0;
publicTriangle(doublea,doubleb,doublec){
this.a=a;
this.b=b;
this.c=c;
}
publicdoublegetA(){
returnthis.a;
}
publicdoublegetB(){
returnthis.b;
}
publicdoublegetC(){
returnthis.c;
}
publicdoublegetArea(){
/*
*运用海伦公式计算三角形的面积
*/
doublep=(this.a+this.b+this.c)/2;
returnMath.sqrt(p*(p-this.a)*(p-this.b)*(p-this.c));
}
publicdoublegetCircumference(){
returnthis.a+this.b+this.c;
}
}
Rentangle.java文件:
packagecategory;
/**
*矩形类的设计
*length为矩形的长
*weight为矩形的宽
*/
publicclassRentangleimplementsShape{
privatedoublelength=0;
privatedoublewidth=0;
publicRentangle(doublelength,doublewidth){
this.length=length;
this.width=width;
}
publicdoublegetLength(){
returnthis.length;
}
publicdoublegetWidth(){
returnthis.width;
}
publicdoublegetArea(){
returnthis.length*this.width;
}
publicdoublegetCircumference(){
return2*(this.length+this.width);
}
}
graphic包:
Graphic_Calculation.java文件(main方法):
/**
*用接口设计并实现面积与周长计算
*程序编写员:
刘晨旭
*编制日期:
2010年9月26日
*最后一次修改日期:
2010年9月27日
*/
packagegraphic;//声明一个图形包
importcategory.*;//导入类的设计与构造包
importjavax.swing.JOptionPane;
/**
*主函数
*负责实现程序的整个功能
*/
publicclassGraphic_Calculation{
publicstaticvoidmain(String[]args){
doubleradius=0;//圆的半径
doublea=0,b=0,c=0;//三角形的三条边
doublelength=0,width=0;//矩形的长和宽
doublearea=0,circumference=0;
Stringstr_Select,str_Graphic;
/*
*data_input_one:
选择计算图形的哪种属性,面积还是周长
*data_input_two:
选择计算哪种图形,圆、三角形还是矩形
*/
intdata_input_one=0,data_input_two=0;//记录输入的数据,可用来判断输入是否合法
booleanflag=true;
booleanindication=true;
/**
*进行初次选择
*让用户选择进行图形的周长运算还是面积运算
*【1】计算面积
*【2】计算周长
*【3】计算面积与周长
*/
while(flag){
str_Select=JOptionPane.showInputDialog("您想计算图形的面积还是周长?
\n【1】面积\n【2】周长\n【3】面积与周长");
try{
data_input_one=Integer.parseInt(str_Select);
/*
*为了防止循环时出现indication的死循环状况
*例如:
假设首次输入操作非法,则indication会被赋值false,此时出现死循环
*故在此仍要重新声明indication的值为true
*/
indication=true;
if(data_input_one<=0||data_input_one>3){
JOptionPane.showMessageDialog(null,"您输入的数字不在选项内!
","警告",JOptionPane.INFORMATION_MESSAGE);
indication=false;
}
}catch(Exceptione){
JOptionPane.showMessageDialog(null,"非法数据","警告",JOptionPane.INFORMATION_MESSAGE);
indication=false;
}
if(indication)
flag=false;
}
/**
*选择要进行哪种图形的运算
*【1】圆
*【2】三角形
*【3】矩形
*/
flag=true;//对flag重新赋真值
while(flag){
str_Graphic=JOptionPane.showInputDialog("要计算的是什么图形?
\n【1】圆\n【2】三角形\n【3】矩形");
try{
data_input_two=Integer.parseInt(str_Graphic);
indication=true;
if(data_input_two<=0||data_input_two>3){
JOptionPane.showMessageDialog(null,"您输入的数字不在选项内!
","警告",JOptionPane.INFORMATION_MESSAGE);
indication=false;
}
}catch(Exceptione){
JOptionPane.showMessageDialog(null,"非法数据","警告",JOptionPane.INFORMATION_MESSAGE);
indication=false;
}
if(indication)
flag=false;
}
/**
*各种图形属性计算的实现
*先进行数据的输入,然后再根据要求输出
*/
/*
*输入语句的实现
*/
flag=true;
switch(data_input_two)
{
case1:
//圆的数据输入
{
while(flag){
try{
StringstrRadius=JOptionPane.showInputDialog("请输入圆的半径:
");
radius=Double.parseDouble(strRadius);
indication=true;
if(radius<=0){
JOptionPane.showInputDialog(null,"您输入的数小于等于零!
","警告",JOptionPane.INFORMATION_MESSAGE);
indication=false;
}
}catch(Exceptione){
JOptionPane.showMessageDialog(null,"非法数据","警告",JOptionPane.INFORMATION_MESSAGE);
indication=false;
}
if(indication)
flag=false;
}
break;
}
case2:
//三角形的数据输入
{
while(flag){
try{
StringstrA=JOptionPane.showInputDialog("请输入三角形的第一条边长:
");
StringstrB=JOptionPane.showInputDialog("请输入三角形的第二条边长:
");
StringstrC=JOptionPane.showInputDialog("请输入三角形的第三条边长:
");
a=Double.parseDouble(strA);
b=Double.parseDouble(strB);
c=Double.parseDouble(strC);
indication=true;
if(a<=0||b<=0||c<=0||a+b<=c||b+c<=a||c+a<=b)
{
JOptionPane.showMessageDialog(null,"您输入的三个数无法构成三角形!
","警告",JOptionPane.INFORMATION_MESSAGE);
indication=false;
}
}catch(Exceptione){
JOptionPane.showMessageDialog(null,"非法数据","警告",JOptionPane.INFORMATION_MESSAGE);
indication=false;
}
if(indication)
flag=false;
}
break;
}
case3:
//矩形的数据输入
{
while(flag){
try{
StringstrLength=JOptionPane.showInputDialog("请输入矩形的长:
");
StringstrWidth=JOptionPane.showInputDialog("请输入矩形的宽:
");
length=Double.parseDouble(strLength);
width=Double.parseDouble(strWidth);
indication=true;
}catch(Exceptione){
JOptionPane.showMessageDialog(null,"非法数据","警告",JOptionPane.INFORMATION_MESSAGE);
indication=false;
}
if(indication)
flag=false;
}
break;
}
}
/**
*具体属性的计算
*先根据data_input_one的取值判断需要计算的是哪个属性值(面积还是周长)
*然后根据data_input_two的取值判断需要计算的是哪种图形
*/
CircleC=newCircle(radius);
TriangleT=newTriangle(a,b,c);
RentangleR=newRentangle(length,width);
switch(data_input_one)
{
case1:
//计算图形的面积
{
switch(data_input_two)
{
case1:
area=C.getArea();break;
case2:
area=T.getArea();break;
case3:
area=R.getArea();break;
}
break;
}
case2:
//计算图形的周长
{
switch(data_input_two)
{
case1:
circumference=C.getCircumference();break;
case2:
circumference=T.getCircumference();break;
case3:
circumference=R.getCircumference();break;
}
break;
}
case3:
//计算图形的面积与周长
{
switch(data_input_two)
{
case1:
area=C.getArea();
circumference=C.getCircumference();
break;
case2:
area=T.getArea();
circumference=T.getCircumference();
break;
case3:
area=R.getArea();
circumference=R.getCircumference();
break;
}
break;
}
}
/**
*输出语句的实现
*先根据data_input_two的取值判断输出的是哪种图形
*然后根据data_input_one的取值判断需要输出哪些属性值(面积还是周长)
*/
switch(data_input_two)
{
case1:
{
switch(data_input_one)
{
case1:
JOptionPane.showMessageDialog(null,"圆的半径:
"+C.getRadius()+"\n面积:
"+area,"结果",JOptionPane.INFORMATION_MESSAGE);
break;
case2:
JOptionPane.showMessageDialog(null,"圆的半径:
"+C.getRadius()+"\n周长:
"+circumference,"结果",JOptionPane.INFORMATION_MESSAGE);
break;
case3:
JOptionPane.showMessageDialog(null,"圆的半径:
"+C.getRadius()+"\n面积:
"+area+"\n周长:
"+circumference,"结果",JOptionPane.INFORMATION_MESSAGE);
break;
}
break;
}
case2:
{
switch(data_input_one)
{
case1:
JOptionPane.showMessageDialog(null,"三角形的三条边长:
"+T.getA()+""+T.getB()+""+T.getC()+"\n面积:
"+area,"结果",JOptionPane.INFORMATION_MESSAGE);
break;
case2:
JOptionPane.showMessageDialog(null,"三角形的三条边长:
"+T.getA()+""+T.getB()+""+T.getC()+"\n周长:
"+circumference,"结果",JOptionPane.INFORMATION_MESSAGE);
break;
case3:
JOptionPane.showMessageDialog(null,"三角形的三条边长:
"+T.getA()+""+T.getB()+""+T.getC()+"\n面积:
"+area+"\n周长:
"+circumference,"结果",JOptionPane.INFORMATION_MESSAGE);
break;
}
break;
}
case3:
{
switch(data_input_one)
{
case1:
JOptionPane.showMessageDialog(null,"矩形的两条边长:
"+R.getLength()+""+R.getWidth()+"\n面积:
"+area,"结果",JOptionPane.INFORMATION_MESSAGE);
break;
case2:
JOptionPane.showMessageDialog(null,"矩形的两条边长:
"+R.getLength()+""+R.getWidth()+"\n周长:
"+circumference,"结果",JOptionPane.INFORMATION_MESSAGE);
break;
case3:
JOptionPane.showMessageDialog(null,"矩形的两条边长:
"+R.getLength()+""+R.getWidth()+"\n面积:
"+area+"\n周长:
"+circumference,"结果",JOptionPane.INFORMATION_MESSAGE);
break;
}
break;
}
}
}
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 接口 设计 实现 面积 周长 计算
![提示](https://static.bdocx.com/images/bang_tan.gif)