Results 1 to 16 of 16
Thread: method call problem
- 10-03-2012, 10:41 PM #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.
- 10-03-2012, 10:56 PM #2
Member
- Join Date
- Sep 2012
- Posts
- 44
- Rep Power
- 0
Re: method call problem
Where is your main method?
Also you should ALWAYS post error message.
- 10-03-2012, 11:06 PM #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.
And there is no error, it just writes "Distance is: [0.0, 0.0]", which I dont really get either...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); }
- 10-03-2012, 11:14 PM #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.
- 10-03-2012, 11:19 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).
- 10-03-2012, 11:21 PM #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.
- 10-03-2012, 11:22 PM #7
Member
- Join Date
- Sep 2012
- Posts
- 44
- Rep Power
- 0
- 10-04-2012, 12:33 AM #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.
- 10-04-2012, 11:12 AM #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.
Where is the problem?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 }
- 10-04-2012, 11:20 AM #10
Member
- Join Date
- Jan 2011
- Location
- Bielefeld, Germany
- Posts
- 10
- Rep Power
- 0
Re: method call problem
You have to write
because getX() and getY() are methods.Java Code:t.setA(a.getX(), a.getY());
- 10-04-2012, 12:11 PM #11
Member
- Join Date
- Oct 2012
- Posts
- 43
- Rep Power
- 0
- 10-04-2012, 12:20 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: method call problem
The setter takes a Vertex, not two doubles.
The getter (inderstandably) takes no parameters.Please do not ask for code as refusal often offends.
- 10-04-2012, 12:26 PM #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);
- 10-04-2012, 12:35 PM #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.
- 10-04-2012, 01:57 PM #15
Member
- Join Date
- Jan 2011
- Location
- Bielefeld, Germany
- Posts
- 10
- Rep Power
- 0
Re: method call problem
this way:
or you write a constructor for the class Vertex that takes the values for x and y. So you can write simply:Java Code:Vertex a = new Vertex(); a.setX(1.0); a.setY(2.0); t.setA(a);
(code is untested!)Java Code:t.setA(new Vertex(1.0, 2.0));
- 10-04-2012, 03:58 PM #16
Similar Threads
-
call a method from another method in same class
By rockstaedy in forum New To JavaReplies: 5Last Post: 10-03-2012, 02:42 PM -
ArrayList problem, cant call method from object in array.
By Chip in forum New To JavaReplies: 3Last Post: 03-27-2012, 11:23 PM -
Method call problem.
By Lufc in forum New To JavaReplies: 3Last Post: 05-10-2011, 09:00 PM -
how to call method?
By leapinlizard in forum New To JavaReplies: 9Last Post: 04-29-2009, 11:55 PM -
cannot call private method from static method
By jon80 in forum New To JavaReplies: 3Last Post: 05-07-2008, 08:37 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks