简体   繁体   English

谁能帮我解决这个Java错误?

[英]Can anyone help me with this java error ???

So I have this lab due where I have to make a basic class with some constructors for getting the points in a space... I seem to have struggled to write most of my code , but my test case is giving me an error that states the following.... "java.lang.NoSuchMethodException: Point.getRadius()" 因此,我要进行这个实验,因为我必须使用一些构造函数来创建基本类以获取空间中的点……我似乎很难编写大部分代码,但是我的测试用例却给我一个错误,指出以下内容。...“ java.lang.NoSuchMethodException:Point.getRadius()”

Can anyone help me understand where i went wrong in my code ? 谁能帮助我了解我的代码在哪里出错?

MY Code 我的密码

 public class Point {

//X and Y cordinates
    private double x;
    private double y;

    //Constructor
    public Point(double x, double y) {
            super();
            this.x = x;
            this.y = y;
    }
    //getters
    public double getX() {
            return x;
    }
    public double getY() {
            return y;
    }

    //Method to find radius of point from the Origin point, which is 
    //passed as argument
    public double getRadius(Point origin){
            double a = this.getX() - origin.getX();
            double b = this.getY() - origin.getY();
            return Math.sqrt(a*a + b*b);
    }

    //Method to find angle of point with respect to x-axis
    public double getAngle(){
            //atan function of Math class is used to find the angle
            //in the range -pi/2 through pi/2, multiply by 180 and 
            //divide by PI to convert into radian
            return Math.atan(this.y/this.x) * 180 / Math.PI;
    }

    //Method to find a point which is rotated 90 of current point from origin
    public Point rotate90(Point origin){
            //these formulas are used to calculate X and Y coordinates of new point
            double newX = origin.getX() + (this.getX()-origin.getX())*Math.cos(90) - (this.getY()-origin.getY())*Math.sin(90);

            double newY = origin.getY() + (this.getX()-origin.getX())*Math.sin(90) + (this.getY()-origin.getY())*Math.cos(90);

            return new Point(newX, newY);

    }

}

Now Here are my test cases 现在这是我的测试用例

 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
 import java.util.function.Predicate;
 import java.util.stream.Collectors;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
 import java.lang.reflect.Field;

    import static org.junit.Assert.assertArrayEquals;
    import static org.junit.Assert.assertEquals;
    import static org.junit.Assert.assertTrue;
    import static org.junit.Assert.fail;
    import org.junit.Test;

public class TestCases
{
   public static final double DELTA = 0.00001;

   /*
    * This test is just to get you started.
    */
   @Test
   public void testGetX()
   {
      assertEquals(1.0, new Point(1.0, 2.0).getX(), DELTA);
   }

   /*
    * The tests below here are to verify the basic requirements regarding
    * the "design" of your class.  These are to remain unchanged.
    */

   @Test
   public void testImplSpecifics()
      throws NoSuchMethodException
   {
      final List<String> expectedMethodNames = Arrays.asList(
         "getX",
         "getY",
         "getRadius",
         "getAngle",
         "rotate90"
         );

      final List<Class> expectedMethodReturns = Arrays.asList(
         double.class,
         double.class,
         double.class,
         double.class,
         Point.class
         );

      final List<Class[]> expectedMethodParameters = Arrays.asList(
         new Class[0],
         new Class[0],
         new Class[0],
         new Class[0],
         new Class[0]
         );

      verifyImplSpecifics(Point.class, expectedMethodNames,
         expectedMethodReturns, expectedMethodParameters);
   }

   private static void verifyImplSpecifics(
      final Class<?> clazz,
      final List<String> expectedMethodNames,
      final List<Class> expectedMethodReturns,
      final List<Class[]> expectedMethodParameters)
      throws NoSuchMethodException
   {
      assertEquals("Unexpected number of public fields",
         0, Point.class.getFields().length);

      final List<Method> publicMethods = Arrays.stream(
         clazz.getDeclaredMethods())
            .filter(m -> Modifier.isPublic(m.getModifiers()))
            .collect(Collectors.toList());

      assertEquals("Unexpected number of public methods",
         expectedMethodNames.size(), publicMethods.size());

      assertTrue("Invalid test configuration",
         expectedMethodNames.size() == expectedMethodReturns.size());
      assertTrue("Invalid test configuration",
         expectedMethodNames.size() == expectedMethodParameters.size());

      for (int i = 0; i < expectedMethodNames.size(); i++)
      {
         Method method = clazz.getDeclaredMethod(expectedMethodNames.get(i),
            expectedMethodParameters.get(i));
         assertEquals(expectedMethodReturns.get(i), method.getReturnType());
      }

      // verify that fields are final
      final List<Field> nonFinalFields = Arrays.stream(
         clazz.getDeclaredFields())
            .filter(f -> !Modifier.isFinal(f.getModifiers()))
            .collect(Collectors.toList());

      assertEquals("Unexpected non-final fields", 0, nonFinalFields.size());
   }
}

If you check the list in your test case with the expected number of parameters for each method you will see the following: 如果在测试用例中检查列表,并为每种方法选择预期的参数数量,则会看到以下内容:

final List<Class[]> expectedMethodParameters = Arrays.asList(
     new Class[0],
     new Class[0],
     new Class[0],
     new Class[0],
     new Class[0]
     );

What are you doing here is creating a list of expected number of parameters for the methods that you have defined, and you are setting everything to zero. 您在这里所做的是为已定义的方法创建期望的参数数量列表,并将所有内容都设置为零。 So you are going to assert that all your methods is defined with zero parameters and the method: 因此,您将断言所有方法都使用零参数和方法定义:

 Point.getRadius()

with zero parameters is not defined! 未定义零参数! In the test case you should replace the value in the list expectedMethodParameters at the index that corresponds to the method Point.getRadius() with: 在测试的情况下就应该更换该列表中的值expectedMethodParameters对应于方法的指数Point.getRadius()有:

 new Class[1]

The same error will happen when test attempts to verify method called Point.rotate90() , which also takes one parameter. 当测试尝试验证称为Point.rotate90()方法时,也会发生相同的错误,该方法也Point.rotate90()一个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM