zoukankan      html  css  js  c++  java
  • 软件工程综合实践专题-第一次作业

    作业题目:维护开发出来的程序,找bug,改bug,满足用户需求,开发测试用例,

    在原基础上做增量开发,设计,回归测试。

    程序来源:同学以前开发的一段程序

    /*求图形体积*/

    需求:求各种图形的体积

    原来程序代码

    import java.util.Scanner;
    abstract class Geometry
    {
        public abstract double getArea();
    }
    class Circle extends Geometry
    {
        double r;
        Circle(double r)
        {
            this.r=r;
        }
        public double getArea()
        {
            return 3.14*r*r;
        }
    }
    class Rectangle extends Geometry
    {
        double a,b;
        Rectangle(double a,double b)
        {
            this.a=a;
            this.b=b;
        }
        public double getArea()
        {
            return a*b;
        }
    }
    class Pillar
    {
        Geometry bottom;
        double height;
        Pillar(Geometry bottom,double height)
        {
            this.bottom=bottom;
            this.height=height;
        }
        public double getVolume()
        {
            return bottom.getArea()*height;
        }
    }
    public class Application
    {
        public static void main(String args[])
        {
            Pillar pillar;
            Geometry bottom;
            
            System.out.println("please enter the radius and height of the cylinder");
            Scanner scanner=new Scanner(System.in);
            bottom=new Circle(scanner.nextDouble());
            pillar=new Pillar(bottom,scanner.nextDouble());
            System.out.println("the volume of a cylinder is "+pillar.getVolume());//圆柱体体积
            
            System.out.println("please enter the length,width and height of the cuboid");
            bottom=new Rectangle(scanner.nextDouble(),scanner.nextDouble());
            pillar=new Pillar(bottom,scanner.nextDouble());
            System.out.println("the volume of a cuboid is "+pillar.getVolume());//长方体体积
        }

    }

    /*测试部分用例
    please enter the radius and height of the cylinder
    2
    2
    the volume of a cylinder is 25.12
    please enter the length,width and height of the cuboid
    2
    3
    4
    the volume of a cuboid is 24.0
    */

    /*测试部分用例(有bug)
    please enter the radius and height of the cylinder
    -2
    2
    the volume of a cylinder is 25.12
    please enter the length,width and height of the cuboid
    -2
    3
    4
    the volume of a cuboid is -24.0
    */

    修改后程序代码(设计开发)

    import java.util.Scanner;
    abstract class Geometry
    {
        public abstract double getArea();
    }
    class Circle extends Geometry
    {
        double r;
        Circle(double r)
        {
            this.r=r;
        }
        public double getArea()
        {
            return 3.14*r*r;
        }
    }
    class Rectangle extends Geometry
    {
        double a,b;
        Rectangle(double a,double b)
        {
            this.a=a;
            this.b=b;
        }
        public double getArea()
        {
            return a*b;
        }
    }
    class Cone_circle extends Geometry//增加求圆锥体体积
    {
        double r;
        Cone_circle(double r)
        {
            this.r=r;
        }
        public double getArea()
        {
            return 1.0/3*3.14*r*r;
        }
    }
    class Pillar
    {
        Geometry bottom;
        double height;
        Pillar(Geometry bottom,double height)
        {
            this.bottom=bottom;
            this.height=height;
        }
        public double getVolume()
        {
            return bottom.getArea()*height;
        }
    }
    public class Application
    {
        public static void main(String args[])
        {
            Pillar pillar;
            Geometry bottom;
            double r,h,a,b;
            
            System.out.println("please enter the radius and height of the cylinder");
            Scanner scanner=new Scanner(System.in);
            r=scanner.nextDouble();
            h=scanner.nextDouble();
            assert (r>0 && h>0):"can't calculate the volume if it's not positive";//使用断言,可以避免非正数的计算
            bottom=new Circle(r);
            pillar=new Pillar(bottom,h);
            System.out.println("the volume of a cylinder is "+pillar.getVolume());
            
            System.out.println("please enter the length,width and height of the cuboid");
            a=scanner.nextDouble();
            b=scanner.nextDouble();
            assert (a>0 && b>0):"can't calculate the volume if it's not positive";//使用断言,可以避免非正数的计算
            bottom=new Rectangle(a,b);
            pillar=new Pillar(bottom,scanner.nextDouble());
            System.out.println("the volume of a cuboid is "+pillar.getVolume());
            
            System.out.println("please enter the radius and height of the cone");
            r=scanner.nextDouble();
            h=scanner.nextDouble();
            assert (r>0 && h>0):"can't calculate the volume if it's not positive";//使用断言,可以避免非正数的计算
            bottom=new Cone_circle(r);
            pillar=new Pillar(bottom,h);
            System.out.println("the volume of a cone is "+pillar.getVolume());
            
        }

    }

    /*部分回归测试用例
    please enter the radius and height of the cylinder
    2
    2
    the volume of a cylinder is 25.12
    please enter the length,width and height of the cuboid
    2
    3
    4
    the volume of a cuboid is 24.0
    please enter the radius and height of the cone
    3
    3
    the volume of a cone is 28.259999999999994
    */

    /*部分回归测试用例(解决bug)
    please enter the radius and height of the cylinder
    -2
    2
    Exception in thread "main" java.lang.AssertionError: can't calculate the volume if it's not positive
        at titi.Application.main(Application.java:70)
    */

    说明:1.测试用例有好多在此只放置一部分;

               2.增量开发,扩展功能有好多(求各种柱体,椎体,台体体积),在此只放置一部分

    心得体会:在运行原来的程序时,进行一些正数测试时总是正确的,但是输入一些负数时程序依然可以运行,但是负数,显然不满足(体积不能是负数),

    因此,为了解决这个问题,使用了断言,可以用户输入负数时提示错误(can't calculate the volume if it's not positive),这样用户就不至于输入出错。

    这个程序是用来求图形的体积的,所以,可以扩展程序的功能,例如,求柱体,椎体,台体等等的体积,可以用在数学,建筑等方面,实现快速计算,

    改进的代码中写了一部分扩展程序,供参考。

  • 相关阅读:
    阿里云CentOS主机修改默认SSH登录的22端口
    python跨文件设置全局变量
    python类装饰器
    执行python manage.py celery beat-l info 时报错 SystemError:<class 'OSError'>可能还会有其他报错
    利用Python脚本实现发送邮件
    python操作pymysql
    python-两个数组元素一样,位置个数不相同,按照一个标准的列表实现另一个列表的排序
    XORM高级操作
    flutter踩坑指南 配置篇
    create-react-app项目暴露webpack配置文件
  • 原文地址:https://www.cnblogs.com/zyhl/p/10460779.html
Copyright © 2011-2022 走看看