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

1. Member
Join Date
Oct 2009
Posts
6
Rep Power
0

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. Senior Member
Join Date
Jun 2008
Posts
2,568
Rep Power
14
Define a constructor in your OurPoint class that takes a double and an int as parameters, of course.

3. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
13
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. Member
Join Date
Oct 2009
Posts
6
Rep Power
0
thx a lot:)

5. Member
Join Date
Oct 2009
Posts
6
Rep Power
0
After putting a constructor that takes two doubles,it has compile yeah but when i run it,its says

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. Member
Join Date
Sep 2009
Posts
22
Rep Power
0
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. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
13
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. Member
Join Date
Oct 2009
Posts
6
Rep Power
0
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. Member
Join Date
Sep 2009
Posts
22
Rep Power
0
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. Senior Member
Join Date
Dec 2008
Location
Kolkata
Posts
286
Rep Power
12
Java Code:
```public OurPoint (double x, double y ){
xCoord=x;
yCoord=y;
}```

11. Member
Join Date
Oct 2009
Posts
6
Rep Power
0
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. Senior Member
Join Date
Dec 2008
Location
Kolkata
Posts
286
Rep Power
12
Java Code:
`OurPoint testPoint = new OurPoint (4.3, -8);`
Just put this in your main or some other method.

13. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
13
Originally Posted by crazygurl
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.

Posting Permissions

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