1. Member
Join Date
Jan 2015
Posts
48
Rep Power
0

## The answer is not correct..

Hello! The method public static boolean onEdge(int n, int numRows, int numCols) should give TRUE if n is on the edge of a rectangular grid (numRows X numCols) and false if n is in interior.
Why (5, 3, 3) gives me TRUE whens it should be FALSE ? Or does it require using arrays?

Java Code:
```public static boolean onEdge(int n, int numRows, int numCols)
{
int originalRow = 0;

if ((n < 0) || (numRows < 0) || (numCols < 0))
return false;

else if (n == originalRow)
return true;

else if (n == numRows)
return true;

else
return false;

}```
Last edited by ssk2659; 01-24-2015 at 10:22 PM.

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
15

## Re: The answer is not correct..

Don't just show the method, show a self contained example of it doing what you say it is doing.

Regards,
Jim

3. Member
Join Date
Jan 2015
Posts
48
Rep Power
0

## Re: The answer is not correct..

After reviewing my code, I fount out it's completely wrong. What I have to only do is to complete a method for this exercise. public static boolean onEdge(int n, int numRows, int numCols). numRows and numCols represent a rectangular grid. The method should return true if n (a cell) is on the edge, i.e. in the first and last rows; false for the interior cells. Every cell starts from 0 from left top. I'm having a trouble with figuring out the right algorithm for this method. How can I solve this w/o arrays?

4. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
15

## Re: The answer is not correct..

The problem is that n is a single number. A cell has width and height. So you might want to use a Rectangle class. See java.awt.Rectangle.

Regards,
Jim

5. ## Re: The answer is not correct..

The question should be: is the given point (x,y) inside of the rectangle. A point can't be represented by a single value: n

One way to find a solution is to draw the rectangle on graph paper, select some points and see what comparisons can be made to determine if a point is inside of the rectangle.

6. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
19

## Re: The answer is not correct..

Every cell starts from 0 from left top.
I'm assuming you are indexing the cells with the top/left being zeros and then numbering to the right and down the way letters and words are wrriten in English.

To figure out if the position indexed by i lies on the edge of an nxm grid I suggest you draw such a grid (say 5x4) and look at the indices of the cells on the edge. Do they have anything in common? Can what those index numbers have in common be expressed neatly with Java expressions? Remember you have / and % operators to work with, and you can join conditions with && and ||.

7. ## Re: The answer is not correct..

If a point lies on an edge of a rectangle, the point lies on the top row OR on the bottom row, OR on the left column OR the right column and no coordinate should be outside of the rectangle ...

kind regards,

Jos

p.s. that was a hint

8. Member
Join Date
Jan 2015
Posts
48
Rep Power
0

## Re: The answer is not correct..

I understand that, I'm just having tough time with converting that into code.

9. ## Re: The answer is not correct..

Can you do it manually? If so, what comparisons are you making?

10. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
19

## Re: The answer is not correct..

Did you try the 5x4 example I suggested? What were the index values of the cells on the edge?

Perhaps the combined intelligence of the forum can figure out what condition is true for just those index values.

11. Member
Join Date
Jan 2015
Posts
48
Rep Power
0

## Re: The answer is not correct..

I've made a sketch (5x4). The true value can be in the first row (row 0) and last row (probably int numRows); also the first and last columns return the true value. Whenever there is a negative value, it returns the false value. But let's say (15, 5, 4) is passed to the parameter, how can it be done in the code?
Last edited by ssk2659; 01-26-2015 at 03:28 AM.

12. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
15

## Re: The answer is not correct..

You have a width by height grid and a j by k cell. The cell is at location (x,y) inside the grid. Using that information and some simple math you should be able to tell if the left edge of the cell touches the left side of grid or the right edge of the cell touches the right edge of the grid. Similar logic can be used for the top and bottom detection. Interpret touches as to mean they both have a least one point in common.

I am assuming the cell and grid sides are always parallel. If the cell can rotate, then complications arise.

Regards,
Jim
Last edited by jim829; 01-26-2015 at 03:36 AM.