Results 1 to 13 of 13
  1. #1
    crazygurl is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

    Talking Can any1 help me plz??Im having errors with constructors.

    This is an school assignment and i am new to JAVA. I need to create a program which manipulate points in the coordinate plane (x, y).

    I shoud create 2 constructors. The 2nd one 'testpoint' to initialise x- and y- coordinate.

    When i'm trying to compile the program i am having 2 same errors. cannot find symbol : constructor OurPoint(double,int). It is pointing where new is.

    This is the program.. Can anyone help me please???? thx:D




    public class OurPoint {

    private double xCoord;
    private double yCoord;


    public OurPoint( ) {
    xCoord = 0;
    yCoord = 0;
    }

    public OurPoint testPoint( ) {
    OurPoint testPoint = new OurPoint (4.3, -8);
    }


    public void setX (double x) {
    xCoord = x;
    }

    public void setY (double y) {
    yCoord= y;
    }

    public double getX () {
    return xCoord;
    }

    public double getY () {
    return yCoord;
    }





    public static void main (String [] args) {
    OurPoint p = new OurPoint (); // Coordinates of p are (0,0)
    OurPoint q = new OurPoint (-6,-12); // Coordinates of q are (-6, 9)
    double xDiff; // the difference of the x values
    double yDiff; // the difference of the y values
    double distance; // the distance between the points

    xDiff = p.getX() - q.getX();
    yDiff = p.getY() - q.getY();
    distance = Math.sqrt (xDiff * xDiff + yDiff * yDiff);

    System.out.println( "Distance from (0,0) to (-5, -12) is: " +
    distance);
    q.setX(3);
    q.setY(4);

    xDiff = p.getX() - q.getX();
    yDiff = p.getY() - q.getY();
    distance = Math.sqrt (xDiff * xDiff + yDiff * yDiff);

    System.out.println( "Distance from (0,0) to (3,4) is: " +
    distance);
    }
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Define a constructor in your OurPoint class that takes a double and an int as parameters, of course.

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    You don't have the constructor that you are trying to call.
    Put a constructor that takes two doubles and sets those values as x and y values.

  4. #4
    crazygurl is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

    Default

    thx a lot:)

  5. #5
    crazygurl is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

    Default

    After putting a constructor that takes two doubles,it has compile yeah but when i run it,its says

    Exception in thread "main" java.lang.StackOverflowError
    at OurPoint.<init>(OurPoint.java:37)
    at OurPoint.<init>(OurPoint.java:37)
    at OurPoint.<init>(OurPoint.java:37)

    and it goes on this way!!!:(

  6. #6
    AlbertoPL is offline Member
    Join Date
    Sep 2009
    Posts
    22
    Rep Power
    0

    Default

    The only way that could be happening is if you are creating an OurPoint object inside of that new constructor. Can you provide the code to your new constructor?

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Yeah you would get that if your constructor is calling itself!
    Don't let it call itself because that call will again call itself and that would again call itself and that would again ....

    The constructor should just set the x, y values it was passed. If you still have problems then post the constructor that you wrote.

  8. #8
    crazygurl is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

    Default

    Am sorry,i can understand what u mean but am really confused about it.. i am new in writing these kind of programs..

    i declared the 2nd constructor as follows: -



    public OurPoint (double x, double y ) {
    OurPoint testPoint = new OurPoint (4.3, -8);
    }

  9. #9
    AlbertoPL is offline Member
    Join Date
    Sep 2009
    Posts
    22
    Rep Power
    0

    Default

    Yes, that would be the problem. You are already in the constructor, but then you are calling the constructor again!

    Simply do this:

    public OurPoint (double x, double y ) {
    xCoord = x;
    yCoord = y;
    }
    You simply want to set the two numbers to the variables in the class. Note that it looks oddly familiar to your no-parameter constructor : )

  10. #10
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    Java Code:
    public OurPoint (double x, double y ){
       xCoord=x;
       yCoord=y;
    }

  11. #11
    crazygurl is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

    Default

    ohh ok..
    but in the question it says

    The code should create an OurPoint named testPoint that has an x-coordinate with value 4.3, and a y-coordinate with value -8.0.

  12. #12
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    Java Code:
    OurPoint testPoint = new OurPoint (4.3, -8);
    Just put this in your main or some other method.

  13. #13
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by crazygurl View Post
    ohh ok..
    but in the question it says

    The code should create an OurPoint named testPoint that has an x-coordinate with value 4.3, and a y-coordinate with value -8.0.
    Yep your code should create those points but think about where should the points be created from.
    The constructor is the code that is creating the points. Anyone who wants a point calls your constructor. The constructor itself doesn't need to create a new point for itself but should stay generic. Clients pass it x and y values and it gives them back a point dressed with those x and y values.

Similar Threads

  1. constructors?
    By shroomiin in forum New To Java
    Replies: 4
    Last Post: 10-13-2009, 02:14 PM
  2. Replies: 3
    Last Post: 03-08-2009, 01:51 AM
  3. Help with constructors
    By Minime in forum New To Java
    Replies: 3
    Last Post: 04-09-2008, 07:59 AM
  4. Any1 can help?
    By Diimitri in forum Java Applets
    Replies: 0
    Last Post: 03-03-2008, 04:35 PM
  5. constructors
    By khamuruddeen in forum New To Java
    Replies: 2
    Last Post: 12-01-2007, 03:15 PM

Posting Permissions

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