任何人都可以帮我解决这个java错误???

时间:2018-01-17 20:24:48

标签: java arrays

所以我有这个实验室,因为我必须用一些构造函数创建一个基本类来获取空间中的点...我似乎很难写出我的大部分代码,但我的测试用例给了我一个说明以下内容的错误.... " java.lang.NoSuchMethodException:Point.getRadius()"

任何人都可以帮我理解我的代码出错了吗?

我的代码

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

    }

}

现在这是我的测试用例

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

1 个答案:

答案 0 :(得分:0)

如果您检查测试用例中的列表以及每种方法的预期参数数量,您将看到以下内容:

<div class="row">
               
  <div class="panel panel-default">

  <!-- /.panel-heading -->
  <div class="panel-body pn">


    <div style="overflow-x:auto;">

      <br>
      <div class="table-responsive">
      
       <div class="panel-heading">
        <span class="panel-title">
        <span class="fa fa-table"></span><font color="blue">Se</font></span>
       </div>
      
        <table class="table table-bordered mbn">
        <tr>
          <th style="width:8%;">Enq</th>
          <th style="width:8%;">Da</th>
          <th style="width:10%;">Bu</th>
          <th style="width:10%;">Prop</th>
          <th style="width:14%;">Pr</th>
          <th style="width:10%;">District</th>
          <th style="width:10%;">City</th>
          <th style="width:10%;">Bedrooms</th>
          <th style="width:10%;">Details</th>
          <th style="width:10%;">Update</th>
        </tr>
        <tr>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td>Det</td>
          <td>ed</td>	
        </tr>
        </table>
      </div>
    </div>
   </div>
 </div>
</div>

您在这里做的是为您定义的方法创建预期数量的参数列表,并将所有内容设置为零。因此,您将声明所有方法都使用零参数和方法定义:

final List<Class[]> expectedMethodParameters = Arrays.asList(
     new Class[0],
     new Class[0],
     new Class[0],
     new Class[0],
     new Class[0]
     );
没有定义零参数的

!在测试用例中,您应该将列表 Point.getRadius() 中的值替换为与方法expectedMethodParameters对应的索引:

Point.getRadius()

当测试尝试验证名为 new Class[1] 的方法时,也会发生同样的错误,该方法也需要一个参数。