1. Member
Join Date
Oct 2012
Posts
43
Rep Power
0

## 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:

Java 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?
Last edited by dawnMist; 10-03-2012 at 10:45 PM.

2. Member
Join Date
Sep 2012
Posts
44
Rep Power
0

## Re: method call problem

Also you should ALWAYS post error message.

3. Member
Join Date
Oct 2012
Posts
43
Rep Power
0

## Re: method call problem

Oh, sorry. The main method is just the code on the 18-23 lines.

Java 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...

4. Member
Join Date
Sep 2012
Posts
44
Rep Power
0

## Re: method call problem

You want the distance between first and second number correcT?
Last edited by Games2Design; 10-03-2012 at 11:18 PM.

5. Member
Join Date
Oct 2012
Posts
43
Rep Power
0

## 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).

6. Member
Join Date
Dec 2010
Posts
18
Rep Power
0

## 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.

7. Member
Join Date
Sep 2012
Posts
44
Rep Power
0

## Re: method call problem

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.

8. Member
Join Date
Nov 2010
Posts
38
Rep Power
0

## Re: method call problem

Java 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.

9. Member
Join Date
Oct 2012
Posts
43
Rep Power
0

## Re: method call problem

Thank you both, that was the exact problem.
I have advanced and another problem occured.

Java 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. Member
Join Date
Jan 2011
Location
Bielefeld, Germany
Posts
10
Rep Power
0

## Re: method call problem

You have to write

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

11. Member
Join Date
Oct 2012
Posts
43
Rep Power
0

## Re: method call problem

Originally Posted by achill
You have to write

Java 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)"
Last edited by dawnMist; 10-04-2012 at 12:17 PM.

12. Moderator
Join Date
Apr 2009
Posts
13,222
Rep Power
23

## Re: method call problem

The setter takes a Vertex, not two doubles.
The getter (inderstandably) takes no parameters.

13. Member
Join Date
Jan 2011
Location
Bielefeld, Germany
Posts
10
Rep Power
0

## Re: method call problem

Ah okay, my fault.
The method setA() wants something of the type Vertex. So just write

Java Code:
`t.setA(a);`

14. Member
Join Date
Oct 2012
Posts
43
Rep Power
0

## Re: method call problem

Ok. And when I want to set vertex using Triangle? I mean specific values now...
Last edited by dawnMist; 10-04-2012 at 01:13 PM.

15. Member
Join Date
Jan 2011
Location
Bielefeld, Germany
Posts
10
Rep Power
0

## Re: method call problem

this way:

Java 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:

Java Code:
`t.setA(new Vertex(1.0, 2.0));`
(code is untested!)

16. ## Re: method call problem

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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•