Answers.Body: <p>An interesting bug occurred during a practice job interview when creating a method that tests whether a substring (the needle) is in a string (the haystack). You can play with <a href="https://repl.it/repls/CandidOccasionalRegister" rel="nofollow noreferrer">the code on repl.it</a> or see it here.</p>
  
  <pre><code>import java.util.Objects;
  
  class Main {
  
    public static void main(String[] args) {
      // Add more test cases.
      String[] needles = {"a", "a", "ab"};
      String[] haystacks = {"a", "b", "ba"};
      for (int i = 0; i &lt; needles.length; i++) {
        boolean expected = haystacks[i].contains(needles[i]);
        boolean actual = isSubstring(needles[i], haystacks[i]);
  
        System.out.println(String.format(
          "%s -- isSubstring(\"%s\", \"%s\") has expected value %b" +
          " and actual value %b", 
          expected == actual ? "PASS" : "FAIL",
          needles[i], haystacks[i],
          expected, actual));
      }
    }
  
    /**
     * Tests whether the given substring (the needle) is in the full string
     * (the haystack).
     *
     * @param needle the substring
     * @param haystack the full string
     * @return true if the substring is contained in the full string, false otherwise
     */
    public static boolean isSubstring(String needle, String haystack) {
      Objects.requireNonNull(needle);
      Objects.requireNonNull(haystack);
  
      if (needle.isEmpty()) {
        return true;
      }
  
      for (int i = 0; i &lt; haystack.length(); i++) {
        // Check if next haystack character matches first haystack character.
        if (haystack.charAt(i) == needle.charAt(0)) {
          // If so, see if the rest of the strings match.
          if (isSubstringHelper(haystack, needle, i + 1)) {
            return true;
          }
        }
        // If not, advance the haystack index and try again.
      }
      return false;
    }
  
    private static boolean isSubstringHelper(String haystack, String needle, int hi) {
      for (int ni = 1; //  The character with index 0 has already been tested.
           ni &lt; needle.length() &amp;&amp; hi &lt; haystack.length(); 
           ni++, hi++){
        if (haystack.charAt(hi) != needle.charAt(ni))
          return false; 
      }
      return true;
    }
  }
  </code></pre>
  
  <p>I would suggest removing all but the first pair of test cases before assigning to students.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-31T14:58:53.297Z
Answers.LastActivityDate: 2019-07-31T18:12:50.637Z
Answers.LastEditDate: 2019-07-31T18:12:50.637Z
Answers.UserDisplayName: Ellen Spertus
Answers.Body: <p>I found this nice Java example on <a href="http://www.horstmann.com/bigj/help/debugger/tutorial.html" rel="nofollow noreferrer">Horstmann's</a> web site. A complete example with a variety of exercises. Very well written, as are his books. </p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-30T04:54:25.857Z
Answers.Text: No, you shouldn't have answered this. [“There's an answer on this site over there” is not an answer](https://meta.stackexchange.com/questions/225370/your-answer-is-in-another-castle-when-is-an-answer-not-an-answer). The content of this site may make a good answer, but just stating its existence does not answer the question.
Answers.UserDisplayName: Gilles 'SO- stop being evil'
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-30T12:49:56.567Z
Answers.Text: Actually, a more complete answer would have been fine. But as @Gilles suggests, simple links are not highly valued here. If you can say more about the example and why it works and with whom, it will find better acceptance. But "Go look over there" isn't considered a "real" answer here. be as complete as you can be. Explore the site and see which answers get up-voted. They generally contain good explanations.  You can edit your answer, of course, and down-votes can then be withdrawn.
Answers.UserDisplayName: Buffy
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-30T16:53:20.363Z
Answers.Text: Your comment above with the link was very appreciated. Thank you.
Answers.UserDisplayName: Ellen Spertus
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-29T19:51:22.393Z
Answers.LastActivityDate: 2018-03-31T00:22:47.203Z
Answers.LastEditDate: 2018-03-31T00:22:47.203Z
Answers.UserDisplayName: Fritz Sieker
Answers.Body: <p>A few Java specific examples come to mind:</p>
  
  <ul>
  <li><p>Using <code>%</code> with a negative number - I recently came across this and had to remember how Java treats modulus v. remainder (cf. <a href="https://stackoverflow.com/questions/5385024/mod-in-java-produces-negative-numbers">this thread</a>)</p></li>
  <li><p>Listing switch statements without the proper <code>break;</code> at the end of each case - maybe something that can be seen right away, but stepping through each line would be good to visualize this process</p></li>
  <li><p>Integer division is also a classic - using the conventional Celcius-to-Fahrenheit (or vice versa) conversion program with the formula broken down over several lines would force them to see line by line what is happening when you evaluate <code>9/5</code> or <code>5/9</code></p></li>
  <li><p>Operations with increment/decrement operators in prefix and/or postfix position throughout a program - I like this one a lot because of how variables may change without that change always being seen</p></li>
  </ul>
  
  <p>I'll edit and add ideas as more occur to me.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-03T05:44:47.093Z
Answers.Text: The second one is, in my opinion, a very, very good one.
Answers.UserDisplayName: ItamarG3
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-02T21:57:19.243Z
Answers.LastActivityDate: 2017-06-02T21:57:19.243Z
Answers.UserDisplayName: Peter
Answers.Body: <p>I would try to focus in on the kinds of errors that beginner programmers get stuck on, to imitate as closely as possible the kind of situation that they would want a debugger for.  So, I might code this method, which contains exactly the sort of subtle mistake 101 students regularly make, right in front of them:</p>
  
  <pre><code>public static boolean compare (String a, String b){
      boolean same  = true;
      for(int k = 0; k &lt; a.length(); k++){
          if (a.charAt(k) != b.charAt(k))
              same = false;
      }
      return same;
  }
  </code></pre>
  
  <p>...If someone starts to mention some errors, pretend that you are simply too focused on coding to deal with them right now.  <em>Please don't interrupt!  We are creating very important code right now, and it takes concentration!</em></p>
  
  <p>I would then follow up by putting in some tests, one at a time.  Have a big reaction when the first two tests go well, appear to get a little flustered by the third test, and become quite upset when the fourth test crashes.</p>
  
  <pre><code>public static void main(String[] args) {
      System.out.println(compare("",""));
      System.out.println(compare("cat","dog"));
      System.out.println(compare("car","carport"));
      System.out.println(compare("fortress","for"));
  }
  </code></pre>
  
  <p>Then I would announce that, as a mistake has clearly been made, and as there is clearly <em>no one in the room even remotely smart enough to figure out what it is,</em> this is now a moment to go to the debugger.</p>
  
  <p>The theatrics are a bit of my own manner in the classroom, but also serve a purpose here.  I want to get around the handful of quicker students who will spot the error right away with a bit of a wink that lets them know that they are perfectly smart, but not to ruin this for everyone else.  I want to keep them quiet long enough to let us get through the debugging process together.  </p>
  
  <p>Afterwards, I would acknowledge that anyone who saw the error was perfectly clever, but that the debugger is important because they will, without any doubt, encounter bugs that they will have trouble locating.  The debugger is one important part of the toolset that they will need to right the ship when it starts to go askew in the future.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-03T02:20:10.120Z
Answers.LastActivityDate: 2017-06-03T03:41:30.217Z
Answers.LastEditDate: 2017-06-03T03:41:30.217Z
Answers.UserDisplayName: Ben I.
Body: <p>One skill I teach students in Java 1 is how to use a debugger, specifically:</p>
  
  <ul>
  <li>single-stepping through code</li>
  <li>using breakpoints</li>
  <li>viewing the values of variables</li>
  <li>going up and down the call stack</li>
  </ul>
  
  <p>I have been unable to come up with good programs for them to use when learning to use the debugger. Specifically, I would like to provide them with a program with a bug that they're unlikely to find just by looking at the code but can find with a debugger.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-03-30T15:22:23.273Z
Comments.Text: Real programmers don't use debuggers, or `pre: real-programmer. post: debugger not used.` We use contracts, it reduces bugs, and makes the remainder easy to find.
Comments.UserDisplayName: ctrl-alt-delor
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-04-01T01:12:09.530Z
Comments.Text: Contacts are a huge help . I wish I could track down the study circa 1980 that tracked progress of university students learning traditionally versus with debuggers first. The speed at which students really understood the course material was much better with debuggers than without
Comments.UserDisplayName: pojo-guy
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-04-01T20:16:50.003Z
Comments.Text: @pojo-guy I assume you accidentally left off the "r" in the first word of your response? Surely you're not suggesting it's who you know, not what you know. :-)
Comments.UserDisplayName: Ellen Spertus
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-04-02T00:32:28.653Z
Comments.Text: Lol yes, responding on a cell phone is always fun. At least automisspell didn't embarrass me this time.
Comments.UserDisplayName: pojo-guy
Tags: <java><labs><debugging>
Title: What are good sample programs for learning to use a debugger?
ThreadText: # What are good sample programs for learning to use a debugger?
  
  (Asked by: username_0 on 2017-06-02)
  
  One skill I teach students in Java 1 is how to use a debugger, specifically:
  
  * single-stepping through code
  * using breakpoints
  * viewing the values of variables
  * going up and down the call stack
  
  I have been unable to come up with good programs for them to use when learning to use the debugger. Specifically, I would like to provide them with a program with a bug that they're unlikely to find just by looking at the code but can find with a debugger.
  
  **Tags:** java, labs, debugging
  
  > **Comments:**
  > **username_1**: Real programmers don't use debuggers, or `pre: real-programmer. post: debugger not used.` We use contracts, it reduces bugs, and makes the remainder easy to find.  
  > **username_2**: Contacts are a huge help . I wish I could track down the study circa 1980 that tracked progress of university students learning traditionally versus with debuggers first. The speed at which students really understood the course material was much better with debuggers than without  
  > **username_0**: @username_2 I assume you accidentally left off the "r" in the first word of your response? Surely you're not suggesting it's who you know, not what you know. :-)  
  > **username_2**: Lol yes, responding on a cell phone is always fun. At least automisspell didn't embarrass me this time.  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_6 on 2017-06-02. Score: 3 | Accepted answer
  
  A few Java specific examples come to mind:
  
  * Using `%` with a negative number - I recently came across this and had to remember how Java treats modulus v. remainder (cf. [this thread](https://stackoverflow.com/questions/5385024/mod-in-java-produces-negative-numbers))
  * Listing switch statements without the proper `break;` at the end of each case - maybe something that can be seen right away, but stepping through each line would be good to visualize this process
  * Integer division is also a classic - using the conventional Celcius-to-Fahrenheit (or vice versa) conversion program with the formula broken down over several lines would force them to see line by line what is happening when you evaluate `9/5` or `5/9`
  * Operations with increment/decrement operators in prefix and/or postfix position throughout a program - I like this one a lot because of how variables may change without that change always being seen
  
  I'll edit and add ideas as more occur to me.
  
  > **Comments:**
  > **username_7**: The second one is, in my opinion, a very, very good one.  
  
  ---
  
  ### Answer by username_8 on 2017-06-03. Score: 2
  
  I would try to focus in on the kinds of errors that beginner programmers get stuck on, to imitate as closely as possible the kind of situation that they would want a debugger for. So, I might code this method, which contains exactly the sort of subtle mistake 101 students regularly make, right in front of them:
  
  ```
  public static boolean compare (String a, String b){
      boolean same  = true;
      for(int k = 0; k < a.length(); k++){
          if (a.charAt(k) != b.charAt(k))
              same = false;
      }
      return same;
  }
  
  ```
  
  ...If someone starts to mention some errors, pretend that you are simply too focused on coding to deal with them right now. *Please don't interrupt! We are creating very important code right now, and it takes concentration!*
  
  I would then follow up by putting in some tests, one at a time. Have a big reaction when the first two tests go well, appear to get a little flustered by the third test, and become quite upset when the fourth test crashes.
  
  ```
  public static void main(String[] args) {
      System.out.println(compare("",""));
      System.out.println(compare("cat","dog"));
      System.out.println(compare("car","carport"));
      System.out.println(compare("fortress","for"));
  }
  
  ```
  
  Then I would announce that, as a mistake has clearly been made, and as there is clearly *no one in the room even remotely smart enough to figure out what it is,* this is now a moment to go to the debugger.
  
  The theatrics are a bit of my own manner in the classroom, but also serve a purpose here. I want to get around the handful of quicker students who will spot the error right away with a bit of a wink that lets them know that they are perfectly smart, but not to ruin this for everyone else. I want to keep them quiet long enough to let us get through the debugging process together.
  
  Afterwards, I would acknowledge that anyone who saw the error was perfectly clever, but that the debugger is important because they will, without any doubt, encounter bugs that they will have trouble locating. The debugger is one important part of the toolset that they will need to right the ship when it starts to go askew in the future.
  
  
  ---
  
  ### Answer by username_0 on 2019-07-31. Score: 0
  
  An interesting bug occurred during a practice job interview when creating a method that tests whether a substring (the needle) is in a string (the haystack). You can play with [the code on repl.it](https://repl.it/repls/CandidOccasionalRegister) or see it here.
  
  ```
  import java.util.Objects;
  
  class Main {
  
    public static void main(String[] args) {
      // Add more test cases.
      String[] needles = {"a", "a", "ab"};
      String[] haystacks = {"a", "b", "ba"};
      for (int i = 0; i < needles.length; i++) {
        boolean expected = haystacks[i].contains(needles[i]);
        boolean actual = isSubstring(needles[i], haystacks[i]);
  
        System.out.println(String.format(
          "%s -- isSubstring(\"%s\", \"%s\") has expected value %b" +
          " and actual value %b", 
          expected == actual ? "PASS" : "FAIL",
          needles[i], haystacks[i],
          expected, actual));
      }
    }
  
    /**
     * Tests whether the given substring (the needle) is in the full string
     * (the haystack).
     *
     * @param needle the substring
     * @param haystack the full string
     * @return true if the substring is contained in the full string, false otherwise
     */
    public static boolean isSubstring(String needle, String haystack) {
      Objects.requireNonNull(needle);
      Objects.requireNonNull(haystack);
  
      if (needle.isEmpty()) {
        return true;
      }
  
      for (int i = 0; i < haystack.length(); i++) {
        // Check if next haystack character matches first haystack character.
        if (haystack.charAt(i) == needle.charAt(0)) {
          // If so, see if the rest of the strings match.
          if (isSubstringHelper(haystack, needle, i + 1)) {
            return true;
          }
        }
        // If not, advance the haystack index and try again.
      }
      return false;
    }
  
    private static boolean isSubstringHelper(String haystack, String needle, int hi) {
      for (int ni = 1; //  The character with index 0 has already been tested.
           ni < needle.length() && hi < haystack.length(); 
           ni++, hi++){
        if (haystack.charAt(hi) != needle.charAt(ni))
          return false; 
      }
      return true;
    }
  }
  
  ```
  
  I would suggest removing all but the first pair of test cases before assigning to students.
  
  
  ---
  
  ### Answer by username_3 on 2018-03-29. Score: -1
  
  I found this nice Java example on [Horstmann's](http://www.horstmann.com/bigj/help/debugger/tutorial.html) web site. A complete example with a variety of exercises. Very well written, as are his books.
  
  > **Comments:**
  > **username_4**: No, you shouldn't have answered this. [“There's an answer on this site over there” is not an answer](https://meta.stackexchange.com/questions/225370/your-answer-is-in-another-castle-when-is-an-answer-not-an-answer). The content of this site may make a good answer, but just stating its existence does not answer the question.  
  > **username_5**: Actually, a more complete answer would have been fine. But as @username_4 suggests, simple links are not highly valued here. If you can say more about the example and why it works and with whom, it will find better acceptance. But "Go look over there" isn't considered a "real" answer here. be as complete as you can be. Explore the site and see which answers get up-voted. They generally contain good explanations.  You can edit your answer, of course, and down-votes can then be withdrawn.  
  > **username_0**: Your comment above with the link was very appreciated. Thank you.  
  
  ---
  
  