CSC300 / CSC402: Debugging

Contents [0/1]

Debugging Demo [1/1]

(Click here for one slide per page)


Debugging Demo [1/1]

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package algs11;
import stdlib.*;
import java.util.Arrays;
public class Playground {
  public static int divThreeMinusDivFive (double[] a) {
    //StdOut.format ("a=%s\n", Arrays.toString(a));
    int div3 = 0;
    int div5 = 0;
    for (int i = 0; i < a.length; i += 1) {
      if (a[i]%3 == 0) { div3 += 1; }
      else if (a[i]%5 == 0) { div3 += 1; }
      //StdOut.format ("  i=%d, div3=%d, div5=%d\n", i, div3, div5);
    }
    return div3 - div5;
  }
  public static void testDivThreeMinusDivFive (int expected, double[] list) {
    int actual = divThreeMinusDivFive (list);
    if (expected != actual) {
      StdOut.format ("Failed: Expecting [%d] Actual [%d] with argument %s\n", expected, actual, Arrays.toString (list));
    }
  }
  public static void main (String[] args) {
    testDivThreeMinusDivFive (2, new double[] { 3, 6 });
    testDivThreeMinusDivFive (-2, new double[] { 5, 10 });
    testDivThreeMinusDivFive (0, new double[] { 3, 5, 6, 10 });   
    testDivThreeMinusDivFive (1, new double[] { 3 });
    testDivThreeMinusDivFive (-1, new double[] { 5 });
    testDivThreeMinusDivFive (0, new double[] { 15 });
    StdOut.println ("Finished tests");
  }
}

Debugging techniques:

You can find a tutorial on debugging in IntelliJ IDEA on the course homepage.