Results 1 to 12 of 12
Thread: The answer is not correct..
 01242015, 10:03 PM #1Member
 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; 01242015 at 10:22 PM.
 01242015, 10:27 PM #2Senior 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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 01242015, 10:42 PM #3Member
 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?
 01242015, 10:49 PM #4Senior 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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 01242015, 11:31 PM #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.If you don't understand my response, don't ignore it, ask a question.
 01252015, 01:08 AM #6Moderator
 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.
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 .
 01252015, 05:56 PM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 29
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 hintBuild a wall around Donald Trump; I'll pay for it.
 01252015, 10:37 PM #8Member
 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.
 01252015, 10:56 PM #9
Re: The answer is not correct..
Can you do it manually? If so, what comparisons are you making?
If you don't understand my response, don't ignore it, ask a question.
 01252015, 11:16 PM #10Moderator
 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.
 01262015, 03:18 AM #11Member
 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; 01262015 at 03:28 AM.
 01262015, 03:34 AM #12Senior 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,
JimLast edited by jim829; 01262015 at 03:36 AM.
The Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads

Prob 4 ProjectEuler. It does find the correct answer, but I also want the multiples.
By AcousticBruce in forum New To JavaReplies: 2Last Post: 08052012, 09:26 PM 
JOptionPane.showInputDialog with only 1 correct answer
By Gladiat0r in forum New To JavaReplies: 1Last Post: 03182012, 09:42 PM 
Compare true Answer /Player answer
By Eilime in forum New To JavaReplies: 1Last Post: 04242011, 08:51 PM 
why my coding cannot give the correct answer?
By qema in forum New To JavaReplies: 5Last Post: 03032009, 05:52 PM 
guss which is correct answer
By abhinav_jain09 in forum Java AppletsReplies: 4Last Post: 09192008, 02:39 AM
Bookmarks