gpt4 book ai didi

java - Bluej 数学测试器类错误

转载 作者:行者123 更新时间:2023-12-01 13:46:40 26 4
gpt4 key购买 nike

我又有一个计算机科学问题。我正在做一个 AP 计算机科学实验室,其中我们有 2 个框架代码、一个测试器类和一个接口(interface)类。我们的工作是让测试仪运行时没有错误。我得到的代码是一个三角形数学代码(边长、周长、面积等)。我做到了,这样它就能全部运行,但其中一个测试一直失败。我不知道为什么,请帮助我。这是三角形主要代码:

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

接口(interface)代码如下:

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

这是测试器代码(下面是它显示的错误消息): 导入静态 org.junit.Assert.*; 导入 org.junit.After; 导入 org.junit.Before; 导入 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);

由于某种原因导致它失败。我不知道为什么,其他一切都很完美。如有任何帮助,我们将不胜感激。

谢谢-基伦

最佳答案

您忘记计算面积表达式的平方根。看来您没有使用正确的变量名称:

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

关于java - Bluej 数学测试器类错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20317497/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com