簡體   English   中英

誰能幫我解決這個Java錯誤?

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

因此,我要進行這個實驗,因為我必須使用一些構造函數來創建基本類以獲取空間中的點……我似乎很難編寫大部分代碼,但是我的測試用例卻給我一個錯誤,指出以下內容。...“ 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());
   }
}

如果在測試用例中檢查列表,並為每種方法選擇預期的參數數量,則會看到以下內容:

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]

當測試嘗試驗證稱為Point.rotate90()方法時,也會發生相同的錯誤,該方法也Point.rotate90()一個參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM