# Using methods within methods

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 01-10-2014, 08:02 PM
Stud1
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.

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;
}
}

• 01-10-2014, 08:10 PM
jim829
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
• 01-10-2014, 08:15 PM
Stud1
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.
• 01-10-2014, 08:19 PM
jim829
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
• 01-10-2014, 08:20 PM
sehudson
Re: Using methods within methods
Within your overlaps() method, you have the following condition being evaluated:

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:

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?
• 01-10-2014, 08:49 PM
jim829
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.

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
• 01-10-2014, 09:04 PM
Stud1
Re: Using methods within methods
Quote:

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

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:

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:

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?
• 01-10-2014, 09:24 PM
sehudson
Re: Using methods within methods
Is all of this code contained within a class called Square?
• 01-10-2014, 10:13 PM
jim829
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
• 01-11-2014, 11:18 AM
Stud1
Re: Using methods within methods
Quote:

Originally Posted by sehudson
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".
• 01-11-2014, 11:21 AM
Stud1
Re: Using methods within methods
Quote:

Originally Posted by sehudson
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.

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. :)-:
• 01-11-2014, 12:58 PM
JosAH
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
• 01-11-2014, 03:26 PM
Stud1
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:

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?
• 01-11-2014, 03:34 PM
jim829
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
• 01-11-2014, 03:43 PM
Stud1
Re: Using methods within methods
Quote:

Originally Posted by jim829
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.
• 01-11-2014, 03:47 PM
JosAH
Re: Using methods within methods
Quote:

Originally Posted by Stud1
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
• 01-11-2014, 04:00 PM
jim829
Re: Using methods within methods
Check this out:

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
• 01-11-2014, 04:00 PM
jim829
Re: Using methods within methods
I think the OP is doing simple overlap, not full containment.

Regards,
Jim
• 01-11-2014, 04:03 PM
Stud1
Re: Using methods within methods
Quote:

Originally Posted by JosAH
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?
• 01-11-2014, 05:09 PM
Stud1
Re: Using methods within methods
Quote:

Originally Posted by jim829
Check this out:

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. :(handshake):

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? :)
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last