Results 1 to 15 of 15
  1. #1
    Bcode is offline Member
    Join Date
    Jun 2013
    Posts
    6
    Rep Power
    0

    Default 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
    that receives two Point objects.

    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. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,341
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Bcode is offline Member
    Join Date
    Jun 2013
    Posts
    6
    Rep Power
    0

    Default 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. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default 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. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

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

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Aggregating class

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    Bcode is offline Member
    Join Date
    Jun 2013
    Posts
    6
    Rep Power
    0

    Default Re: Aggregating class

    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.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Aggregating class

    We alert people of cross-posting so they do not waste their time answering a question that has already been answered elsewhere.

  9. #9
    Bcode is offline Member
    Join Date
    Jun 2013
    Posts
    6
    Rep Power
    0

    Default Re: Aggregating class

    Quote Originally Posted by Junky View Post
    We alert people of cross-posting so they do not waste their time answering a question that has already been answered elsewhere.
    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. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default 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. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Aggregating class

    Quote Originally Posted by Bcode View Post
    Yes, I did. Is there something wrong with that?
    Perhaps you should have gone through the page I linked to in your JavaRanch thread.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    Bcode is offline Member
    Join Date
    Jun 2013
    Posts
    6
    Rep Power
    0

    Default Re: Aggregating class

    Quote Originally Posted by Junky View Post
    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. #13
    Bcode is offline Member
    Join Date
    Jun 2013
    Posts
    6
    Rep Power
    0

    Default Re: Aggregating class

    Also, i'm going to ask my professor about this tomorrow. I don't feel like I completely understand it.

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,341
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,341
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 1
    Last Post: 12-13-2012, 03:06 AM
  2. Replies: 1
    Last Post: 11-29-2012, 09:46 AM
  3. Replies: 0
    Last Post: 12-07-2011, 11:55 AM
  4. Replies: 0
    Last Post: 03-27-2011, 05:49 AM
  5. Replies: 5
    Last Post: 01-26-2011, 06:38 PM

Posting Permissions

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