Bluej数学测试仪类错误

时间:2013-12-01 21:31:21

标签: java interface bluej

我再次有一个计算机科学问题。我正在为AP计算机科学做一个实验室,我们给它提供了2个骨架代码,一个测试器类和一个接口类。我们的工作是使测试仪运行没有错误。我得到的代码是三角形数学代码(边长,边长,面积,那种东西)。我做到了所以它会全部运行,但其中一个测试仍然失败。我不知道为什么,请帮助我。 这是三角形主要代码:

import java.util.Scanner;
import java.lang.Math.*;
import java.lang.*;

public class Triangle implements TestableTriangle
{
    private int sideA, sideB, sideC;
    private double perimeter;
    private double theArea;

    public Triangle()
    {
       setSides(0,0,0);
       perimeter=0;
       theArea=0;
    }

    public Triangle(int a, int b, int c)
    {
        sideA=a;
        sideB=b;
        sideC=c;
    }

    public void setSides(int a, int b, int c)
    {
        sideA=a;
        sideB=b;
        sideC=c;
    }

    public void calcPerimeter( )
    {
        perimeter=(sideA+sideB+sideC);


    }

    public void calcArea( )
    {
        double s;
        s=(perimeter/2);
        theArea=(s*(s-a)*(s-b)*(s-c));


    }

    public void print( )
    {
        System.out.println("\n\n");
        System.out.println(sideA+" "+sideB+" "+sideC+"\n");
    }

    public int getSideA()
    {
        return sideA;
    }

    public int getSideB()
    {
        return sideB;     
    }

    public int getSideC()
    {
        return sideC;
    }
    public double getPerimeter()
    {
        return perimeter;
    }
    public double getTheArea()
    {
        return theArea;
    }

}

这是实验室代码。该类用于测试Triangle

import java.util.Scanner;
import java.lang.Math.*;
public class Lab03a  //this class is used to test Triangle
{
    public static void main( String[] args )
    {
        Scanner keyboard = new Scanner(System.in);

        //ask for user input
        System.out.print("Enter side A ::  ");
        int a = keyboard.nextInt();

        System.out.print("Enter side B ::  ");
        int b = keyboard.nextInt();

        System.out.print("Enter side C ::  ");
        int c = keyboard.nextInt();


        Triangle test = new Triangle(a, b, c);
        test.calcPerimeter();
        test.calcArea();
        test.print();


        //ask for user input
        System.out.print("Enter side A ::  ");
        a = keyboard.nextInt();

        System.out.print("Enter side B ::  ");
        b = keyboard.nextInt();

        System.out.print("Enter side C ::  ");
        c = keyboard.nextInt();


        test.setSides(a,b,c);
        test.calcPerimeter();
        test.calcArea();
        test.print();


        //add one more input section
        System.out.print("Enter side A ::  ");
        a = keyboard.nextInt();

        System.out.print("Enter side B ::  ");
        b = keyboard.nextInt();

        System.out.print("Enter side C ::  ");
        c = keyboard.nextInt();


        test.setSides(a,b,c);
        test.calcPerimeter();
        test.calcArea();
        test.print();
    }
}

以下是界面代码:

public interface TestableTriangle
{
    public void setSides(int a, int b, int c);

    public void calcPerimeter( );

    public void calcArea( );

    public void print( );

    public int getSideA();

    public int getSideB();

    public int getSideC();

    public double getPerimeter();

    public double getTheArea();
}

这是测试人员代码(下面是它显示的错误消息):     import static org.junit.Assert。*;     import org.junit.After;     import org.junit.Before;     import org.junit.Test;

public class TriangleTest
{
    private Triangle triangle1;

    /**
     * Default constructor for test class TriangleTest
     */
    public TriangleTest()
    {
    }

    /**
     * Sets up the test fixture.
     *
     * Called before every test case method.
     */
    @Before
    public void setUp()
    {

        triangle1 = new Triangle(37, 38, 39);
    }

    /**
     * Tears down the test fixture.
     *
     * Called after every test case method.
     */
    @After
    public void tearDown()
    {
    }

    @Test
    public void testConstructorAndSides()
    {
        //Triangle triangle1 = new Triangle(35, 36, 37);
        assertEquals(37, triangle1.getSideA());
        assertEquals(38, triangle1.getSideB());
        assertEquals(39, triangle1.getSideC());
    }

    @Test
    public void testConstructorAndPerimeter()
    {
        //Triangle triangle1 = new Triangle(36, 37, 38);
        assertEquals(0.0, triangle1.getPerimeter(), 0.1);
        triangle1.calcPerimeter();
        assertEquals(114.0, triangle1.getPerimeter(), 0.1);
    }

    @Test
    public void testSetSidesAndSides()
    {
        //Triangle triangle1 = new Triangle(37, 38, 39);
        triangle1.setSides(10, 15, 16);
        assertEquals(10, triangle1.getSideA());
        assertEquals(15, triangle1.getSideB());
        assertEquals(16, triangle1.getSideC());
    }

    @Test
    public void testSetSidesAndPerimeter()
    {
        Triangle triangle2 = new Triangle();
        triangle2.calcPerimeter();
        assertEquals(0.0, triangle2.getPerimeter(), 0.1);
        triangle2.setSides(38, 39, 40);
        assertEquals(0.0, triangle2.getPerimeter(), 0.1);
        triangle2.calcPerimeter();
        assertEquals(117.0, triangle2.getPerimeter(), 0.1);
    }

    @Test
    public void testSetSidesAndTheArea()
    {
        Triangle triangle2 = new Triangle();
        assertEquals(0.0, triangle2.getTheArea(), 0.1);
        triangle2.setSides(12, 13, 14);
        triangle2.calcArea();
        assertEquals(-0.0, triangle1.getTheArea(), 0.1);
        triangle2.calcPerimeter();
        triangle2.calcArea();
        assertEquals(72.30794, triangle2.getTheArea(), 0.00001);
    }


}

错误信息表示此行有问题:

assertEquals(72.30794, triangle2.getTheArea(), 0.00001);

由于某种原因导致它失败。我不知道为什么,其他一切都很完美。任何帮助表示赞赏。

由于 -Keelen

3 个答案:

答案 0 :(得分:1)

你忘了采取你为该地区得到的表达式的平方根。而且您似乎没有使用正确的变量名称:

public void calcArea( )
{
    // ensure perimeter is up-to-date!
    calcPerimeter();
    double s = (perimeter / 2.0);
    theArea = Math.sqrt(s * (s - sideA) * (s - sideB) * (s - sideC));
}

答案 1 :(得分:0)

在代码中

theArea=(s*(s-a)*(s-b)*(s-c));

应该是

theArea=Math.sqrt((s*(s-a)*(s-b)*(s-c)));

答案 2 :(得分:0)

这是因为在计算三角形的面积时会出现错误代码。 它应该如下:

public void calcArea( )
{
    double s;
    s=(perimeter/2);
    theArea=Math.sqrt((s*(s-a)*(s-b)*(s-c)));


}

希望这有帮助。