# method call problem

• 10-03-2012, 10:41 PM
dawnMist
method call problem
Hello, I have been learning Java just for a few days and right in my first little assignment I got stuck.
Here is the code:

Code:

```public class Vertex {   private double x;   private double y;         public void setPt(double x, double y) {       this.x = x;       this.y = y;   }   // Now this method should take another vertex and return their distance. The formula itself is OK.   public double distance(Vertex otherPoint) {       return Math.sqrt(((this.x - otherPoint.x)*(this.x - otherPoint.x)) + ((this.y - otherPoint.y)*(this.y - otherPoint.y)));   } } Vertex a = new Vertex(); Vertex b = new Vertex(); a.setPt(1,1); b.setPt(0,0); b.distance(a); // This call does not calculate the distance. System.out.println("Distance is: " + b);```

What am I doing wrong?
• 10-03-2012, 10:56 PM
Games2Design
Re: method call problem

Also you should ALWAYS post error message.
• 10-03-2012, 11:06 PM
dawnMist
Re: method call problem
Oh, sorry. The main method is just the code on the 18-23 lines.

Code:

```public static void main(String[] args) {          Vertex a = new Vertex();         Vertex b = new Vertex();         a.setPt(1,1);         b.setPt(0,0);         b.distance(a)         System.out.println("Distance is: " + b); }```
And there is no error, it just writes "Distance is: [0.0, 0.0]", which I dont really get either...
• 10-03-2012, 11:14 PM
Games2Design
Re: method call problem
You want the distance between first and second number correcT?
• 10-03-2012, 11:19 PM
dawnMist
Re: method call problem
Well, I want the distance between two 2D points described with two coordinates. The formula is distance = sqrt( (x2-x1)^2 + (y2-y1)^2) ).
So, for my two vertices (a, b) it should be sqrt(2).
• 10-03-2012, 11:21 PM
s3ib
Re: method call problem
First of all your instance variables x and y are private, which means you need to make some kind of geters and seters for them(and access them through those geters. Instead of this.x - otherPoint.x there should be this.x - otherPoint.getX()). Or easier (but more dirty) way is to make your variables public. Secondly you are printing out your vertex object b not the distance between 2 vertex objects a and b.
• 10-03-2012, 11:22 PM
Games2Design
Re: method call problem
Quote:

Originally Posted by dawnMist
Well, I want the distance between two 2D points described with two coordinates. The formula is distance = sqrt( (x2-x1)^2 + (y2-y1)^2) ).
So, for my two vertices (a, b) it should be sqrt(2).

Ok, that confused me. I thought you just wanted the distance between one number and another.
Sadly i am of no help then.
• 10-04-2012, 12:33 AM
rippon
Re: method call problem
Code:

``` b.distance(a); // delete this line System.out.println("Distance is: " + b.distance(a));```

I'm no expert, but I think this is the change you require.
• 10-04-2012, 11:12 AM
dawnMist
Re: method call problem
Thank you both, that was the exact problem.
I have advanced and another problem occured.

Code:

```public class Vertex {   private double x;   private double y;         public void setX(double x) {       this.x = x;   }     public void setY(double y) {       this.y = y;   }       public double getX() {       return x;   }   public double getY() {       return y;    } } public class Triangle {     private double ax;     private double ay;     private double bx;     private double by;     private double cx;     private double cy;         private double distance1;     private double distance2;     private double distance3;         private Vertex a = new Vertex();     private Vertex b = new Vertex();     private Vertex c = new Vertex();         public void setA(Vertex v) {         this.ax = vert.getX();         this.ay = vert.getY();     }         public void setB(Vertex v) {         this.bx = vert.getX();         this.by = vert.getY();     }         public void setC(Vertex v) {         this.cx = vert.getX();         this.cy = vert.getY();     }         public Vertex getA() {         return a;     }         public Vertex getB() {         return b;     }         public Vertex getC() {         return c;     } } public static void main(String[] args) {                 Triangle t = new Triangle();                                Vertex a = new Vertex();         Vertex b = new Vertex();         Vertex c = new Vertex();                 t.setA(a.getX,a.getY); // There is the error: "cannot find symbol - variable getX"         // t.setA(0.0,0.0); -this doesnt work either }```
Where is the problem?
• 10-04-2012, 11:20 AM
achill
Re: method call problem
You have to write

Code:

`t.setA(a.getX(), a.getY());`
because getX() and getY() are methods.
• 10-04-2012, 12:11 PM
dawnMist
Re: method call problem
Quote:

Originally Posted by achill
You have to write

Code:

`t.setA(a.getX(), a.getY());`
because getX() and getY() are methods.

Yes, of course. But I still get this error: "setA cannot be applied to (double, double)" or "getX cannot be applied to (double, double)"
• 10-04-2012, 12:20 PM
Tolls
Re: method call problem
The setter takes a Vertex, not two doubles.
The getter (inderstandably) takes no parameters.
• 10-04-2012, 12:26 PM
achill
Re: method call problem
Ah okay, my fault.
The method setA() wants something of the type Vertex. So just write

Code:

`t.setA(a);`
• 10-04-2012, 12:35 PM
dawnMist
Re: method call problem
Ok. And when I want to set vertex using Triangle? I mean specific values now...
• 10-04-2012, 01:57 PM
achill
Re: method call problem
this way:

Code:

```Vertex a = new Vertex(); a.setX(1.0); a.setY(2.0); t.setA(a);```
or you write a constructor for the class Vertex that takes the values for x and y. So you can write simply:

Code:

`t.setA(new Vertex(1.0, 2.0));`
(code is untested!)
• 10-04-2012, 03:58 PM
DarrylBurke
Re: method call problem
Quote:

Originally Posted by s3ib
First of all your instance variables x and y are private, which means you need to make some kind of geters and seters for them(and access them through those geters. Instead of this.x - otherPoint.x there should be this.x - otherPoint.getX()). Or easier (but more dirty) way is to make your variables public.

The code is in the Vertex class. Hence it can access private members of any instance of the class.

db