1. Member
Join Date
Mar 2014
Posts
6
Rep Power
0

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

}
double length()
{

}
}

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

3. Member
Join Date
Mar 2014
Posts
6
Rep Power
0

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

5. Member
Join Date
Mar 2014
Posts
6
Rep Power
0

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

7. Member
Join Date
Mar 2013
Posts
21
Rep Power
0

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

#### Posting Permissions

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