Bluej继续围绕我的双重...我怎么让它停下来?

时间:2013-12-13 22:08:11

标签: interface double computer-science bluej

我正在做一个项目,我有一个测试文件和接口,我被告知制作一个成功实现接口的“line”文件,并在所有测试中检查成功。 它在4个测试中的3个中实现并且成功,但在最后一个测试失败,因为它将斜率舍入为1.0 ...

以下是测试人员的代码:

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * The test class LineTest.
 *
 * @author  (your name)
 * @version (a version number or a date)
 */
public class LineTest
{
    /**
     * Default constructor for test class LineTest
     */
    public LineTest()
    {
    }

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

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

    @Test
    public void testConstructor()
    {
        Line line1 = new Line(10, 10, 25, 25);
        assertEquals(10, line1.getXOne());
        assertEquals(25, line1.getXTwo());
        assertEquals(10, line1.getYOne());
        assertEquals(25, line1.getYTwo());
    }

    @Test
    public void testGetSlope()
    {
        Line line1 = new Line(10, 10, -25, -25);
        assertEquals(0.0, line1.getSlope(), 0.1);
        line1.print();
    }

    @Test
    public void testCalcSlope()
    {
        Line line1 = new Line(10, 10, -25, -25);
        line1.calculateSlope();
        assertEquals(1.0, line1.getSlope(), 0.1);
        line1.print();
    }

    @Test
    public void testSetCoords()
    {
        Line line1 = new Line(10, 10, -25, -35);
        line1.calculateSlope();
        assertEquals(1.285, line1.getSlope(), 0.003);
        line1.print();
        line1.setCoordinates(10, 10, 25, 35);
        line1.calculateSlope();
        assertEquals(1.667, line1.getSlope(), 0.001);
        line1.print();
    }
}

这是行类:

public class Line
{
    private int xOne,yOne, xTwo, yTwo;
    private double slope;

    public Line(int x1, int y1, int x2, int y2)
    {
        xOne=x1;
        yOne=y1;
        xTwo=x2;
        yTwo=y2;
    }

    public void setCoordinates(int x1, int y1, int x2, int y2)
    {
        x1=xOne;
        y1=yOne;
        x2=xTwo;
        y2=yTwo;

    }

    public void calculateSlope( )
    {
        slope = (((yTwo)-(yOne))/((xTwo)-(xOne)));

    }
    public void print( )
    {
        System.out.println("The slope of the line created by the points ("+xOne+","+yOne+"), and ("+xTwo+","+yTwo+") is "+slope+".");

    }
    public int getXOne(){
        return xOne;
    }
    public int getXTwo(){
        return xTwo;
    }
    public int getYOne(){
        return yOne;
    }
    public int getYTwo(){
        return yTwo;
    }
    public double getSlope(){
        return slope;
    }
}

这是界面:

public interface TestableLine
{
    public void setCoordinates(int x1, int y1, int x2, int y2);

    public void calculateSlope( );

    public void print( );

    public int getXOne();

    public int getYOne();

    public int getXTwo();

    public int getYTwo();

    public double getSlope();
}

这里有什么问题?我已经尝试指定要舍入的小数位数,它只会使测试3失败。

1 个答案:

答案 0 :(得分:1)

您只使用int值计算斜率。在计算斜率的方法中,您可以从int值中创建双变量,并使用它们进行计算。