# Thread: Working with Points and Polyline - Need help!

1. Member
Join Date
Dec 2013
Posts
6
Rep Power
0

## Working with Points and Polyline - Need help!

Hey all!
I just started study Java, and so far so good, it's fun and challenging.
Right now, I'm pretty stuck, and I need some help.
My teacher has given me a problem with Points and Polylines. I did make the class "Point", "Polyline" and "PolylineTest". So, I've done all basic stuff like make the polyline and print it out. Now I need to create a method inside "Polyline" that adds one point after another specified point (I call these points "heads" due to less confuesness). (I've done a method that adds a point at the end of the polyline). I also need to create a method that removes a certain point from the polyline.

These specified arguments must be used.

EDIT: Complete classes is posted

Java Code:
```public class Polyline {
private String color = "black";
private int width = 1;

public Polyline ()
{
this.horn = new Point[0];
}

{

for (int i = 0; i < head.length; i++) {
}
// this.horn = horn;
}

public String toString () {
String s = "";
int i = 0;
}
return s + ", " + color + ", " + width +"" ;

}

{

}

public String getColor () {

return Color;

}

public int getWidth () {

return width;
}

public void setColor (String color)
{
this.color = color;

}

public void setWidth (int width)
{

this.width = width;
}

public double length ()
{
int i = 0;
double length = 0;
{
i++;
}

return length;

}

// Oh, and the add method looks like this (works fine):
{
Point[] h = new Point[this.head.length + 1];
int i = 0;
for (i = 0; i < this.head.length; i++)
{
}

}

// No idea what to do here...
{
????
}

// This one have I begun working with a bit, it is not a runable method. (I must use a loop method for this one):
public void remove (String headName) {
Point[] h = new Point[this.head.length - 1];
int i = 0;
int j = 0;
for (i = 0; i < this.horn.length; i++)
{
// so I want to compare the name of the head I want to remove
// with the head we see right now in the list head, but I cant compare
// a String with a double..
if (hornName == ?? )
h[j++] = head[i]; // maybe something like this, donno really.
}
}

}```

The test program looks like this at the moment:
Java Code:
```import java.io.*;
public class PolylineTest
{
public static void main (String[] args)
{
PrintWriter	out = new PrintWriter (System.out, true);
Point 	p1 = new Point ("A", 3, 4);
Point 	p2 = new Point ("B", 5, 6);
Point 	p3 = new Point ("C", 3, 4);
Point 	p4 = new Point ("D", 79, 65);

Point[] head = {p1, p2, p3, p4};

Polyline polyline = new Polyline (head);
out.println(polyline.length());
polyline.setColor("orange");
polyline.setWidth(5);
out.println("Polyline: " +polyline);
// I want to use the remove method like this:
// polyline.remove(Point("C", 79, 65);
// got no idea about the other one.

}
}```
The Point class:
Java Code:
```class Point {

private double x;
private double y;
private String name;

// constructors

Point (String a, double x, double y)
{
this.name = a;
this.x = x;
this.y = y;
}

Point (Point p)
{
this.x = p.x;
this.y = p.y;
}

// methods

public double getX()
{
return x;
}

public double getY()
{
return y;
}

public String getName()
{
return name;
}

public String toString(){
String name = "";
name = "("+ x +", "+ y +")";
return name;
}

public void setX (double x)
{
this.x = x;
}

public void setY (double y)
{
this.y = y;
}

// distance to a certain point
double distance (Point p)
{
double d = 0;
d = Math.sqrt((p.x-this.x)*(p.x -this.x)+(p.y-this.y)*(p.y-this.y));
return d;
}
}```

I would appriciate if you could help me out here!
Last edited by Thornblom; 12-11-2013 at 07:04 PM.

2. ## Re: Working with Points and Polyline - Need help!

One potential problem I see is that there are no {}s to enclose code in if and for statements. Always enclose the code in {}s to make the code clearer to read and to prevent errors.

3. Member
Join Date
Dec 2013
Posts
6
Rep Power
0

## Re: Working with Points and Polyline - Need help!

Originally Posted by Norm
One potential problem I see is that there are no {}s to enclose code in if and for statements. Always enclose the code in {}s to make the code clearer to read and to prevent errors.
OK, thats not the problem, but thanks anyway, will remember that one.

I don't really know how to think when adding another point in a certain place.
I know tho, that a must compare the names of some points (heads), to remove any of em. But I don't know how to get that down into code...
Last edited by Thornblom; 12-11-2013 at 06:14 PM.

4. ## Re: Working with Points and Polyline - Need help!

Its a problem for me when I try to read code without the {}s. I don't like having to guess if this code is in the loop or not. Or if I add some println() statements inside a loop, does it break the code.
Last edited by Norm; 12-11-2013 at 06:45 PM.

5. Member
Join Date
Dec 2013
Posts
6
Rep Power
0

## Re: Working with Points and Polyline - Need help!

Originally Posted by Norm
Its a problem for me when I try to read code without the {}s. I don't like having to guess if this code in the loop or not. Or if I add some println() statements inside a loop, does it break the code.
Of course, sorry.

I did add brackets in the "add" method, the "remove" method is just some of my thoughts, don't really now if I'm on the right track there, added some brackets where I think is right tho.

Also: I refer to a class named Point, which is not showed in this thread, but it's a complete working class. I could post it here if it would help... Same for the Polyline class, just the problematic parts posted.
Last edited by Thornblom; 12-11-2013 at 06:45 PM.

6. ## Re: Working with Points and Polyline - Need help!

Next problem is having a small, complete program that compiles, executes and shows the problem. It's hard to work with the bits and pieces of code that has been posted.

7. Member
Join Date
Dec 2013
Posts
6
Rep Power
0

## Re: Working with Points and Polyline - Need help!

Originally Posted by Norm
Next problem is having a small, complete program that compiles, executes and shows the problem. It's hard to work with the bits and pieces of code that has been posted.
Done. Sorry again, new at this...

8. ## Re: Working with Points and Polyline - Need help!

The posted code has several compiler errors that need to be fixed.

9. Member
Join Date
Dec 2013
Posts
6
Rep Power
0

## Re: Working with Points and Polyline - Need help!

Originally Posted by Norm
The posted code has several compiler errors that need to be fixed.
Well, it works good for me. Working in Eclipse....
I've translated all methods from Swedish, but what I see, everything should work as it does for me...

10. ## Re: Working with Points and Polyline - Need help!

I noticed that your PolyLine class is just an old fashioned array of Points; had you used a List<Point> instead and maybe a Comparable<Point> interface implementation in the Point class itself, you wouldn't have had all of those nasty little bookkeeping problems ...

kind regards,

Jos

11. Member
Join Date
Dec 2013
Posts
6
Rep Power
0

## Re: Working with Points and Polyline - Need help!

Originally Posted by JosAH
I noticed that your PolyLine class is just an old fashioned array of Points; had you used a List<Point> instead and maybe a Comparable<Point> interface implementation in the Point class itself, you wouldn't have had all of those nasty little bookkeeping problems ...

kind regards,

Jos
It is, and thats the way my teacher wants me to do it... He gave me the PolyLine class with empty methods. My task is to complete it, and create a simple test program. And as it is for now, I've two methods left, that I can't figure out..

12. ## Re: Working with Points and Polyline - Need help!

Here is one line of code (line 41) that does NOT compile:
Java Code:
`         return Color;`
There are several others.

You need to compile all the code that is posted and fix its errors.

#### Posting Permissions

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