Results 1 to 13 of 13
- 10-28-2009, 12:03 PM #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);
}
}
- 10-28-2009, 12:07 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Define a constructor in your OurPoint class that takes a double and an int as parameters, of course.
- 10-28-2009, 12:11 PM #3
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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, 12:28 PM #4
Member
- Join Date
- Oct 2009
- Posts
- 6
- Rep Power
- 0
thx a lot:)
- 10-28-2009, 01:17 PM #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
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!!!:(
- 10-28-2009, 01:31 PM #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?
- 10-28-2009, 01:33 PM #7
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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, 01:45 PM #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);
}
- 10-28-2009, 01:49 PM #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:
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 : )public OurPoint (double x, double y ) {
xCoord = x;
yCoord = y;
}
- 10-28-2009, 01:51 PM #10
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 280
- Rep Power
- 5
Java Code:public OurPoint (double x, double y ){ xCoord=x; yCoord=y; }
- 10-28-2009, 01:55 PM #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.
- 10-28-2009, 01:59 PM #12
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 280
- Rep Power
- 5
Just put this in your main or some other method.Java Code:OurPoint testPoint = new OurPoint (4.3, -8);
- 10-28-2009, 02:14 PM #13
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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
-
constructors?
By shroomiin in forum New To JavaReplies: 4Last Post: 10-13-2009, 02:14 PM -
What is the difference between Semantic Errors and Logical Errors?
By tlau3128 in forum New To JavaReplies: 3Last Post: 03-08-2009, 01:51 AM -
Help with constructors
By Minime in forum New To JavaReplies: 3Last Post: 04-09-2008, 07:59 AM -
Any1 can help?
By Diimitri in forum Java AppletsReplies: 0Last Post: 03-03-2008, 04:35 PM -
constructors
By khamuruddeen in forum New To JavaReplies: 2Last Post: 12-01-2007, 03:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks