Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><em>(Figured I'd post an answer outlining the progress we made in our chat discussion)</em></p> <p>Assuming the following state of the <code>Coord</code> class:</p> <pre><code>public class Coord { public int x; public int y; public Coord(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Object obj){ if (obj == null) return false; if (obj.getClass() != Coord.class) return false; if (obj == this) return true; Coord a = (Coord)obj; return (a.x == this.x &amp;&amp; a.y == this.y); } @Override public int hashCode() { return x*17 + y*31; } @Override public String toString() { return "("+x+", "+y+")"; } } </code></pre> <p>... that implements:</p> <ul> <li><code>equals()</code> as this is what stack search uses, according to its Javadoc</li> <li><code>hashCode()</code> as a best practice, accompanying <code>equals()</code></li> <li><code>toString()</code> for clearer diagnostic output</li> </ul> <p>The idea is to test the functionality of stack search in isolation from the rest of the code. If we can prove that stack search functions properly, then the problem is in elsewhere.</p> <p>Proving that the stack search works can be done using a test class, such as this one:</p> <pre><code>public class CoordTest { public static void main(String[] args) { System.out.println("Running tests..."); System.out.println("Testing: equals"); Coord c1a = new Coord(2,3); Coord c1b = new Coord(2,3); check(c1a.equals(c1b)); System.out.println("Testing: not equals"); Coord c2a = new Coord(2,3); Coord c2b = new Coord(6,8); Coord c2c = new Coord(2,8); Coord c2d = new Coord(6,3); check(!c2a.equals(c2b)); check(!c2a.equals(c2c)); check(!c2a.equals(c2d)); System.out.println("Testing: not found in empty stack"); Stack&lt;Coord&gt; stack1 = new Stack&lt;Coord&gt;(); int result1 = stack1.search(c1a); check(result1 == -1); System.out.println("Testing: not found in non-empty stack"); Stack&lt;Coord&gt; stack2 = new Stack&lt;Coord&gt;(); stack2.push(new Coord(4,5)); stack2.push(new Coord(6,7)); int result2 = stack2.search(c1a); check(result2 == -1); System.out.println("Testing: found in non-empty stack"); Stack&lt;Coord&gt; stack3 = new Stack&lt;Coord&gt;(); stack3.push(new Coord(4,5)); stack3.push(new Coord(3,1)); stack3.push(new Coord(6,7)); int result3 = stack3.search(new Coord(3,1)); check(result3 == 2); System.out.println("All tests completed successfully."); } private static void check(boolean condition) { if (!condition) { throw new RuntimeException("Condition failed!"); } } } </code></pre> <p>Output:</p> <pre><code>Running tests... Testing: equals Testing: not equals Testing: not found in empty stack Testing: not found in non-empty stack Testing: found in non-empty stack All tests completed successfully. </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload