質問

I am doing a project, I have a test file and interface and I am told to make a "line" file that successfully implements the interface and checks out successful on all the tests. It implements and is successful on 3 of the 4 tests but fails on the last because it rounds the slope to 1.0...

Here is the code for the tester:

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

Here is the line class:

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

Here is the interface:

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

What is wrong here? I have tried specifying the number of decimals to round to, it just makes test 3 fail as well.

役に立ちましたか?

解決

You are calculating the slope only with int values. In the method which calculates the slope you can create double variables out of the int values and use those to make the calculation.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top