【问题标题】:Creating a Test Driver for the Box class and testing all its methods为 Box 类创建测试驱动程序并测试其所有方法
【发布时间】:2019-10-31 09:34:45
【问题描述】:
public static void main(String[] args) {

        double w;
        double h;
        double d;

        Scanner input = new Scanner(System.in);

        System.out.print ("Enter the width of your box:");
        w= input.nextDouble();
        System.out.print ("Enter the height of your box:");
        h= input.nextDouble();
        System.out.print ("Enter the depth of your box:");
        d= input.nextDouble();

        Box boxOne = new Box(w, h, d);
        System.out.println(boxOne.toString());

        boxOne.setDim(w, h, d);


    }

}

class Box
{
    private double width = 1.0;
    private double height = 1.0;
    private double depth = 1.0;

     public Box (double w, double h, double d)
     {
         setWidth(w);
         setHeight(h);
         setDepth(d);
     }

     public void setWidth(double w)
        {
            if(w > 0)
            {
                width = w;
            }
            else
            {
                width = 1.0;
            }
        }

     public void setHeight(double h)
        {
            if(h > 0)
            {
                height = h;
            }
            else
            {
                height = 1.0;
            }
        }

        public void setDepth(double d)
        {
            if(d > 0)
            {
                depth = d;
            }
            else
            {
                depth = 1.0;
            }
        }

        public void setDim(double width, double height, double depth)
        {
            double volume=width*height*depth;
            System.out.println("The volume of the box is "+volume);
        }

        public double volume ()
        {

        }

        public String getWidth()
        {
            return String.format("%f",width);
        }
        public String getHeight()
        {
            return String.format("%f",height);
        } 
        public String getDepth()
        {
            return String.format("%f",depth);
        } 
        public String toString()
        {
            return String.format("Width is %s.\nHeight is %s.\nDepth is %s.", getWidth(), getHeight(),getDepth());
        }

        public boolean equalTo (Box o) 
        {
               }
}

我不明白如何在这段代码中使用 public boolean equalTo (Box o)public double volume() 方法。我应该在这两种方法的主体中写什么?以及如何在主要方法中使用它们?我不理解这两种方法。如何创建boolean equalTo() 方法和double volume() 方法,以及如何测试它们?

【问题讨论】:

  • JavaJavaScript 是两种完全不同的编程语言。因此,当您对Java 有疑问时,不应标记JavaScript
  • 调用equalTo > equals,以后Java可以自动使用,这是继承自Object类的方法

标签: java oop methods


【解决方案1】:

equalTo 方法需要比较两个对象的属性:

public boolean equalTo (Box o){
    boolean widthEquals = o.width == this.width;

    // other checks

    return widthEquals && heightEquals && depthEquals;
}

对我来说,音量方法看起来很简单:

public double volume() {
    return this.width * this.height * this.depth;
}

测试这些方法需要您设置一个测试框架,例如 JUnit,这在很多教程中都有介绍。例如:

https://www.tutorialspoint.com/junit/junit_test_framework.htm

https://junit.org/junit4/faq.html#atests_1

测试volume方法看起来像这样:

@Test
public void testVolume() {
    Box box = new Box(1.0, 2.0, 3.0);

    double expectedVolume = 1.0 * 2.0 * 3.0;

    assertEquals(expectedVolume, box.volume());
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    • 1970-01-01
    • 2017-07-23
    • 2019-08-22
    • 1970-01-01
    相关资源
    最近更新 更多