# Thread: LineSegment Class

1. midPoint can't be a double as it has an x and a y component. Consider instead making two fields, double midPointX and double midPointY.

2. Member
Join Date
Oct 2009
Posts
49
Rep Power
0
Java Code:
```public class LineSegment
{
private double x1;
private double x2;
private double y1;
private double y2;
private double length;
private double MidPointX;
private double MidPointY;

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

public void setx1(double dX1){
x1 = dX1;
}
public void setx2(double dX2){
x2 = dX2;
}
public void sety1(double dY1){
y1 = dY1;
}
public void sety2(double dY2){
y2 = dY2;
}
public double setlength(double newlength){
length = newlength;
newlength = Math.sqrt(Math.pow(x2+x1,2) + Math.pow(y2+y1,2));
}
public double setMidPointX(double newMidPointX){
MidPointX = newMidPointX;
newMidPointX = (x1+x2) / 2;
}
public double setMidPointY(double newMidPointY){
MidPointY = newMidPointY;
newMidPointY = (y1+y2) / 2;
}
public double getx1(){
return x1;
}
public double getx2(){
return x2;
}
public double gety1(){
return y1;
}
public double gety2(){
return y2;
}
public double getlength(){
return length;
}
public double getMidPointX(){
return MidPointX;
}
public double getMidPointY(){
return MidPointY;
}

}```
look good??? it doesnt compile though

3. Originally Posted by s_dawg101
look good???
Better

it doesnt compile though
Tells us little. How doesn't it compile? Which line is causing the error? What message are you seeing?

4. Member
Join Date
Oct 2009
Posts
49
Rep Power
0
Java Code:
```LineSegment.java:36: missing return statement
}
^
LineSegment.java:40: missing return statement
}
^
LineSegment.java:44: missing return statement
}
^
3 errors```

5. The error is telling you exactly what is wrong. The error messages are usually quite straightforward. It's telling you that you have a method that is supposed to return a result but that method is missing a .... ?

and the line numbers are telling you where the problems are.

6. Member
Join Date
Oct 2009
Location
Belgium
Posts
18
Rep Power
0
You've got 3 setters, in which your method declaration has a double in it, so it expects that you return a value, but since it is a setter there should be nothing returned. So you need to change "double" in "void"(void= your method does not expect that it returns something).

7. Yeah, I didn't look at the code too carefully. It looks as if he's trying to combine getters and setters in one method. I would posit that midPointX and midPointY shouldn't be fields or set at all, that they should be calculated results calculated on the fly in getters only.

8. Member
Join Date
Oct 2009
Posts
49
Rep Power
0
ok it compiled. now how would i create a class to run this program????

9. Member
Join Date
Oct 2009
Location
Belgium
Posts
18
Rep Power
0
Look up "Main classes" and then in your main class you make a new instance of your LineSegment object (look up "creating an instance of an object")

and then I should take a better look at this piece of code, you already changed double in void but like Fubarable said you're mixing setters and methods that calculate something which is wrong.

Java Code:
```public double setlength(double newlength){
length = newlength;
newlength = Math.sqrt(Math.pow(x2+x1,2) + Math.pow(y2+y1,2));
}
public double setMidPointX(double newMidPointX){
MidPointX = newMidPointX;
newMidPointX = (x1+x2) / 2;
}
public double setMidPointY(double newMidPointY){
MidPointY = newMidPointY;
newMidPointY = (y1+y2) / 2;
}```

10. Member
Join Date
Oct 2009
Posts
49
Rep Power
0
what do i need to correct????

11. Member
Join Date
Oct 2009
Posts
49
Rep Power
0
Java Code:
```import java.util.Scanner;

public class LineSegmentDemo
{
public static void main(String[] args)
{

Scanner keyboard = new Scanner (System.in);

double x1;
double x2;
double y1;
double y2;

System.out.println("What is the first x-coordinate?     ");
x1 = keyboard.nextInt();

System.out.println("What is the first y-coordinate?     ");
y1 = keyboard.nextInt();

System.out.println("What is the second x-coordinate?    ");
x2 = keyboard.nextInt();

System.out.println("What is the second y-coordinate?    ");
y2 = keyboard.nextInt();

}
}```
this is what i have so far. how would i incorporate what the user puts in for the x and y values to the midpoint and distance formula???

12. Member
Join Date
Oct 2009
Posts
49
Rep Power
0
Java Code:
```import java.util.Scanner;

public class LineSegmentDemo
{
public static void main(String[] args)
{

Scanner keyboard = new Scanner (System.in);

double x1;
double x2;
double y1;
double y2;

LineSegment problem = new LineSegment();

System.out.print("What is the first x-coordinate?     ");
x1 = keyboard.nextInt();

System.out.print("What is the first y-coordinate?     ");
y1 = keyboard.nextInt();

System.out.print("What is the second x-coordinate?    ");
x2 = keyboard.nextInt();

System.out.print("What is the second y-coordinate?    ");
y2 = keyboard.nextInt();

System.out.print("The length of the line segment is: "  + problem.getlength());

System.out.print("The mid-point of the line segment is: (" + problem.getMidPointX() + "," + problem.getMidPointY());
}
}```
ok this is what i have for the demo class. it doesnt compile because there is one error.
Java Code:
```symbol  : constructor LineSegment()
location: class LineSegment
LineSegment problem = new LineSegment();
^
1 error```
what does this mean? how do i correct it??

13. The rest of the error message tells you more, doesn't it? It tells you exactly what the problem is, I believe. Post it please.

14. Member
Join Date
Oct 2009
Posts
49
Rep Power
0
thats all the error says

i copied it exactly

15. Member
Join Date
Oct 2009
Location
Belgium
Posts
18
Rep Power
0
Take a look at your LineSegment class and it's constructor, your constructor demands a few things.

16. Member
Join Date
Apr 2009
Posts
54
Rep Power
0
Why have you taken away the constructor you made in your post #4

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

17. Member
Join Date
Oct 2009
Location
Belgium
Posts
18
Rep Power
0
Originally Posted by pellebye
Why have you taken away the constructor you made in your post #4

Java Code:
```public LineSegment( double dX1, double dY1
double dX2, double dY2 ){
x1 = dX1;
x2 = dX2;
y1 = dY1;
y2 = dY2;
}```
He didn't?

18. Member
Join Date
Apr 2009
Posts
54
Rep Power
0
Ok, I just look at post #52 and cannot see no constructor.

19. Member
Join Date
Oct 2009
Location
Belgium
Posts
18
Rep Power
0
Originally Posted by pellebye
Ok, I just look at post #52 and cannot see no constructor.
That's his main class.

20. Member
Join Date
Oct 2009
Posts
49
Rep Power
0
do i need a contructor in the demo class???

Page 3 of 4 First 1234 Last

#### Posting Permissions

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