Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By Norm

Thread: Working with Points and Polyline - Need help!

  1. #1
    Thornblom is offline Member
    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

    Default 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 Point[] head; 
    	 private String color = "black"; 
    	 private int width = 1; 
    	 
    	 public Polyline () 
    	 { 
    	 this.horn = new Point[0]; 
    	 } 
    	 
    	 public Polyline (Point[] head) 
    	 { 
    		 
    	 this.head = new Point[head.length]; 
    	 for (int i = 0; i < head.length; i++) {
    	 this.head[i] = new Point (head[i]); 
             }
    	 // this.horn = horn; 
    	 } 
    	 
    	 public String toString () {
    		 String s = "";
    		 int i = 0;
    		 while (i < head.length){
    			 s += head[i++];
    		 }
    		 return s + ", " + color + ", " + width +"" ;
    		 
    	 } 
    	 
    	 
    	 public Point[] getHead ()
    	 {
    		 
    		 return head;
    	 } 
    	 
    	 
    	 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;
    		while (i < head.length-1)
    		{
    		length += head[i].distance(head[i+1]);
    		i++;
    		}
    		
    		return length;
    		 
    	 } 
    	 
    	// Oh, and the add method looks like this (works fine):
             public void add (Point head) 
    	 { 
    	 Point[] h = new Point[this.head.length + 1]; 
    	 int i = 0; 
    	 for (i = 0; i < this.head.length; i++) 
             {
    	       h[i] = this.head[i]; 
             }
    
    	 h[i] = new Point (head);
    	 this.head = h; 
    	 } 
    	 
    	 // No idea what to do here...
    	 public void addAheadOff(Point head, String headName) 
    	 {
    		 	 ????
    	 } 
    	 
              // 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.
    		        }
    		          h = this.head;	 
    	 } 
    
    }

    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);
    		polyline.add(new Point("E", 4,3));
    	        // 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. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Thornblom is offline Member
    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

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

    Quote Originally Posted by Norm View Post
    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. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default 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.
    gimbal2 likes this.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Thornblom is offline Member
    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

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

    Quote Originally Posted by Norm View Post
    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. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Thornblom is offline Member
    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

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

    Quote Originally Posted by Norm View Post
    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. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

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

    The posted code has several compiler errors that need to be fixed.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Thornblom is offline Member
    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

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

    Quote Originally Posted by Norm View Post
    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. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,734
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Thornblom is offline Member
    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

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

    Quote Originally Posted by JosAH View Post
    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. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: 11-06-2013, 11:14 AM
  2. Replies: 1
    Last Post: 08-11-2011, 11:46 AM
  3. Replies: 6
    Last Post: 11-12-2009, 11:50 AM
  4. Drawing a polyline within a JTabbedPane
    By prseifer in forum New To Java
    Replies: 1
    Last Post: 03-05-2009, 01:21 AM
  5. Adding a Polyline drawing to a tab
    By CirKuT in forum New To Java
    Replies: 1
    Last Post: 11-19-2008, 09:32 PM

Posting Permissions

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