Page 1 of 2 12 LastLast
Results 1 to 20 of 23
Like Tree3Likes

Thread: Using methods within methods

  1. #1
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default Using methods within methods

    I have written two methods called "contains" and "overlaps". The method "contains" is to detemine whether a point (x, y coordinate) is within the surface area of a square object. The location of the square objects is determined by the location of the upper left corner of the square.

    The method "overlaps" is to determine whether two square objects overlap each other.

    I have written these as two separate methods. However I want to change the method "overlaps", so that it uses the method "contains" within it. I.e. using a method within a method. Thereby hopefully making the "overlaps" method a bit more clear and easy to read. Please help me how I could do that.

    Java Code:
    	/** Returns true of the point with the coordinates x,y, are within the square. */
    	public boolean contains(int x, int y){
    		int sx = location.getX(); //"location" refers to a square object
    		int sy = location.getY(); // getX() and getY() are to find it's coordinates
    // "side" is the side length of the square
    		if (x >= sx && x <= (sx + side) && y >= sy && y <= (sy + side)){
    			return true;
    		}
    		else {
    			return false;
    		}
    	}
    	
    	/** Returns true if this square and the square sq overlaps. */
    	
    	public boolean overlaps(Square sq) {
    		int x = getX();
    		int y = getY();
    		int sx = sq.getX();
    		int sy = sq.getY();
    		int ss = sq.getSide();
    		
    		if (x >= sx && x <= (sx + ss) && y >= sy && y <= (sy + ss) ||
    			x + side >= sx && x + side <= (sx + ss) && y >= sy && y <= (sy + ss) ||
    			x >= sx && x <= (sx + ss) && y + side >= sy && y + side <= (sy + ss) ||
    			x + side >= sx && x + side <= (sx + ss) && y + side >= sy && y + side <= (sy + ss)){
    		
    			return true;
    		}
    		
    		else if(sx >= x && sx <= (x + side) && sy >= y && sy <= (y + side) ||
    				sx + ss >= x && sx + ss <= (x + side) && sy >= y && sy <= (y + side) ||
    				sx >= x && sx <= (x + side) && sy + ss >= y && sy + ss <= (y + side) ||
    				sx + ss >= x && sx + ss <= (x + side) && sy + ss >= y && sy + ss <= (y + side)) {
    			return true;
    		}
    		
    		else {
    			return false;
    		}
    	}

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Using methods within methods

    Well, if you want to make it easier to read use a Rectangle class to hold your square constraints. Then use SwingUtilities.computeDifference to see if they overlap. And use Rectangle.contains to see if the point is within the square. Both of these methods should make your code easier to read. Check out the JDK API for both.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default Re: Using methods within methods

    I would definately do that normally. Unfortunately this is a homework task (I hope it's okay to ask questions about those) and I have to use the "square" object for it.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Using methods within methods

    Ah, didn't know that. And yes, you may ask questions about homework. And we will do pretty much anything to help you understand the issues you are having except provide code (except in rare cases when one demonstrates a general concept).

    Regards,
    Jim
    Last edited by jim829; 01-10-2014 at 08:33 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default Re: Using methods within methods

    Within your overlaps() method, you have the following condition being evaluated:

    Java Code:
            if (x >= sx && x <= (sx + ss) && y >= sy && y <= (sy + ss) ||
                x + side >= sx && x + side <= (sx + ss) && y >= sy && y <= (sy + ss) ||
                x >= sx && x <= (sx + ss) && y + side >= sy && y + side <= (sy + ss) ||
                x + side >= sx && x + side <= (sx + ss) && y + side >= sy && y + side <= (sy + ss)){
             
                return true;
            }
    The first condition being evaluated is:

    Java Code:
    x >= sx && x <= (sx + ss) && y >= sy && y <= (sy + ss) // ....remaining conditions
    Is that what you want to replace with a call to the contains() method? If so, simply replace that portion of the code with a method call to the contains method.

    Is that what you are trying to do?

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Using methods within methods

    It would also make things much clearer (and slightly more efficient) if you would define certain characteristics of your squares one time. For example, look how many times you calculate sy + ss. Why not define a single variable, meaningfully named, and use that? Same for other variables. You can also go one step further by assigning repeated boolean evaluations. If you use a < b && c < d more than once, then set it to a boolean and use that.

    Java Code:
    boolean someName = a < b && c < d;
    //Then later..
    if ((someName && foo) || (someName && bar)) {
      ...
    }
    This is a matter of personal preference but it helps me keep track of my comparisons.

    Regards,
    Jim
    sehudson likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default Re: Using methods within methods

    Quote Originally Posted by sehudson View Post
    Within your overlaps() method, you have the following condition being evaluated:

    Java Code:
            if (x >= sx && x <= (sx + ss) && y >= sy && y <= (sy + ss) ||
                x + side >= sx && x + side <= (sx + ss) && y >= sy && y <= (sy + ss) ||
                x >= sx && x <= (sx + ss) && y + side >= sy && y + side <= (sy + ss) ||
                x + side >= sx && x + side <= (sx + ss) && y + side >= sy && y + side <= (sy + ss)){
             
                return true;
            }
    The first condition being evaluated is:

    Java Code:
    x >= sx && x <= (sx + ss) && y >= sy && y <= (sy + ss) // ....remaining conditions
    Is that what you want to replace with a call to the contains() method? If so, simply replace that portion of the code with a method call to the contains method.

    Is that what you are trying to do?
    Yes that's exactly what I'm trying to do! I think I solved it halfly now:

    Java Code:
    	public boolean contains(int px, int py){
    		int x = getX();
    		int y = getY();
    		if (px >= x && px <= (x + side) && py >= y && py <= (y + side)){
    			return true;
    		}
    		else {
    			return false;
    		}
    	}
    	
    	
    	public boolean overlaps(Square sq) {
    		int x = getX();
    		int y = getY();
    		int sx = sq.getX();
    		int sy = sq.getY();
    		int ss = sq.getSide();
    		
    		if (contains(sx, sy) ||
    			contains(sx + ss,y) ||
    			contains(sx, sy + ss) ||
    			contains(sx + ss, sy + ss)){
    		
    			return true;
    		}
    		
    		else if(x >= sx && x <= (sx + ss) && y >= sy && y <= (sy + ss) ||
    				x + side >= sx && x + side <= (sx + ss) && y >= sy && y <= (sy + ss) ||
    				x >= sx && x <= (sx + ss) && y + side >= sy && y + side <= (sy + ss) ||
    				x + side >= sx && x + side <= (sx + ss) && y + side >= sy && y + side <= (sy + ss)) {
    			return true;
    		}
    		
    		else {
    			return false;
    		}
    	}
    }
    Observe that I first changed the contains() method to make it clearer (the "location" is now simply called "int x, int y"). Then I switched place between the if() and the else if() statements in the overlaps().

    I don't know how to do with the else if() though. Normally the contains() method calls whether the something is within the Square (which is the object in the constructor/attributes of the class), but how is it possible to use the method for that Square object when the method by definition checks whether it's within that same Square object? That would only make it checking whether the square object overlaps itself and that would of course always be always true. How can this be solved?
    Last edited by Stud1; 01-10-2014 at 09:07 PM.

  8. #8
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default Re: Using methods within methods

    Is all of this code contained within a class called Square?

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Using methods within methods

    I didn't mention this earlier, but you may want to rethink your logic on the overlap method. You make it overly complicated. Consider this: If the sum of their individual widths is greater or equal to the absolute difference of the minimum and maximum x of both squares, you have overlap in the x direction. If you haven't done so it is easy to see if you use a pencil and paper.

    Edit: Actually never mind. If it ain't broke, don't fix it. A momentary lapse in judgement on my part.

    Regards,
    Jim
    Last edited by jim829; 01-10-2014 at 10:30 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default Re: Using methods within methods

    Quote Originally Posted by sehudson View Post
    Is all of this code contained within a class called Square?
    Yeah exactly!

    Let me explain how I'm thinking on the overlaps() mehod.

    First, in the if() statement, we check whether any of the four corner points of the square Sq are conatained within "the default square" (I'm not sure if "default" is a correct name for it?) in the square class. Anyway so far the method works for every case, EXCEPT the case where the "default" square is completely surrounded by the square sq.

    That means we also have to do the same check again, but "the other way around", which is what I'm doing in the else if() statement. I.e. we also have to check whether the any of the four points of the "default" square are contained within the square sq. And this is where I'm getting confused. How is it possible to use a method with the "default object" (again please tell me if there is a better name for it) as parameter, when a method, by definition, is always performed by the default object itself?

    I mean if we perform the method on the "default" square object and also have that same object as method parameter then all we do is to check whether that square object overlaps itself, which will of course always give the result "true".

  11. #11
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default Re: Using methods within methods

    Quote Originally Posted by sehudson View Post
    Is all of this code contained within a class called Square?
    Yeah exactly!

    Let me explain how I'm thinking on the overlaps() mehod.

    First, in the if() statement, we check whether any of the four corner points of the square Sq are conatained within "the default square" (I'm not sure if "default" is a correct name for it?) in the square class. Anyway so far the method works for every case, EXCEPT the case where the default square is completely surrounded by the square sq.

    That means we also have to do the same check again, but "the other way around", which is what I'm doing in the else if() statement. I.e. we also have to check whether the any of the four points of "the square constructed in the square class" are contained within the square sq.

    Edit: actually I think I solved it now.

    Java Code:
    	public boolean overlaps(Square sq) {
    		int x = getX();
    		int y = getY();
    		int sx = sq.getX();
    		int sy = sq.getY();
    		int ss = sq.getSide();
    		
    		if (contains(sx, sy) ||
    			contains(sx + ss,y) ||
    			contains(sx, sy + ss) ||
    			contains(sx + ss, sy + ss)){
    		
    			return true;
    		}
    		
    		else if(sq.contains(x, y) ||
    				sq.contains(x + side,y) ||
    				sq.contains(x, y + side) ||
    				sq.contains(x + side, y + side)){
    			return true;
    		}
    		
    		else {
    			return false;
    		}
    	}
    }
    Now it suddenly seems so easy. The hard part is always finding out how to do it, after that it's suddenly easy.
    Last edited by Stud1; 01-11-2014 at 11:23 AM.

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Using methods within methods

    Think of a square as (x,y) the top left point and a side length s; another point (X,Y) lies inside that square if x <= X <= x+s and y <= Y <= y+s; this boolean expression is an excellent candidate for a simple 'contains( ... )' method; A square (X, Y),S lies completely inside another square if all of its four corner points are within that other circle.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default Re: Using methods within methods

    Actually the program doesn't work as intended. When I use the overlaps() method on squares (x,y,side)=(100,100,100) and (1,300,100) it says they overlap even though they obviously don't.

    Here's the code again:

    Java Code:
    /** Returns true of the point with the coordinates x,y are within the square. */
    	public boolean contains(int px, int py){
    		int x = getX();
    		int y = getY();
    		if (px >= x && px <= (x + side) && py >= y && py <= (y + side)){
    			return true;
    		}
    		else {
    			return false;
    		}
    	}
    	
    	/** Returns true if the square sq is within the square or if the square is within the square sq. */
    	public boolean overlaps(Square sq) {
    		int x = getX();
    		int y = getY();
    		int sx = sq.getX();
    		int sy = sq.getY();
    		int ss = sq.getSide();
    		
    		if (contains(sx, sy) ||
    			contains(sx + ss,y) ||
    			contains(sx, sy + ss) ||
    			contains(sx + ss, sy + ss)){
    		
    			return true;
    		}
    		
    		else if(sq.contains(x, y) ||
    				sq.contains(x + side,y) ||
    				sq.contains(x, y + side) ||
    				sq.contains(x + side, y + side)){
    			return true;
    		}
    		
    		else {
    			return false;
    		}
    	}
    }
    Can someone help me to see what I did wrong?

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Using methods within methods

    Determining overlap is relatively easy if you break it down. Imagine a left square ,s1, and a right square, s2. The left most point of the left square is s1.x. The right most point of the right square is s2.x + s2.width. Now subtract the smaller from the larger of those two values. If the sums of their widths is greater than or equal to that value, they overlap in the x axis. They need to overlap in both axes for partial overlap of one over the other. And from my definition, overlaps do not need to be complete (i.e. one square inside of another). This works for any rectangle using width and height appropriately. Of course for a square those are equal.

    Regards,
    Jim
    Last edited by jim829; 01-11-2014 at 03:37 PM. Reason: spelling
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  15. #15
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default Re: Using methods within methods

    Quote Originally Posted by jim829 View Post
    Determining overlap is relatively easy if you break it down. Imagine a left square ,s1, and a right square, s2. The left most point of the left square is s1.x. The right most point of the right square is s2.x + s2.width. Now subtract the smaller from the larger of those two values. If the sums of their widths is greater than or equal to that value, they overlap in the x axis. They need to overlap in both axes for partial overlap of one over the other. And from my definition, overlaps do not need to be complete (i.e. one square inside of another). This works for any rectangle using width and height appropriately. Of course for a square those are equal.
    Thanks, I already know this, and that's exactly the theory I have used to make the contains() method. Then I use the contains() method within the overlaps() method. Can you see the code I've posted? I wrote it all there how I made it. But still the program doesn't work as intended, so there must be some misstake in the code, but I can't find what it could be. I would like help with that.

  16. #16
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Using methods within methods

    Quote Originally Posted by Stud1 View Post
    Actually the program doesn't work as intended. When I use the overlaps() method on squares (x,y,side)=(100,100,100) and (1,300,100) it says they overlap even though they obviously don't.
    If one square is supposed to be entirely in another square, all of it corner points should be within the square so use the && operator, not the || operator.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Using methods within methods

    Check this out:

    Java Code:
    if (contains(sx, sy) ||  contains(sx + ss,y) ||
                contains(sx, sy + ss) ||
                contains(sx + ss, sy + ss)){
                return true;
            }
    The y should probably be sy. And you don't need the the other test. If one square contains the point of another, then they both must contain a point of each other (at least, I can't think of where that isn't true). And in your contains method, just return the value of the expression. No ifs are needed.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  18. #18
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Using methods within methods

    I think the OP is doing simple overlap, not full containment.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  19. #19
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default Re: Using methods within methods

    Quote Originally Posted by JosAH View Post
    If one square is supposed to be entirely in another square, all of it corner points should be within the square so use the && operator, not the || operator.
    The way overlaps should be defined for this program is that "if any corner point of a square is within the area surface of another square". When one square is completely within another square, that is just one special case of overlapping, but not the only possible case. Do you see how I mean?

  20. #20
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default Re: Using methods within methods

    Quote Originally Posted by jim829 View Post
    Check this out:

    Java Code:
    if (contains(sx, sy) ||  contains(sx + ss,y) ||
                contains(sx, sy + ss) ||
                contains(sx + ss, sy + ss)){
                return true;
            }
    The y should probably be sy. And you don't need the the other test. If one square contains the point of another, then they both must contain a point of each other (at least, I can't think of where that isn't true). And in your contains method, just return the value of the expression. No ifs are needed.

    Regards,
    Jim
    Thanks Jim, I missed that y, it should be sy.

    However both tests are needed. Just imagine a square with coordinates (x,y,side)=(100,100,100) and another one (150,150,10). The second square will be completely within the first square. So the second square will have it's corner points within the first (and give "true") but if we switch them, the first square will not have any corner points within the first and hence give "false" even though they overlap. See how I mean? :)

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: 07-13-2012, 12:04 AM
  2. Replies: 2
    Last Post: 07-13-2012, 12:04 AM
  3. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  4. Java Noob, trying to call methods from another methods
    By gabrielpr12 in forum New To Java
    Replies: 8
    Last Post: 11-17-2011, 10:07 PM
  5. Replies: 1
    Last Post: 10-23-2011, 01:37 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
  •