简体   繁体   English

编写测试课程的课堂方法

[英]Writing class method for test class

public class CirclTest{
    public static void main(String[] args){
        Circle first=new Circle('R',3.0);

        Circle first=new Circle('R',3.0);

        Circle second=new Circle();

        System.out.println("first's radius is " + first.getRadius());

        System.out.println("first's area is " + first.getArea());
        System.out.println("second's area is " + second.getArea());

        if(first.hasLargerAreaThan(20)){
            System.out.println("first's area is larger than 20. ");
        }else{
            System.out.println("first's area is smaller than 20. ");
        }
    }
}

So i am supposed to write a circle class.This is what i have done. 所以我应该写一个圆类。这就是我所做的。

public class Circle{
    private double radius=0.0;
    private double area=0.0;
    private char colour=' ';

    public Circle(char colour,double radius){
        this.colour=colour;
        this.radius=radius;
    }

    public Circle(){
        radius=0;
        colour='B';
    }


    public char getColour(){
        return colour;
    }
    public double getRadius(){
        return radius;
    }

    public double getArea(){
        return area;
    }


    }

I am actually confused on how to write a class.Like i know i need to initialize the variables by private etc.And i need to build a constructor but somehow this code above does not work.the test method is correct.But i have to use it to implement my class. 我实际上对如何写一个类感到困惑。就像我知道我需要通过private等初始化变量,并且我需要构建一个构造函数,但是以某种方式上面的这段代码不起作用。测试方法是正确的。但是我必须用它来实现我的课程。

You're declaring the variable 您在声明变量

Circle first

twice. 两次。 If you want to reassign its value, just do 如果您想重新分配其价值,请执行

first=new Circle('R',3.0);

And inside the if statement you're calling 在您要调用的if语句中

first.hasLargerAreaThan(20)

when I don't see such a method defined in your class. 当我看不到您的课程中定义了这种方法时。

Can you please what you mean by the code does not work? 您能不能满意代码的含义? If you are referring to area not getting calculated correct and is always 0, that is happening because you have a default value for the same as 0 and are never calculating it. 如果您指的是面积计算不正确且始终为0,则发生这种情况是因为您具有与0相同的默认值,并且从不对其进行计算。 You might want to put calculation logic in getArea() method. 您可能需要将计算逻辑放入getArea()方法中。

First, you're going to want to use a testing framework to assert the validity of your code, if that's what is required. 首先,如果需要的话,您将要使用测试框架来断言代码的有效性。 Look into JUnit . 查看JUnit

A sample assertion of if the area was larger than some value would be written like this. 这样会写一个有关面积是否大于某个值的样本断言。

@Test
public void assertArea_calculatedProperly() {
    //given that the radius is 5,
    Circle c = new Circle('R', 5);

    //when I get the area...
    double result = c.getArea();

    //then I expect it to be around 78.53981634.
    assertTrue(result < 78.6);
    assertTrue(result > 78.5);
}

Second, your getArea isn't actually getting the area. 其次,您的getArea实际上并没有获得该区域。 There's nothing in your code to retrieve, then calculate the area. 您的代码中没有要检索的内容,然后计算面积。 You're not even using Math.PI . 您甚至都没有使用Math.PI I would recommend that you implement that - but use the unit test as a valid way to assert that you're going to get an appropriate response back. 我建议您实施该方法-但使用单元测试作为断言您将获得适当响应的有效方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM