| 
0102
 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
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 
 | package algs11;
import stdlib.StdOut;
// Homework 2, Parts 2 and 3 (Version 2)
//
// Part 1. See MyDebugging.java.
//
// Part 2.
// The motivation for this homework is to help you develop the mental model for how Java
// functions and loops execute.
//
// TODO: Hand-trace the examples given in the main program. There are three of them.
// You only need to trace the execution of the max method, from the beginning to the end.
// Be sure to show how each variable changes. You do not need to show line numbers
// in your traces.
//
// Scan your traces and submit them. Or you can do it in ASCII or a Word document.
//
// Part 3.
// Re-write the max function on paper, or in a Word document, using a for loop.
//
// TODO: Hand in your new max method with a for loop.
//
// Both your traces and your new max method should be in the same file as Part 1.
// Upload a scan of your handwritten answers or your Word document to D2L.
//
// DO NOT TAKE A PICTURE WITH YOUR PHONE.
//
// You do not need to hand in this file!
//
public class MyMaxTrace {
    public static Double max(double[] a) {
        if (a.length == 0) {
            throw new IllegalArgumentException();
        }
        // DO NOT change this to "double" with a lower-case "d."
        Double m = a[0];
        int i = 1;
        while (i < a.length) {
            if (m < a[i]) {
                m = a[i];
            }
            i += 1;
        }
        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) {
        // Test 1
        testMax(31, new double[]{11, 21, 31});
        // Test 2
        testMax(31, new double[]{31, 11, 21});
        // Test 3
        testMax(81, new double[]{21, 31, 11, 71, 51, 81, 41});
    }
}
 |