Home arrow static arrow Java Programming [Archive] - constructor problem
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - constructor problem
This topic has 4 replies on 1 page.

Posts:8
Registered: 2/15/04
constructor problem  
Jun 21, 2004 1:08 PM



 
Im new to java and i hava difficulties understanding construtors.
I have 2 constructors in Line.java and 2 constructors in Point.java:
class Line
{
Line (final Point start, final Point end)
{
this.start = new Point(start);
this.end = new Point(end);
}

Line (double xStart, double yStart, double xEnd, double yEnd)
{
start = new Point (xStart, yStart);
end = new Point (xEnd, yEnd);
}
Point intersects (final Line line1)
{
Point localPoint = new Point (0, 0);

double num =
(this.end.y - this.start.y) * (this.end.x - line1.start.x) -
(this.end.x - this.start.x) * (this.end.y - line1.start.y);
//
rest of the code
}
}


And 2 constructors from Point.java:

class Point
{
double x;
double y;

Point (double xVal, double yVal)
{
x = xVal;
y = yVal;
}

Point(final Point oldPoint)
{
x = oldPoint.x;
y = oldPoint.y;
}
// --- rest of the code
}

I have 2 questions about the above 2 files:
1. where it says "Point intersects (final Line line1)"
which Line constructor is it refering to?

2. where it says "this.end.y" and "line1.start.x"
which x or y is it refering to?

Im really confused about the constructor,
please help.
 

Posts:6,750
Registered: 1/25/04
Re: constructor problem  
Jun 21, 2004 1:13 PM (reply 1 of 4)



 
I have 2 questions about the above 2 files:
1. where it says "Point intersects (final Line line1)"
which Line constructor is it refering to?

I don't see that referring to a constructor at all.

2. where it says "this.end.y" and "line1.start.x"
which x or y is it refering to?

The y is the the field y of the object referred to by end, which is a variable in the current instance ("this") of the class Line. The x is the field x of the object referred to by start, which is a field of the object referred to by line1, which is an argument to the method. Does that help?
 

Posts:3,258
Registered: 00-08-28
Re: constructor problem  
Jun 21, 2004 1:17 PM (reply 2 of 4)



 
you have two constructors
Line (final Point start, final Point end){this.start = new Point(start);this.end = new Point(end);} Line (double xStart, double yStart, double xEnd, double yEnd){start = new Point (xStart, yStart);end = new Point (xEnd, yEnd);}

when you say
Point intersects (final Line line1) you are assigning a variable name to instance of Line. The constructor is called when you are initializing the state of an instance, i.e. calling the new operator. So which constructor to be called is not going to be defined here as a matter of fact it might not even be a constructor of class Line but maybe a subclass of Line.


2)The answer to your second question is that you r refering to the double x; double y; variables in point class of end instance defined in Line class.

 

Posts:945
Registered: 3/13/02
Re: constructor problem  
Jun 21, 2004 1:19 PM (reply 3 of 4)



 
Point intersects(final Line line1)                ------------------{   //...blah blah blah   return p;}


The underlined area is where you are confused. No Line instance is being constructed. What you see is an argument parameter.

A copy of a reference to an (already created) Line object is being passed to the intersects method.
 

Posts:8
Registered: 2/15/04
Re: constructor problem  
Jun 21, 2004 2:34 PM (reply 4 of 4)



 
Thank you guys, I read through my programs with your explanations and I understand better now! :)
thanx a lot.
 
This topic has 4 replies on 1 page.