-1

Can someone please enlighten me as to what I am doing wrong? I have the errors listed below the code. I am just getting my feet wet with Java so, please be nice in your response.

//The Point class definition
public class Point 
    {
    private int x;
    private int y;

   // Constructors
    public Point() 
    {
      x = 0;
      y = 0;
   }

    public Point(int newX, int newY) 
    {
      x = newX;
      y = newY;
    }
  // Getters and Setters
    public int getX() 
    {
      return x;
    }

     public int getY() 
    {
      return y;
    }

    public void setX(int newX) 
    {
      x = newX;
    }

    public void setY(int newY) 
    {
      y = newY;
    }

   public double distance(Point another) //takes one parameter of Point type and returns a double
    {
      double xDiff = x - another.x;
      double yDiff = y - another.y;
      return Math.sqrt(xDiff*xDiff + yDiff*yDiff);
    }

    public void translate(int dx, int dy)
//takes two int parameters for values used to translate,returns a new point with a new location (do not update current location) point. 
    {                     
        x = x + dx;
        y = y + dy;
    }  

    public void setXY(int newX, int newY) //updates the location of the point all at one time.
    {
      x = newX;
      y = newY;
    }

    public String toString() 
    {
      return "(" + x + "," + y + ")";
    }

}

Error when I run the driver:

 PointDriver.java:24: error: method distance in class Point cannot
 be applied to given types;
        pt3 = pt2.distance(3, -10);
                 ^    required: Point    found: int,int    reason: actual and formal argument lists differ in length 
 PointDriver.java:31: error: method distance in class Point cannot be
 applied to given types;
        pt1 = pt1.distance(4, -2);
                 ^    required: Point
5
  • 3
    What part of the error message don't you understand?
    – SLaks
    Oct 20, 2014 at 20:11
  • Since the errors occur in PointDriver maybe showing that code would be beneficial? Were you supposed to define a method public double distance( int x, int y ){ }?
    – clcto
    Oct 20, 2014 at 20:13
  • 1
    You have defined the distance() method to take a Point parameter, but are trying to pass in two integers.
    – BarrySW19
    Oct 20, 2014 at 20:14
  • I changed the return to double but still received the same error. Thank you for your input.
    – NoobieDoo
    Oct 20, 2014 at 20:37
  • The error message is not telling you about the return value, it's telling you about the argument types: "method distance in class Point cannot be applied to given types. ... required: Point found: int,int" It's saying you called distance(x, y) where x and y are ints. But that's not how you declared it. You declared it to take one Point. Oct 20, 2014 at 21:39

4 Answers 4

0

Distance nethod need a Point not a separates coordinates. Use:

pt3 = pt2.distance(new Point(3, -10));
0

You should pass distance a Point object not 2 integers. Try something like this:

pt2.distance(new Point(3, -10));

That is what you are probably looking for.

0

It looks like the error is happening because you are passing two integers to a method that is expecting a Point. But without seeing your code that is actually referenced in the error, it's hard to know for sure.

I'm going to guess that your line:

pt3 = pt2.distance(3, -10);

should probably look like this:

pt3 = pt2.distance(new Point(3, -10));

or possibly like this:

Point p4 = new Point(3,-10);
pt3 = pt2.distance(pt4);

Or maybe you should write a new method in Point:

public double distance(int x, int y) 
{
    Point p = new Point(x,y);
    return this.distance(p);
}
0

The problem is, that you didn't have called the method distance with a Point. It will work, when you do it like this:

 Point t = new Point(4, -2);
 pt1.distance(t);

The other problem lies in

pt1 = pt1.distance(4, -2), because the pt1 is a point, but the returning value is a double.

You could do that like this:

double p1 = pt1.distance(4, -2)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.