For each problem, create a class named ProblemX,
where X is the problem number
(e.g., Problem1.java).
Write corresponding JUnit test classes named
ProblemXTest.
All methods (yes, including helpers) must include proper Javadoc comments describing the (1) purpose of the method, (2) parameters, and (3) return value.
Helper methods should be marked as private.
You do not need to write explicit tests for helper methods because they will be indirectly tested through the driver methods.
For problems containing multiple parts, write the methods inside the same class.
You may not use anything beyond Chapter 3.2.1. This includes but is not limited to:
Any violation results in a score of 0 on the lab.
Please contact a staff member if you are unsure as to whether you're allowed to use something.
Note 1: One way to test methods that return multi-dimensional arrays is to call your method, storing its (array) result in a variable, and call assertTrue on the result of Arrays.deepEquals on the variable and the expected array.
Note 2: Another way to test two-dimensional arrays in particular is to traverse over the expected and actual arrays and call assertArrayEquals on each pair of rows.
Note 3: Remember that methods returning ArrayList data structures should be tested using assertEquals.
Note 4: An easy way to create lists quickly is to use List.of. If you don't know how to use this, consult your lecture notes or the textbook or ask your neighbors/TAs!
(50 points) Design the int[][] crop(int[][] M, int r1, int c1, int r2, int c2) method that crops a given array of integers, starting from \((r_1, c_1)\) to \((r_2, c_2)\), inclusive on both ends.
Assume that \(M\) is a non-null two-dimensional array of integers, and \(r_1, c_1, r_2, c_2\) are valid indices in \(M\) such that \(0 \leq r_1 \leq r_2 < M\texttt{.length}\) and \(0 \leq c_1 \leq c_2 < M\texttt{[0].length}\).
For instance, if we invoke crop with the following array and indices \((1, 1)\) to \((2, 2)\):
Then the resulting array should be:
\[ \begin{bmatrix} 5 & 6 \\ 8 & 9 \end{bmatrix} \]
Files to Submit:
Problem1.java, Problem1Test.java
(50 points)
Design the List<String> assignGrades(List<Double> G, List<String> L, double[] C)
method that, when given a List<Double> of exam grades \(G\), a
List<String> of letter grades \(L\), and a cut-off score array \(C\),
assigns a letter grade to the given exam.
That is, the method should return a list where the \(i^\text{th}\) element corresponds to the
\(i^\text{th}\) element of \(G\).
You may assume that \(L.\texttt{length} = C.\texttt{length} + 1\), and that \(L[i]\) corresponds to \(C[i]\). Importantly, \(C[i]\) is the lowest score at which the letter \(L[i]\) is awarded. The fact that \(L.\texttt{length}\) is one greater than \(C.\texttt{length}\) implies that anything lower than the lowest cutoff is awarded the "lowest" grade. You may also assume that \(C\) is sorted in descending order. Finally, you may assume that all grades in \(G\) can be assigned to exactly one of the letters. Take the following test cases as motivation.
// Test 1:
List<Double> G1 = List.of(95.0);
List<String> L1 = List.of("A", "B", "C", "D", "F");
double[] C1 = new double[]{90, 80, 70, 60};
assignGrades(G1, L1, C1) => ["A"]
// Test 2:
List<Double> G2 = List.of(80.0);
List<String> L2 = List.of("A", "B", "C", "D", "F");
double[] C2 = new double[]{90, 80, 70, 60};
assignGrades(G2, L2, C2) => ["B"]
// Test 3:
List<Double> G3 = List.of(79.0, 85.5, 89.95, 90.14, 0.0, 50.0, 60.01);
List<String> L3 = List.of("A", "B", "C", "D", "F");
double[] C3 = new double[]{90, 80, 70, 60};
assignGrades(G3, L3, C3) => ["C", "B", "B", "A", "F", "F", "D"]
// Test 4:
List<Double> G4 = List.of(79.0, 85.5, 89.95, 90.14, 0.0, 50.0, 60.01);
List<String> L4 = List.of("A", "A-", "B+", "B", "B-", "C+", "C",
"C-", "D+", "D", "D-", "F");
double[] C4 = new double[]{93, 90, 87, 83, 80, 77, 73, 70, 67, 63, 60};
assignGrades(G4, L4, C4) => ["C+", "B", "B+", "A-", "F", "F", "D-"]
Warning 1: A lot of students find this problem unintuitive. When we say, “take the following test cases as motivation,” we mean that you should actually sit down, work through them, and understand what’s going on. Don’t just rush through it!
Warning 2: If you use only the test cases that we provide, you will receive a very low "Student Tests" score.
Files to Submit:
Problem2.java, Problem2Test.java