Contents [0/12] |
Starter code [1/12] |
Does this work? [2/12] |
Does this work? [3/12] |
Does this work? [4/12] |
Does this work? [5/12] |
Does this work? [6/12] |
Does this work? [7/12] |
Does this work? [8/12] |
Does this work? [9/12] |
Does this work? [10/12] |
Does this work? [11/12] |
Some common errors and advice [12/12] |
(Click here for one slide per page)
Starter code [1/12] |
01 |
package algs11; import java.util.Arrays; import stdlib.*; public class Playground { /* Return number of times 5.0 occurs in the list */ public static int numFives (double[] list) { return StdRandom.uniform (100); //TODO: fix this } /* This is a test function */ public static void testNumFives (int expected, double[] list) { int actual = numFives (list); if (expected != actual) { StdOut.format ("Failed: Expecting [%d] Actual [%d] with argument %s\n", expected, actual, Arrays.toString (list)); } } /* A main function for testing */ public static void main (String[] args) { testNumFives (3, new double[] { 11, 21, 5, 31, 5, 41, 5, 51}); testNumFives (4, new double[] { 11, 21, 5, 31, 5, 5, 41, 5, 51}); testNumFives (4, new double[] { 11, 21, 5, 5, 5, 31, 41, 5, 51}); testNumFives (0, new double[] { 11, 21, 31, 41 }); testNumFives (1, new double[] { 11, 21, 5, 31, 41 }); testNumFives (1, new double[] { 11, 21, 31, 41, 5 }); testNumFives (1, new double[] { 5, 11, 21, 31, 41 }); testNumFives (0, new double[] { 11 }); testNumFives (1, new double[] { 5 }); testNumFives (3, new double[] { 5, 5, 5 }); testNumFives (0, new double[] { }); StdOut.println ("Finished tests"); } } |
Does this work? [2/12] |
01 |
public static int numFives (double[] a) { int result = 0; for (int i=0; i<a.length; i++) if (a[i] == 5.0) result++; return result; } |
Does this work? [3/12] |
01 |
public static int numFives (double[] a) { double[] list = new double[] { 4, 5, 6, 5, 3 }; int result = 0; for (int i=0; i<list.length; i++) if (list[i] == 5.0) result++; return result; } |
Does this work? [4/12] |
01 |
public static int numFives (double[] a) { int result = 0; for (int i=0; i<a.length; i++) { if (a[i] == 5.0) result++; else return result; return result; } StdOut.print (result); } |
Does this work? [5/12] |
01 |
public static int numFives (double[] a) { int result = 0; for (int i=0; i<a.length; i++) if (a[i] == 5.0) result++; else i++; return result; } |
Does this work? [6/12] |
01 |
public static int numFives (double[] a) { int result = 0; for (int i=0; i<a.length; i++) if (i == 5.0) result++; return result; } |
Does this work? [7/12] |
01 |
public static int numFives (double[] a) { int result = 0; for (int i=0; i<a.length; i++) if (a == 5.0) result++; return result; } |
Does this work? [8/12] |
01 |
public static int numFives (double[] a) { int result = 0; for (int i=0; 0<a.length; i++) if (a[i] == 5.0) result++; return result; } |
Does this work? [9/12] |
01 |
public static int numFives (double[] a) { int result = 0; for (int i=0; i>0; i++) if (a[i] == 5.0) result++; return result; } |
Does this work? [10/12] |
01 |
public static int numFives (double[] a) { int result = 0; int i = 0; while (i < a.length) { if (a[i] == 5.0) { result++; i++; } else { i++; } } return result; } |
Does this work? [11/12] |
01 |
public static int numFives (double[] a) { int result = 0; int i = 0; while (i < a.length) { if (a[i] == 5.0) { result++; i++; } if (a[i] != 5.0) i++; } return result; } |
Some common errors and advice [12/12] |
A common problem is using <
when you mean >
.
In the bound of a loop this can cause big trouble!
For example, consider:
01 |
LOOP1: int i = 1; while (i > a.length) { i += 1; } |
and
01 |
LOOP2: int i = 1; while (i < a.length) { i += 1; } |
If a.length
is 0
or 1
, then the LOOP1
runs forever!
Another common issue that comes up is incrementing in the wrong place.
01 |
LOOP3: int i = 1; while (i < a.length) { if (a[i] == 5.0) { result += 1; i += 1; } } |
If a
is length 2
or more, and a[1] != 5.0
, then this is an infinite loop!
Sometimes your indentation can get messed up if you are typing for a while... To fix that, use Source > Format
. That is, use the Format option on the Source menubar item.
A final common issue I will mention for now. Sometimes you may forget to put in the curly braces. This can be confusing!
01 |
LOOP4: int i = 1; while (i < a.length) if (a[i] == 5.0) { result += 1; } i += 1; |
LOOP4
looks okay, right?
But if you format, it will turn into this:
01 |
LOOP5: int i = 1; while (i < a.length) if (a[i] == 5.0) { result += 1; } i += 1; |
LOOP5
is the same as LOOP4
, but the indentation has changed. It makes it clear that i
is being incremented outside the loop. To fix this, you need to add curly braces:
01 |
LOOP6: int i = 1; while (i < a.length) { if (a[i] == 5.0) { result += 1; } i += 1; } |
Hurrah! finally a correct loop!
Revised: 2023-09-12 18:09