作业题目:维护开发出来的程序,找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),这样用户就不至于输入出错。
这个程序是用来求图形的体积的,所以,可以扩展程序的功能,例如,求柱体,椎体,台体等等的体积,可以用在数学,建筑等方面,实现快速计算,
改进的代码中写了一部分扩展程序,供参考。