Interface 布鲁杰一直在我的双倍。。。我怎样才能让它停下来?
我正在做一个项目,我有一个测试文件和接口,我被告知制作一个成功实现接口的“行”文件,并检查所有测试是否成功。 它实现并在4个测试中的3个测试中成功,但在最后一个测试中失败,因为它将斜率舍入到1.0 以下是检测仪的代码:Interface 布鲁杰一直在我的双倍。。。我怎样才能让它停下来?,interface,double,computer-science,bluej,Interface,Double,Computer Science,Bluej,我正在做一个项目,我有一个测试文件和接口,我被告知制作一个成功实现接口的“行”文件,并检查所有测试是否成功。 它实现并在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.
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();
}
}
以下是line类:
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也失败。您只使用int值计算斜率。在计算坡度的方法中,可以从int值中创建双变量,并使用这些变量进行计算 答案有帮助吗?是的,正如我在下面的评论中所说的,但是我的setcoords方法在实际的line类中有问题。它显然没有读取它,因此它仍然将坐标视为10,10,-25,-35,而不是
line1.setCoordinates(10,10,25,35)之后应该存在的坐标代码>(很抱歉,stackoverflow说我因为某些原因无法编辑我上面的帖子)那么你的意思是将其更改为slope=((((双)yTwo)-(双)yOne))/((双)xTwo)-(双)xOne))
?我试过了,它说它们都已经在setCoordinates方法中指定了。。。我知道它应该可以工作,BlueJ只是拒绝接受它,我上网去了,它编译得很顺利,但是当我在BlueJ中运行完全相同的代码时,它突然在我身上爆发出来。只是重新启动我的计算机,然后再试一次。它现在可以编译,但仍然无法通过测试。如您所见,testSetCoords()中有两个单独的测试。第一个是通过,第二个不是。上面写着“期望1.6667,得到1.285”,你的setCoordinates方法是错误的。方法的主体应该与构造函数的主体完全相同。也就是说,xOne=x1而不是x1=xOne,其他变量也是如此。。。现在它工作得很好,谢谢你救了我的屁股:P我在班上得了79.4分,这是这个评分期的最后一个实验室。。。现在我没有拿到c:D