Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 21 of 21

Thread: Create a polygone

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Create a polygone

    Hi all! My name is Dan and I'm first time here. Hope someone can help me with the task.
    The exercice is from my exam and it sounds like this:

    a) Define proper data structures to represent a polygone. The endpoints of the line segments are defined by two
    float values. Make sure in the design of your data structures that an existing polygon can be easily modified by
    dynamically adding and removing line segments.

    b) Implement a function createPolygon that will create a new polygon based on an array of end points passed as
    argument, such as for example
    float endpoints[][2] = { {2.7, 5.4}, {1.1, 2.8}, {1.9, 1.3}};


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Create a polygone

    Welcome r0x,

    I'm afraid we cannot provide a solution for you, but if you show us what you have so far and what you are stuck on then we should be able to point you in the right direction with your problem.


    Regards,
    Chris

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Create a polygone

    ok

    public class PolygoneMain {
       public static void main(String[] args) {
     
    	   float[][2] endpoints = { {2.7,5.4}, {1.1,2.8}, {1.9,1.3} };
     
    	}
     
    }

    Multiple markers at this line
    - Type mismatch: cannot convert from double to
    float
    - Syntax error on token "2", delete this token
    - Type mismatch: cannot convert from double to
    float
    - Type mismatch: cannot convert from double to
    float
    - Type mismatch: cannot convert from double to
    float
    - Type mismatch: cannot convert from double to
    float
    - Type mismatch: cannot convert from double to
    float

    1) why this array can't be float ?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Create a polygone

    2.7 is a double. 2.7f is a float. (float)2.7 is also a float.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Create a polygone

    ok thanks but I changed to double
    so I was thinking may be an ArrayList would be a good choice so here is the code

    public class Point {
     
    	private double x;
    	private double y;
     
    	public Point(double x, double y) {
    		this.x = x;
    		this.y = y;
    	}
     
    }

    import java.util.*;
     
    public class PolygoneMain {
     
    	public static void main(String[] args) {
     
    		ArrayList<Double> points = new ArrayList<Double>();
     
    		points.add(new Point(2.7, 5.4));
     
    	}
    }

    ERROR: The method add(Double) in the type ArrayList<Double> is not applicable for the arguments
    (Point)

    Here I don't understand how to solve the error, because basically the add method accepts Objects and Point for me it's an object right?

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Create a polygone

    No. Your points ArrayList only accepts Doubles. A Point is not a Double, it contains Doubles (or doubles). Trying to add a Point to an ArrayList of Doubles won't work. Perhaps you want an ArrayList of Points instead?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Jan 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Create a polygone

    I got it ) thank you

  8. #8
    Junior Member
    Join Date
    Jan 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Create a polygone

    public class Point {
     
    	private double x;
    	private double y;
     
    	public Point(double x, double y) {
    		this.x = x;
    		this.y = y;
    	}
     
    }


    import java.util.*;
     
    public class PolygoneMain {
     
    	public static void main(String[] args) {
     
    		ArrayList<Point> points = new ArrayList<Point>();
     
    		points.add(new Point(2.7, 5.4));
    		points.add(new Point(3.1, 10.5));
    		points.add(new Point(1.1, 34.3));
     
    		System.out.println(points.size());
    		System.out.println("Contents of points: " + points);
     
    	}
    }

    Console:
    3
    Contents of points: [Point@268b819f, Point@10eb017e, Point@4d20a47e]

    Why there is this abrakadabra as the contents of points ?

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Create a polygone

    Because that's what's returned by Point.toString(), which is probably just using Object's toString().

    What did you expect to print out? Java doesn't automatically know how to format a String version of an Object. For example, what do you think should happen if you did something like this:

    JFrame frame = new JFrame();
    System.out.println("Frame: " + frame);

    If you want something else to print out, you'll have to get at that information yourself.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Junior Member
    Join Date
    Jan 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Create a polygone

    the problem is that I have no idea what toString is, I heard about this but never understood
    can u just write how can I see the contents of points ?

  11. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Create a polygone

    The API is your friend.

    Java Platform SE 6 (that's a link, click it)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #12
    Junior Member
    Join Date
    Jan 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Create a polygone

    // Custom toString() Method.
    	public String toString() {
    		return "X=" + x + " " + "Y=" + y;
    	}

    ok, so I need to write my own version of toString method ? so that after in Main it could be overriden.

  13. #13
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Create a polygone

    Quote Originally Posted by r0x View Post
    // Custom toString() Method.
    	public String toString() {
    		return "X=" + x + " " + "Y=" + y;
    	}

    ok, so I need to write my own version of toString method ? so that after in Main it could be overriden.
    I suppose that's one way to do it. Or you could just use the getter functions in Point to print the values out yourself.

    For example, say I have a Rectangle r, and I want to print its width. I would just do this:

    System.out.println("Width: " + r.getWidth());
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  14. #14
    Junior Member
    Join Date
    Jan 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Create a polygone

    so the idea is that when I use System.out.println() this method needs toString to print the stuff ? and by default println takes Object's toString, am I right ?

  15. #15
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Create a polygone

    Quote Originally Posted by r0x View Post
    so the idea is that when I use System.out.println() this method needs toString to print the stuff ?
    Sort of. If you don't use a getter or some other function (like in my example), then println() is going to look at the toString() method to figure out what it should print.

    Quote Originally Posted by r0x View Post
    and by default println takes Object's toString, am I right ?
    Not necessarily. For example, many Objects (take Integer for example) override toString to provide a more meaningful String representation. If toString is not overridden by any of the parent classes, then the toString() inherited from Object is used.

    Check out the API to see which classes override toString().
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  16. #16
    Junior Member
    Join Date
    Jan 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Create a polygone

    Thank you very much for explications!! This forum is cool

  17. #17
    Junior Member
    Join Date
    Jan 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Create a polygone

    Have another question, I put the code straight away to be more clear
    ArrayList<Point><Line> points = new ArrayList<Point><Line>();

    Is it possible to put in the same ArrayList objects of different type ? In this case Point and Line.

  18. #18
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Create a polygone

    [edit] oops, didn't read to the end

    No, you can't do something like that. You could base type Point and Line and have ArrayList contain that base type.

    // I didn't define the classes, but you obviously would need to
    public class Geometry
    {}
     
    public class Point extends Geometry
    {}
     
    public class Line extends Geometry
    {}

    Then something like this would work:
    ArrayList<Geometry> geometries = new ArrayList<Geometry>();
    geometries.add(new Line(/*with the correct constructor params*/));
    geometries.add(new Point(/*with the correct constructor params*/));

    Additionally, all objects are base-typed as Object, so in theory you could create an ArrayList of Objects and it can hold anything, however this practice is generally frowned upon.
    Last edited by helloworld922; January 6th, 2011 at 06:37 PM.

  19. #19
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Create a polygone

    And to reiterate what helloworld922 said, you could also do something like this:

    ArrayList<Number> numbers = new ArrayList<Number>();
    Integer i = new Integer(1);
    Double d = new Double(3.14);
    numbers.add(i);
    numbers.add(d);

    You can do this because both Integer and Double extend Number.

    So since everything extends Object, you could have an ArrayList of Objects (or just don't user generics). But again, that's generally not the way to go.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  20. #20
    Junior Member
    Join Date
    Jan 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Create a polygone

    big thanks for answers!!! so the polygone was created let's say in my way but there is still teacher's method (because he wanted to start off with the 2D array of endpoints), for now I don't understand what this line means, ok basically it's a 2D array of type float but what happens inside of curly braces it's a mystery for me. Also [][2] it means we have 2 columns and infinite rows ??

    float endpoints[][2] = { {2.7, 5.4}, {1.1, 2.8}, {1.9, 1.3} };

  21. #21
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Create a polygone

    Arrays in Java are always fixed size. You can create a new array to replace the old one, but you must fix the size of the array.

    Actually that notation of [][2] isn't correct Java syntax. Instead, you must do one of the following:

    Remove the 2 and let Java figure out the size the array must be.
    float endpoints[][] = { {2.7, 5.4}, {1.1, 2.8}, {1.9, 1.3} }; // Java will create a 3*2 array of floats
    float endpoints[][] = new float[][] { {2.7, 5.4}, {1.1, 2.8}, {1.9, 1.3} }; // this does the exact same thing

    Declare the size of all the dimensions.
    float endpoints[][] = new float[3][2]; // you can put values into endpoints either 1 by 1 or row by row

    If you want an "infinite" number of rows, you can either create an array of very large size (generally not a good idea) or use ArrayLists (see: http://www.javaprogrammingforums.com...t-example.html). Note that ultimately you are going to be limited by the amount of memory your system has (more specifically the maximum JVM heap size).

    With Java arrays there is no way to enforce that the columns must be width 2 (at least without a wrapper class), however if you're careful you can enforce this de-facto.

Similar Threads

  1. How I can create those shapes with Java?
    By Learner in forum AWT / Java Swing
    Replies: 3
    Last Post: November 18th, 2010, 02:10 AM
  2. How do i create Sub Form
    By hadhebola in forum Object Oriented Programming
    Replies: 1
    Last Post: July 16th, 2010, 08:21 AM
  3. create a file
    By mos33 in forum Java Theory & Questions
    Replies: 4
    Last Post: December 4th, 2009, 02:21 PM
  4. How to create exe file
    By sirimalla in forum Java Theory & Questions
    Replies: 6
    Last Post: November 1st, 2009, 04:07 AM
  5. how to create exe jar
    By ttsdinesh in forum Java Theory & Questions
    Replies: 1
    Last Post: September 27th, 2009, 08:21 AM