CSC300 / CSC402: Homework (MyDebugging, MyMaxTrace)

Contents [0/1]

Homework (MyDebugging, MyMaxTrace) [1/1]

(Click here for one slide per page)


Homework (MyDebugging, MyMaxTrace) [1/1]

file:MyDebugging.java [source]
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package algs11;

//import java.util.Arrays;
//
//import stdlib.*;
//
///**
// * Part 1: MyDebugging (Version 2)
// *
// * This homework is intended to give you hands-on experience debugging and testing code. The intent is to give
// * you more practice with loops and develop your intuition about how Java works. Making sure you understand this
// * material thoroughly should set you up for success for the rest of the course.
// *
// * Remember, for the exam, you must be comfortable actually writing Java code on paper. To that end, I suggest that
// * if you're having trouble with Java and loops, practice. After you've fixed one or two of the methods below, write
// * it out on paper. Keep practicing until you can implement numFives on paper from scratch without looking at the code.
// * You can test out your on-paper coding ability by typing in an implementation from paper and running the tests to
// * see if they pass.
// *
// * Instructions:
// *
// * 1. Make sure you have watched last week's lecture. Read over the main method and the test methods to develop an understanding
// *    of what they are doing.
// *
// * 2. Look for the TODO in the code below.
// *
// * 3. Utilize the techniques presented (your choice of Inspection, print statements, trace on paper, Trace program,
// *    or IntelliJ Interactive debugger) to find and fix the bugs.
// */
//public class MyDebugging {
//
//    /* 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");
//    }
//
//    /* 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));
//        }
//    }
//
//    /**
//     * TODO: Change this to invoke each method, numFives1, numFives2, ... numFives10,
//     * in turn. You can comment out the first line and uncomment the second line.
//     *
//     * For each version of the method, tell me:
//     *
//     *   a. Whether there is a compiler error or a runtime bug, or both
//     *   b. All the things that are wrong with the method, and the changes you would need to make to get it working.
//     *
//     * Fix the method to make it compile if it does not, and you may as well fix the code.
//     * Some of the implementations may end up looking the same, or very similar.
//     *
//     * Write your answers on paper, or put them in a Word document. 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 static int numFives(double[] list) {
//        return numFives1(list);
//        // return numFives2(list);
//        // return numFives3(list);
//        // return numFives4(list);
//        // return numFives5(list);
//        // return numFives6(list);
//        // return numFives7(list);
//        // return numFives8(list);
//        // return numFives9(list);
//        // return numFives10(list);
//    }
//    /* Return number of times 5.0 occurs in the list */
//    public static int numFives1(double[] a) {
//        int result = 0;
//        for (int i = 0; i < a.length; i++)
//            if (a == 5.0)
//                result++;
//        return result;
//    }
//
//    /* Return number of times 5.0 occurs in the list */
//    public static int numFives2(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);
//    }
//
//    /* Return number of times 5.0 occurs in the list */
//    public static int numFives3(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;
//    }
//
//    /* Return number of times 5.0 occurs in the list */
//    public static int numFives4(double[] a) {
//        int result = 0;
//        for (int i = 0; i < a.length; i++)
//            if (a[i] == 5.0)
//                result++;
//            else
//                i++;
//        return result;
//    }
//
//    /* Return number of times 5.0 occurs in the list */
//    public static int numFives5(double[] a) {
//        int result = 0;
//        for (int i = 0; i < a.length; i++)
//            if (i == 5.0)
//                result++;
//        return result;
//    }
//
//    /* Return number of times 5.0 occurs in the list */
//    public static int numFives6(double[] a) {
//        int result = 0;
//        for (int i = 0; 0 < a.length; i++)
//            if (a[i] == 5.0)
//                result++;
//        return result;
//    }
//
//    /* Return number of times 5.0 occurs in the list */
//    public static int numFives7(double[] a) {
//        int result = 0;
//        for (int i = 0; i > 0; i++)
//            if (a[i] == 5.0)
//                result++;
//        return result;
//    }
//
//    /* Return number of times 5.0 occurs in the list */
//    public static int numFives8(double[] a) {
//        int result = 0;
//        int i = 0;
//        while (i < a.length) {
//            if (a[i] == 5.0) {
//                result++;
//                i++;
//            } else {
//                i++;
//            }
//        }
//        return result;
//    }
//
//    /* Return number of times 5.0 occurs in the list */
//    public static int numFives9(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;
//    }
//
//    /* Return number of times 5.0 occurs in the list */
//    public static int numFives10(double[] a) {
//        int result = 0;
//        for (int i = 0; i < a.length; i++)
//            if (a[i] == 5.0)
//                result++;
//        return result;
//    }
//}

file:MyMaxTrace.java [source]
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
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});
    }
}