Results 1 to 7 of 7
  1. #1
    alsade is offline Member
    Join Date
    Mar 2014
    Posts
    6
    Rep Power
    0

    Question first java exercise

    This is my first java exercise. i got peace of code and i need to write two methods (Length and IsParallel). my question is how can i check line's length for example if the methode doesnt take any variable in its decleration?

    this is the code i got:


    Java Code:
    class Point2D
    {
              private double x;
              private double y;
              Point2D(double xVal, double yVal)
              {
                       x = xVal;
                       y = yVal;
              }
              double getX()
              {
                       return x;
              }
              double getY()
              {
                       return y;
              }
              void details()
              {
                       System.out.println("("+x+","+y+")");
              }
    }
    class Line2D 
    {
              Point2D first;
              Point2D second;
              Line2D(Point2D p1, Point2D p2)
              { 
                       first = p1;
                       second = p2;
              }
              void details()
              {
                       System.out.println("(("+first.getX() + "," + first.getY() + ") , (" + 
                                                                 second.getX() + "," + second.getY() + "))");
              } 
              boolean isParallel(Line2D otherLine)
              {
                       //add your lines here
    
    
    
              }
              double length() 
              {
                       //add your lines here
    
    
              }
     }
    
    public class LinesTest
    { 
              public static void main(String args[])
              {
                       Line2D line1, line2;
                       line1 = new Line2D(new Point2D(10,20), new Point2D(5,15));
                       line2 = new Line2D(new Point2D(4,4), new Point2D(33,22));
                       System.out.println("The length of line1 is : " + line1.length());
                       System.out.println("The length of line2 is : " + line2.length());
                       if(line1.isParallel(line2))
                       {
                                 System.out.println("line1 is parallel to line2");
                       }
                       else
                       {
                                 System.out.println("line1 is not parallel to line2");
                       }
              }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: first java exercise

    i need to write two methods (Length and IsParallel)
    Do you have a description of what those methods are supposed to do? You will need that BEFORE you can write any code.

    how can i check line's length
    The class that the methods are in contain data that can be used by the methods.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    alsade is offline Member
    Join Date
    Mar 2014
    Posts
    6
    Rep Power
    0

    Default Re: first java exercise

    yes, here is what they supposed to do:
    The length method should return the length of the line and the isParallel method should check whether this line is parallel to another line represented by another object.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: first java exercise

    Do you know where the data is that can be used to do that?
    Given the data, you will need some formulas to do the computation.
    Ask an internet Search engine for those formulas.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    alsade is offline Member
    Join Date
    Mar 2014
    Posts
    6
    Rep Power
    0

    Default Re: first java exercise

    the only data i got is in the main and i know the formulas to do that. what i dont know its how make the methode Length (for example) work on line1 , as it shows in the main , without sending any parameters to the Length methode...

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: first java exercise

    A line is defined by two points, one at each end. The length of that line will be the distance between the points.
    The Line2D class has two Point objects. Use the values in them.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    exonaut is offline Member
    Join Date
    Mar 2013
    Location
    USA
    Posts
    28
    Rep Power
    0

    Default Re: first java exercise

    You'll want to look into Vector math. I don't believe Java has native support for Vectors or Vector math but you can make your own or find a basic library. Basically a 2D Vector is a set of points (x, y) from the origin (0, 0) in 2D space. There are mathematical formulas to calculate the distance between two vectors, their lengths, etc.

Similar Threads

  1. java exercise help please
    By javaprogr in forum New To Java
    Replies: 0
    Last Post: 11-16-2013, 07:18 PM
  2. Java exercise
    By Mbka in forum New To Java
    Replies: 9
    Last Post: 06-28-2012, 09:58 PM
  3. Java exercise I REALLY need help on
    By velvetymold in forum New To Java
    Replies: 2
    Last Post: 03-27-2011, 04:02 AM
  4. Exercise for java 3d
    By armiri in forum Java 2D
    Replies: 2
    Last Post: 05-14-2010, 12:14 AM
  5. Exercise for java 3d
    By armiri in forum Java Software
    Replies: 3
    Last Post: 05-14-2010, 12:13 AM

Posting Permissions

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