1.课程设计任务:
完成一个应用项目的开发,要求实现 (1)计算几何图形矩形、平行四边形和园的面积和周长, (2)计算圆柱体和圆锥体的体积。 基本要求如下: ①使用面向对象的开发思想,体现封装性、继承性和多态性 ②合理使用多类设计,并在不同的包中实现,形成可复用模块; 提示:项目需要1-2组成团队共同完成,组员创建2个包,分别为pack1,pack2,其中pack1包中完成功能(a)中类及方法的建立及测试,pack2包中完成功能(b)中类及方法的建立及测试,最后组长建立一个项目kcsj_name,并创建一个包为compute,完成(a)和(b)中类方法的调用及集成功能测试。(这里的项目名和包名为举例) ③程序正确并满足健壮性 ④程序代码命名合理,注释清晰,可读性好;程序名、变量名、方法名符合JAVA命名规范 ⑤控制台人机交流流畅,互动性合理,具有一定的通用性
2.设计项目的功能说明
完成一个应用项目的开发,用来计算几何图形矩形、平行四边形和园的面积和周长,计算圆柱体和圆锥体的体积
3.课程设计代码
3.1pack1(平面图形包)
3.1.1抽象类
package pack1;
public abstract class Shape{
static final float PI=3.14F;
public abstract float Area();
public abstract float Perimeter();
}
3.1.2圆类
package pack1;
public class Clicle extends Shape
{
private float radius;
public Clicle(float r)
{
radius =r;
}
@Override
public float Perimeter()
{
return 2*PI*radius;
}
@Override
public float Area()
{
return PI*radius*radius;
}
}
3.1.3平行四边形类
package pack1;
public class Pingxing extends Shape
{
private float base;
private float high;
private float side_length;
public Pingxing(float base,float high,float side_length)
{
this.base=base;
this.high=high;
this.side_length=side_length;
}
@Override
public float Perimeter()
{
return (base+side_length)*2;
}
@Override
public float Area()
{
return base*high;
}
}
3.1.4矩形类
package pack1;
public class Rantangle extends Shape{
private float length;
private float width;
public Rantangle(float length,float width)
{
this.length=length;
this.width=width;
}
@Override
public float Area()
{
return length*width;
}
@Override
public float Perimeter()
{
return (length+width)*2;
}
}
3.2pack2 (立体图形包)
3.2.1圆柱体类(作为父类)
package pack2;
public class yuanzhu
{
private float high;
private float radius;
private float PI=3.14F;
public yuanzhu()
{
}
public yuanzhu(float high,float radius)
{
this.high=high;
this.radius=radius;
}
public void setHigh(float high)
{
this.high=high;
}
public void setradius(float radius)
{
this.radius=radius;
}
public float solid()
{
return PI*radius*radius*high;
}
}
3.2.2圆锥体类
package pack2;
public class yuanzhui extends yuanzhu
{
private float One_third=0.33F;
public float solid()
{
return super.solid()*One_third;
}
}
3.3集成测试
package project;
import java.util.Scanner;
import pack1.Pingxing;
import pack1.Rantangle;
import pack1.Clicle;
import pack1.Shape;
import pack2.yuanzhu;
import pack2.yuanzhui;
public class design {
public static void main(String[] args) {
int type;
Scanner scan=new Scanner(System.in);
try
{
do
{
System.out.println("请输入需要计算的几何图形:0、退出系统,1、圆形,2、矩形,3、平行四边形,4、圆锥体,5、圆柱体");
type=scan.nextInt();
if(type==1)
{
Shape cli=new Clicle(2);
System.out.println("圆形的面积"+cli.Area()+"圆形的周长"+cli.Perimeter());
}
if(type==2)
{
System.out.println("请输入矩形的长");
float length =scan.nextFloat();
System.out.println("请输入矩形的宽");
float high =scan.nextFloat();
Shape rant=new Rantangle(length,high);
System.out.println("长方形的面积"+rant.Area()+"长方形的周长"+rant.Perimeter());
}
if(type==3)
{
System.out.println("请输入平行四边形的底");
float base =scan.nextFloat();
System.out.println("请输入平行四边形的宽");
float high =scan.nextFloat();
System.out.println("请输入平行四边形的边长");
float length =scan.nextFloat();
Shape pingx=new Pingxing(base,high,length);
System.out.println("长方形的面积"+pingx.Area()+"长方形的周长"+pingx.Perimeter());
}
if(type==4)
{
yuanzhu con=new yuanzhu();
con.setHigh(8);
con.setradius(3);
System.out.println("圆锥体的体积是:"+con.solid());
}
if(type==5)
{
yuanzhu cyl=new yuanzhu(5,2);
System.out.println("圆柱体的体积是:"+cyl.solid());
}
}while(type!=0);
System.out.println("程序结束,谢谢使用");
scan.close();
System.exit(0);
}catch(Exception e)
{
System.out.println("请输入要求的自然数:整数或小数");
System.out.println("程序结束,谢谢使用");
}
}
}
4.测试结果
|