# Aggregating class

• 06-18-2013, 03:49 AM
Bcode
Aggregating class
3. Create an aggregating class

Write a class called "Line". It should store two Point objects. It should have two
constructors, one that receives 4 ints to set the (x,y) of each end, and the other

It should have a distance method that computes the length of the line. Its toString
method should print like this: (x,y) to (x,y)

Write a main method that creates a line with each constructor. It should then print
each line and its length.

Code:

```import java.awt.Point; public class Line {     private Point p1, p2;     private int x1, x2, y1, y2;         public Line(int x1, int x2, int y1, int y2)     {         this.x1 = x1;         this.y1 = y1;         this.x2 = x2;         this.y2 = y2;     }     public Line(Point p1, Point p2)     {         this.p1 = p1;         this.p2 = p2;     }     public double Distance(int x1, int y1, int x2, int y2)     {         return Math.sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) );     }     public String toString()     {         String str = "(" + x1 + "," + y1 + ")" + " to " + "(" + x2 + "," + y2 + ")" +                 " has a length of " + Distance(x1,y1,x2,y2);                 return str;     }         public static void main(String[] args)     {         Line line = new Line(2,4,6,5);         Point point = new Point(4,3);                 System.out.println(line.toString());         System.out.println(point.toString());             }     }```
Output:

Code:

```(2,6) to (4,5) has a length of 2.23606797749979 java.awt.Point[x=4,y=3]```
The Point objects constructor is throwing me off. I am not sure where to go from here.

Any help is appreciated.
• 06-18-2013, 04:30 AM
jim829
Re: Aggregating class
The instructions said that you had to use a two Point object constructor. But that doesn't mean (at least to me) that you can't store them as normal x,y points. So in your point object constructor, do this:

Code:

```public Line(Point p1, Point p2) {   this(p1.x, p2.y, p1.y, p2.y;  // invoke the other constructor }```
I would also re-arrange your constructor so the points are in order of coordinate pairs:
e.g:
Code:

`public Line(x1, y1, x2, y2)`
And adjust the Point constructor accordingly.

This also means that you don't need the Point instance fields. You can leave them in to satisfy the requirements of having two point objects but you really don't need them. You may want to discuss this with your instructor.

Regards,
Jim
• 06-18-2013, 05:02 AM
Bcode
Re: Aggregating class
Thanks.

As far as getting the length of point p1 and p2. How would I find the length? I may be wrong but p1 and p2 is just one point of the line.
• 06-18-2013, 05:05 AM
Junky
Re: Aggregating class
My 0.02 cents

The instructions state that the class must contain 2 Point objects. I take that to mean that you do not need the four x and y instance variables. The constructor that takes the four int parameters should create new Point objects from those values and store them in the instance variables. The second constructor appears correct.
• 06-18-2013, 05:08 AM
Junky
Re: Aggregating class
The Points p1 and p2 are two points and a line exists between them. To calculate the length (and slope if needed) you need do some math research.
• 06-18-2013, 06:10 AM
DarrylBurke
Re: Aggregating class
• 06-18-2013, 06:34 AM
Bcode
Re: Aggregating class
Quote:

Originally Posted by DarrylBurke

Yes, I did. Is there something wrong with that? If you think I am trying to "GET" someone to do it for me you are very wrong.

Thanks.
• 06-18-2013, 07:16 AM
Junky
Re: Aggregating class
• 06-18-2013, 07:24 AM
Bcode
Re: Aggregating class
Quote:

Originally Posted by Junky

Well, before he even posted his responses on every post I had already resolved it(coderanch) so no one would post on it and deleted the yahoo answer post simply because there was no attempt at helping. This was the only forum to give insight on the direction I should go with my question.
• 06-18-2013, 07:29 AM
Junky
Re: Aggregating class
Hey cool down. As I said Daryl was only alerting people to other posts. In future if you do cross post the polite thing is to let people know.
• 06-18-2013, 07:40 AM
DarrylBurke
Re: Aggregating class
Quote:

Originally Posted by Bcode
Yes, I did. Is there something wrong with that?

db
• 06-18-2013, 08:11 AM
Bcode
Re: Aggregating class
Quote:

Originally Posted by Junky
Hey cool down. As I said Daryl was only alerting people to other posts. In future if you do cross post the polite thing is to let people know.

Noted.
• 06-18-2013, 08:15 AM
Bcode
Re: Aggregating class
• 06-18-2013, 02:47 PM
jim829
Re: Aggregating class
Hmm. I agree. For some reason I was stuck on the x,y instance fields. Constructing the two Point objects from the explicit x,y constructor would be the way to go. And it would meet the requirements.

Regards,
Jim
• 06-18-2013, 03:09 PM
jim829
Re: Aggregating class
One more recommendation. Since the Point object is not immutable you should do the following:

Code:

```public Line (Point p1, Point p2) {     this.p1 = new Point(p1); // makes a copy     this.p2 = new Point(p2); // makes a copy }```
It protects the Line instance in case someone decides, unwittingly, to reassign x and y within the Point object that was passed to the constructor. That would then also change the values in your Line.

Regards,
Jim