1. Member
Join Date
Jun 2013
Posts
6
Rep Power
0

## 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.

Java 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:

Java 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.

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## 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:

Java 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:
Java 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

3. Member
Join Date
Jun 2013
Posts
6
Rep Power
0

## 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.

4. ## 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.

5. ## 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.

7. Member
Join Date
Jun 2013
Posts
6
Rep Power
0

## Re: Aggregating class

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.
Last edited by Bcode; 06-18-2013 at 05:57 AM.

9. Member
Join Date
Jun 2013
Posts
6
Rep Power
0

## Re: Aggregating class

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.
Last edited by Bcode; 06-18-2013 at 06:27 AM.

10. ## 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.

11. ## Re: Aggregating class

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

db

12. Member
Join Date
Jun 2013
Posts
6
Rep Power
0

## Re: Aggregating class

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.

13. Member
Join Date
Jun 2013
Posts
6
Rep Power
0

## Re: Aggregating class

14. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## 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

15. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Aggregating class

One more recommendation. Since the Point object is not immutable you should do the following:

Java 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

#### Posting Permissions

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