Page 1 of 4 123 ... LastLast
Results 1 to 20 of 65
  1. #1
    s_dawg101 is offline Member
    Join Date
    Oct 2009
    Posts
    49
    Rep Power
    0

    Question LineSegment Class

    Hey guys I am having a hard time creating a class to model a line segment.

    This class should include a constructor, accessor and mutator methods. It should also include a method that calculates the length of the line segment, which can be calculated using the distance formula and it also must calculate the midpoint formula. It must also have a class to demonstrate the LineSegment.java class.

    I would appreciate it if someone could help me with this task.

    thanks

  2. #2
    pellebye is offline Member
    Join Date
    Apr 2009
    Posts
    54
    Rep Power
    0

    Default

    Why not start with the constructor. Take a look at this google search

    class constructor java - Google Search
    -
    Life is not the worst thing we have ... in a few minutes my coffee is ready.

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Post what you have tried and explain where you are stuck?

  4. #4
    s_dawg101 is offline Member
    Join Date
    Oct 2009
    Posts
    49
    Rep Power
    0

    Default

    public class LineSegment
    {
    private double x1;
    private double x2;
    private double y1;
    private double y2;
    private double length;

    public LineSegment( double dX1, double dY1
    double dX2, double dY2 ){
    x1 = dX1;
    x2 = dX2;
    y1 = dY1;
    y2 = dY2;
    }

    this is what i have so far

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Please use code tags next time you post code.

    So then get on with it. Realize though that your specs say you need a getLength method not property. You can get the length from the points so no need of storing it.

  6. #6
    pellebye is offline Member
    Join Date
    Apr 2009
    Posts
    54
    Rep Power
    0

    Default

    -
    Life is not the worst thing we have ... in a few minutes my coffee is ready.

  7. #7
    s_dawg101 is offline Member
    Join Date
    Oct 2009
    Posts
    49
    Rep Power
    0

    Default

    public class LineSegment
    {
    private double x1;
    private double x2;
    private double y1;
    private double y2;
    private double length;

    public LineSegment( double dX1, double dY1
    double dX2, double dY2 ){
    x1 = dX1;
    x2 = dX2;
    y1 = dY1;
    y2 = dY2;
    }

    public setx1(double dX1){
    x1 = dX1;
    }
    public setx2(double dX2){
    x2 = dX2;
    }
    public sety1(double dY1){
    y1 = dY1;
    }
    public sety2(double dY2){
    y2 = dY2;
    }
    public getx1(){
    return x1;
    }
    public getx2(){
    return x2;
    }
    public gety1(){
    return y1;
    }
    public gety2(){
    return y2;
    }

    does this look good so far???
    Last edited by s_dawg101; 10-11-2009 at 10:54 PM. Reason: not finished

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You are incapable of understanding my request about code tags above?

    P.S Dude, get on with it.

  9. #9
    s_dawg101 is offline Member
    Join Date
    Oct 2009
    Posts
    49
    Rep Power
    0

    Default

    i dont know what those are

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by s_dawg101 View Post
    i dont know what those are
    The forum's FAQ (see my post's signature), will tell you how to use code tags.

  11. #11
    s_dawg101 is offline Member
    Join Date
    Oct 2009
    Posts
    49
    Rep Power
    0

    Default

    could you give me an example??

  12. #12
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by s_dawg101 View Post
    could you give me an example??
    headDesk++ ;

  13. #13
    pellebye is offline Member
    Join Date
    Apr 2009
    Posts
    54
    Rep Power
    0

    Default

    The code tags are here Java Forums - BB Code List
    -
    Life is not the worst thing we have ... in a few minutes my coffee is ready.

  14. #14
    pellebye is offline Member
    Join Date
    Apr 2009
    Posts
    54
    Rep Power
    0

    Default

    Now, go on making a method that calculates the length of the line segment. For that purpose you should have a distance formula.

    methods java - Google Search
    -
    Life is not the worst thing we have ... in a few minutes my coffee is ready.

  15. #15
    s_dawg101 is offline Member
    Join Date
    Oct 2009
    Posts
    49
    Rep Power
    0

    Default

    how would you put the square root in there??

  16. #16
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    See the methods in the Math class.

  17. #17
    s_dawg101 is offline Member
    Join Date
    Oct 2009
    Posts
    49
    Rep Power
    0

    Default

    ok i got that done. now what???

  18. #18
    s_dawg101 is offline Member
    Join Date
    Oct 2009
    Posts
    49
    Rep Power
    0

    Default

    [code] length = sqrt(pow(x2+x1,2)+pow(y2+y1,2)); [code]

    does that look good for the distance formula or does something else need to be added???

  19. #19
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Does it compile?

  20. #20
    pellebye is offline Member
    Join Date
    Apr 2009
    Posts
    54
    Rep Power
    0

    Default

    If you have made methods for the distance and midpoint (and also accessors for these), its time for the last part of the job ... to demonstrate that the class can be used. One way is to add a Main class Main java - Google Search
    -
    Life is not the worst thing we have ... in a few minutes my coffee is ready.

Page 1 of 4 123 ... LastLast

Similar Threads

  1. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 03:48 AM
  2. Replies: 7
    Last Post: 05-06-2009, 01:42 PM
  3. Replies: 8
    Last Post: 03-21-2009, 10:34 AM
  4. Replies: 0
    Last Post: 05-13-2008, 07:16 AM
  5. Class Reflection: Finding super class names
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:12 PM

Tags for this Thread

Posting Permissions

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