2013-04-24 150 views
0
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. "); 
     } 
    } 
} 

所以我应该写一个圆班。这就是我所做的。写作班级的方法测试类

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; 
    } 


    } 

我就怎么写了class.Like我知道我需要初始化私有变量实际上混淆等二十多个国家,我需要建立一个构造函数,但不知何故这上面的代码不work.the测试方法是正确的,但我必须用它来实现我的课程。

+1

你对如何写一堂课感到困惑......但你写了一堂课。好的,你有什么特别的困惑吗? – Makoto 2013-04-24 02:57:10

+0

如果您想编写测试用例,请查看像[junit](http://junit.org/)或[testng](http://testng.org/doc/index.html) – 2013-04-24 03:00:13

+0

这样的单元测试框架。很好..遇到什么错误? – Zain 2013-04-24 03:00:39

回答

0

你声明变量

Circle first 

两次。如果要重新分配其价值,只是做

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

而且里面的if语句你打电话

first.hasLargerAreaThan(20) 

时,我没有看到在你的类中定义这样的方法。

0

你可以请你说的代码不工作?如果您所指的区域未被正确计算并且始终为0,则会发生这种情况,因为您的默认值为0,并且从不计算它。您可能想要将计算逻辑放在getArea()方法中。

0

首先,如果这是必需的,您将需要使用测试框架来声明代码的有效性。看看JUnit

如果面积大于某个值,则样本断言将如此写入。

@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); 
} 

其次,你的getArea实际上没有得到区。代码中没有任何内容可以检索,然后计算该区域。你甚至没有使用Math.PI。我建议你实现这一点 - 但使用单元测试作为一种有效的方式来断言你将得到适当的回应。

相关问题