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

• 10-28-2009, 01:03 PM
crazygurl
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);
}
}
• 10-28-2009, 01:07 PM
Define a constructor in your OurPoint class that takes a double and an int as parameters, of course.
• 10-28-2009, 01:11 PM
r035198x
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.
• 10-28-2009, 01:28 PM
crazygurl
thx a lot:)
• 10-28-2009, 02:17 PM
crazygurl
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!!!:(
• 10-28-2009, 02:31 PM
AlbertoPL
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?
• 10-28-2009, 02:33 PM
r035198x
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.
• 10-28-2009, 02:45 PM
crazygurl
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);
}
• 10-28-2009, 02:49 PM
AlbertoPL
Yes, that would be the problem. You are already in the constructor, but then you are calling the constructor again!

Simply do this:

Quote:

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-28-2009, 02:51 PM
dswastik
Code:

```public OurPoint (double x, double y ){   xCoord=x;   yCoord=y; }```
• 10-28-2009, 02:55 PM
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.
• 10-28-2009, 02:59 PM
dswastik
Code:

`OurPoint testPoint = new OurPoint (4.3, -8);`
Just put this in your main or some other method.
• 10-28-2009, 03:14 PM
r035198x
Quote:

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.