CSC300: Using an array of length 3 [9/21] Previous pageContentsNext page

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
package algs11;
import stdlib.*;
public class PlaygroundMax {
  public static double max (double[] a) {
    double m = a[0];
    if (m < a[1]) { m = a[1]; }
    if (m < a[2]) { m = a[2]; }
    return m;
  }
  public static void testMax (double expected, double[] a) {
    double actual = max (a);
    if (expected != actual) {
      StdOut.format ("max failed: Expecting [%d] Actual [%d] with argument %s\n", expected, actual, java.util.Arrays.toString(a));
    }
  }
  public static void main (String[] args) { 
    testMax(31, new double[] { 11, 21, 31 });
    testMax(31, new double[] { 11, 31, 21 });
    testMax(31, new double[] { 31, 11, 21 });
    StdOut.println ("Finished tests");
  }
}

Previous pageContentsNext page