Создание тестового драйвера для класса Box и тестирование всех его методов - PullRequest
1 голос
/ 31 октября 2019
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() и как их проверить?

1 Ответ

1 голос
/ 31 октября 2019

Метод 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

Тестирование метода объема будет выглядеть примерно так:

@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());
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...