Results 1 to 16 of 16
  1. #1
    dawnMist is offline Member
    Join Date
    Oct 2012
    Posts
    43
    Rep Power
    0

    Default 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 11:45 PM.

  2. #2
    Games2Design is offline Member
    Join Date
    Sep 2012
    Posts
    44
    Rep Power
    0

    Default Re: method call problem

    Where is your main method?

    Also you should ALWAYS post error message.

  3. #3
    dawnMist is offline Member
    Join Date
    Oct 2012
    Posts
    43
    Rep Power
    0

    Default 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. #4
    Games2Design is offline Member
    Join Date
    Sep 2012
    Posts
    44
    Rep Power
    0

    Default Re: method call problem

    You want the distance between first and second number correcT?
    Last edited by Games2Design; 10-04-2012 at 12:18 AM.

  5. #5
    dawnMist is offline Member
    Join Date
    Oct 2012
    Posts
    43
    Rep Power
    0

    Default 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. #6
    s3ib is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default 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. #7
    Games2Design is offline Member
    Join Date
    Sep 2012
    Posts
    44
    Rep Power
    0

    Default Re: method call problem

    Quote Originally Posted by dawnMist View Post
    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. #8
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default 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. #9
    dawnMist is offline Member
    Join Date
    Oct 2012
    Posts
    43
    Rep Power
    0

    Default 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. #10
    achill is offline Member
    Join Date
    Jan 2011
    Location
    Bielefeld, Germany
    Posts
    10
    Rep Power
    0

    Default Re: method call problem

    You have to write

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

  11. #11
    dawnMist is offline Member
    Join Date
    Oct 2012
    Posts
    43
    Rep Power
    0

    Default Re: method call problem

    Quote Originally Posted by achill View Post
    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 01:17 PM.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

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

    ** This space for rent **

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

    Default 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. #14
    dawnMist is offline Member
    Join Date
    Oct 2012
    Posts
    43
    Rep Power
    0

    Default 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 02:13 PM.

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

    Default 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. #16
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: method call problem

    Quote Originally Posted by s3ib View Post
    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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. call a method from another method in same class
    By rockstaedy in forum New To Java
    Replies: 5
    Last Post: 10-03-2012, 03:42 PM
  2. Replies: 3
    Last Post: 03-28-2012, 12:23 AM
  3. Method call problem.
    By Lufc in forum New To Java
    Replies: 3
    Last Post: 05-10-2011, 10:00 PM
  4. how to call method?
    By leapinlizard in forum New To Java
    Replies: 9
    Last Post: 04-30-2009, 12:55 AM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 09:37 AM

Posting Permissions

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