| 
001002
 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;
//    }
//} |