Results 1 to 12 of 12
Like Tree3Likes
  • 1 Post By pbrockway2
  • 1 Post By JosAH
  • 1 Post By jim829

Thread: The answer is not correct..

  1. #1
    ssk2659 is offline Member
    Join Date
    Jan 2015
    Posts
    48
    Rep Power
    0

    Default 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. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    ssk2659 is offline Member
    Join Date
    Jan 2015
    Posts
    48
    Rep Power
    0

    Default 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. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

    Default 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 ||.
    ssk2659 likes this.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default 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
    ssk2659 likes this.
    Build a wall around Donald Trump; I'll pay for it.

  8. #8
    ssk2659 is offline Member
    Join Date
    Jan 2015
    Posts
    48
    Rep Power
    0

    Default Re: The answer is not correct..

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

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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.

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

    Default 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. #11
    ssk2659 is offline Member
    Join Date
    Jan 2015
    Posts
    48
    Rep Power
    0

    Default 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. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default 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.
    ssk2659 likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2012, 09:26 PM
  2. JOptionPane.showInputDialog with only 1 correct answer
    By Gladiat0r in forum New To Java
    Replies: 1
    Last Post: 03-18-2012, 09:42 PM
  3. Compare true Answer /Player answer
    By Eilime in forum New To Java
    Replies: 1
    Last Post: 04-24-2011, 08:51 PM
  4. why my coding cannot give the correct answer?
    By qema in forum New To Java
    Replies: 5
    Last Post: 03-03-2009, 05:52 PM
  5. guss which is correct answer
    By abhinav_jain09 in forum Java Applets
    Replies: 4
    Last Post: 09-19-2008, 02:39 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •