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 14 of 14

Thread: Problem with method

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with method

    public abstract class SimplePolygon {
     
      public abstract Vertex2D getVertex(int index) throws IllegalArgumentException;
      public abstract int getNumVertices();
     
      public double getArea() {
     
        int area = 0;
     
        for (int i = 0; i < getNumVertices(); i++) {
     
          area += (getVertex(i).getX() * getVertex(i + 1).getY() - getVertex(i + 1).getX() * getVertex(i).getY() );
        }
     
        return 0.5 * area;
      }
     
      public double getWidth() {
     
        double max = -Double.MAX_VALUE,
               min = Double.MAX_VALUE;
     
        for (int i = 0; i < getNumVertices(); i++) {
     
          max = Math.max(max, getVertex(i).getX() );
          min = Math.min(min, getVertex(i).getX() );
        }
     
        return max - min;
      }
     
      public double getHeight() {
     
        double max = -Double.MAX_VALUE,
               min = Double.MAX_VALUE;
     
        for (int i = 0; i < getNumVertices(); i++) {
     
          max = Math.max(max, getVertex(i).getY() );
          min = Math.min(min, getVertex(i).getY() );
        }
     
        return max - min;
      }
     
      public double getLength() {
     
        double length = 0;
     
        for (int i = 0; i < getNumVertices(); i++) {
     
          length += getVertex(i).distance(getVertex(i + 1));
        }
     
        return length;
      }
     
      public String toString() {
     
        StringBuilder result = new StringBuilder();
     
        for (int i = 0; i < getNumVertices(); i++) {
     
          result.append(" "+ getVertex(i).toString());
        }
     
        return "Polygon: vertices ="+ result;
      }
    }
     
    public class ArrayPolygon extends SimplePolygon {
     
      private Vertex2D[] vertices;
     
      public ArrayPolygon(Vertex2D[] vertices) {
     
        if (vertices == null) throw new IllegalArgumentException();
     
        for (int i = 0; i < vertices.length; i++) {
     
          if (vertices[i] == null) throw new NullPointerException();
        }
     
        this.vertices = new Vertex2D[vertices.length];
     
        System.arraycopy(vertices, 0, this.vertices, 0, vertices.length);
      }
     
      public Vertex2D getVertex(int index) throws IllegalArgumentException {
     
        if (index < 0) throw new IllegalArgumentException();
     
        return vertices[index % vertices.length];
      }
     
      public int getNumVertices() {
     
        return vertices.length;
      }
     
      public ArrayPolygon invert() {
     
        Vertex2D[] temp = new Vertex2D[vertices.length];
     
        for (int i = vertices.length - 1, y = 0; i >= 0; i--) {
     
          temp[++y] = vertices[i];
        }
     
        return new ArrayPolygon(temp);
      }
     
      public boolean compare(ArrayPolygon pol) {
     
        int i = 0;
     
        for (; i < vertices.length; i++) {
     
          if (vertices[0].equals(pol.getVertex(i) ) ) break;
        }
     
        System.out.println(i +"; "+ vertices.length);
        return true;
      }
    }
     
    public class Vertex2D {
     
      private double x,y;
     
      public Vertex2D(double newX, double newY) {
     
        x = newX;
        y = newY;
      }
     
      public double getX() {
     
        return x;
      }
     
      public double getY() {
     
        return y;
      }
     
      public String toString() {
     
        return "["+ x +", "+ y +"]";
      }
     
      public double distance (Vertex2D vert) {
     
        if(vert == null) {
     
          return -1.0;
        }
        else {
     
          return Math.sqrt( (vert.x - x) * (vert.x - x) 
                          + (vert.y - y) * (vert.y - y) );
        }
     
      }
     
    }
     
    public class Tester {
     
      public static void main(String[] args) {
     
        Vertex2D[] vert1 = {
            new Vertex2D(-100,-100),
            new Vertex2D( -40,  10),
            new Vertex2D(  50,  20),
            new Vertex2D(  10, -20),
            new Vertex2D(  60, -40)
        };
     
        Vertex2D[] vert2 = {
            new Vertex2D(  50,  20),
            new Vertex2D(  10, -20),
            new Vertex2D(  60, -40),
            new Vertex2D(-100,-100),
            new Vertex2D( -40,  10)
        };
     
        Vertex2D[] vert3 = {
            new Vertex2D( -40,  10),
            new Vertex2D(-100,-100),
            new Vertex2D(  60, -40),
            new Vertex2D(  10, -20),
            new Vertex2D(  50,  20)
        };
     
        Vertex2D[] vert4 = {
            new Vertex2D( -40,  10),
            new Vertex2D(-100,-100),
            new Vertex2D(  50,  20),
            new Vertex2D(  10, -20),
            new Vertex2D(  60, -40)
        };
     
        System.out.println(new ArrayPolygon(vert1).compare(new ArrayPolygon(vert1)));
        System.out.println("");
        System.out.println(new ArrayPolygon(vert1).compare(new ArrayPolygon(vert2)));
        System.out.println("");
        System.out.println(new ArrayPolygon(vert1).compare(new ArrayPolygon(vert3)));
        System.out.println("");
        System.out.println(new ArrayPolygon(vert1).compare(new ArrayPolygon(vert4)));
      }
    }

    I think when I run Tester.java it have to return something like 0; 5 \n true \n \n 3; 5 \n true \n \n 2; 5 \n true \n \n 2;5 \n true. But it is returning 0; 5 \n true \n \n 5; 5 \n true \n \n 5; 5 \n true \n \n 5;5 \n true. Whats wrong with my code? I am looking at it for a while and I can't come up to whats wrong. It's one file for each class.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,105
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Problem with method

    Can you explain what is wrong with the current output? What items are wrong?

    If the current output and the desired output were put on lines one above the other, it would be easier to see what the differences are.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with method

    basically I am comparing two instances of class Vertex 2D.

    int i = 0;
     
    for (; i < vertices.length; i++) {
     
      if (vertices[0].equals(pol.getVertex(i) ) ) break;
    }
     
    System.out.println(i +"; "+ vertices.length);

    so I take zero Vertex2D from vert1 and then I am searching for first match in vertex1, vertex2, vertex3 then vertex4 and that matches are 0 3 1 1 so I think output was to be 0 3 1 1 or 1 4 2 2 but real output is 0 5 5 5 and thats a problem and I don't know why it is not working. :-(
     Vertex2D[] vert1 = {
            new Vertex2D(-100,-100),
            new Vertex2D( -40,  10),
            new Vertex2D(  50,  20),
            new Vertex2D(  10, -20),
            new Vertex2D(  60, -40)
        };
     
        Vertex2D[] vert2 = {
            new Vertex2D(  50,  20),
            new Vertex2D(  10, -20),
            new Vertex2D(  60, -40),
            new Vertex2D(-100,-100),
            new Vertex2D( -40,  10)
        };
     
        Vertex2D[] vert3 = {
            new Vertex2D( -40,  10),
            new Vertex2D(-100,-100),
            new Vertex2D(  60, -40),
            new Vertex2D(  10, -20),
            new Vertex2D(  50,  20)
        };
     
        Vertex2D[] vert4 = {
            new Vertex2D( -40,  10),
            new Vertex2D(-100,-100),
            new Vertex2D(  50,  20),
            new Vertex2D(  10, -20),
            new Vertex2D(  60, -40)
        };

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,105
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Problem with method

    output is 0 5 5 5
    What do the values of those 4 digits mean? What is the 0 and each 5?

    Should the search loop determine that no match was found and print out a different message instead of always printing the same message even if not found?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with method

    it's value of int i but like I said output have to be 0 3 1 1 or 1 4 2 2 not 0 5 5 5 and there is no case when match is not found so I think it's not necessary to fix.

    Edit: so value of i have to be turn of for when match is found and then I can easily found in which array is match.
    Last edited by Slapy; November 8th, 2013 at 10:23 AM. Reason: more text

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,105
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Problem with method

    not necessary to fix.
    Are you sure that there is always a match found in the loop?

    What do the values of the 4 digits: 0 5 5 5 represent?


    When I compile and execute the code, I get this output which is different from what you posted:
    0; 5
    true

    5; 5
    true

    5; 5
    true

    5; 5
    true
    What is wrong with this output?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with method

    yes I am sure and like I said values of 4 digits: 0 5 5 5 have to be numbers of variable in array with match but it doesn't.

    --- Update ---

    It's just little bit different. First number have to be number of match on array second is lenght of array and true is just true. But like I said first numbers have to be 0 3 1 1 or 1 4 2 2 and not 0 5 5 5.

    Edit: In this post, second code block are arreys about which I am talking.
    Last edited by Slapy; November 8th, 2013 at 10:52 AM. Reason: Added link to arrays

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,105
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Problem with method

    Try some debugging. I debug by using the println() method to print out the values of variables as the code executes. Add some println() statements to the compare() method to see what values the code is looking at as it runs the loop.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with method

    Sorry for duplicate I don't know how to delete it.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,105
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Problem with method

    What prints out when you add the println() statements to the compare() method and execute the code?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with method

    Try to add this
    System.out.println(vertices[0] +"; "+ pol.getVertex(i));
    before
    if (vertices[0].equals(pol.getVertex(i) ) ) break;
    and you will see it have to break that shitty for but it doesn't I just don't get it why.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,105
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Problem with method

    Sorry, I don't understand what you are saying?

    What prints out when you execute the code?

    I still think the code should say if it does not find a match instead of printing the same type of message.
    Or does 5;5 mean that there was no match? Makes for confusion to not specifically say: NO MATCH
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with method

    So that if have to break for if there are same values as you can see in first one it's all ok but in another ones it just failed I don't know why.
    [-100.0, -100.0]; [-100.0, -100.0]
    0; 5
    true
     
    [-100.0, -100.0]; [50.0, 20.0]
    [-100.0, -100.0]; [10.0, -20.0]
    [-100.0, -100.0]; [60.0, -40.0]
    [-100.0, -100.0]; [-100.0, -100.0]
    [-100.0, -100.0]; [-40.0, 10.0]
    5; 5
    true
     
    [-100.0, -100.0]; [-40.0, 10.0]
    [-100.0, -100.0]; [-100.0, -100.0]
    [-100.0, -100.0]; [60.0, -40.0]
    [-100.0, -100.0]; [10.0, -20.0]
    [-100.0, -100.0]; [50.0, 20.0]
    5; 5
    true
     
    [-100.0, -100.0]; [-40.0, 10.0]
    [-100.0, -100.0]; [-100.0, -100.0]
    [-100.0, -100.0]; [50.0, 20.0]
    [-100.0, -100.0]; [10.0, -20.0]
    [-100.0, -100.0]; [60.0, -40.0]
    5; 5
    true

    and what I want is
    [-100.0, -100.0]; [-100.0, -100.0]
    0; 5
    true
     
    [-100.0, -100.0]; [50.0, 20.0]
    [-100.0, -100.0]; [10.0, -20.0]
    [-100.0, -100.0]; [60.0, -40.0]
    [-100.0, -100.0]; [-100.0, -100.0]
    3; 5
    true
     
    [-100.0, -100.0]; [-40.0, 10.0]
    [-100.0, -100.0]; [-100.0, -100.0]
    1; 5
    true
     
    [-100.0, -100.0]; [-40.0, 10.0]
    [-100.0, -100.0]; [-100.0, -100.0]
    1; 5
    true


    --- Update ---

    So problem was Vertex2D is my own class and I have to do my own equals for comparing this classes.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,105
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Problem with method

    I have to do my own equals for comparing this classes.
    Yes, you need your own method to compare the contents of your own class.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Method problem
    By skeptile2 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 7th, 2013, 02:16 AM
  2. Calculate method problem.....
    By DavidXCode in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2012, 09:04 PM
  3. Looping method problem.
    By Swen in forum What's Wrong With My Code?
    Replies: 24
    Last Post: December 29th, 2011, 08:49 AM
  4. Problem with a JFrame method
    By Lakmini Kuruppu in forum AWT / Java Swing
    Replies: 1
    Last Post: May 20th, 2010, 08:40 AM
  5. dynamic method problem...
    By Ranger-Man in forum Object Oriented Programming
    Replies: 8
    Last Post: September 7th, 2009, 04:22 PM