简体   繁体   中英

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()"

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:

 new Class[1]

The same error will happen when test attempts to verify method called Point.rotate90() , which also takes one parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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