Lab 05 - Arrays

Instructions

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.

What You Cannot Use

You may not use anything beyond Chapter 3.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.

Notes and Version Changes:

Note 1: Remember that any methods that returns a one-dimensional array should be tested using assertArrayEquals and not assertEquals.

Problems

  1. (50 points) Design the int[] countEvensOddsZeroes(int[] A) method that counts the number of even numbers, odd numbers, and zeroes in the given array. The returned array should contain three elements: the count of even numbers, the count of odd numbers, and the count of zeroes, in that order.

    Files to Submit: Problem1.java, Problem1Test.java

  2. (50 points) Design the String[] repeatStrings(String[] S, int[] A) method that, when given an array of strings \(S\) and an array of integers \(A\), returns a new array of strings \(S'\) such that, for each index \(i\) of \(S'\), \(S'[i]\) contains \(A[i]\) concatenated copies of \(S[i]\).

    For example, consider \(S = \)["CANVAS", "Plain", "Row"] and \(A = \)[2, 1, 3]. The returned array of strings \(S' = \)["CANVASCANVAS", "Plain", "RowRowRow"].

    You may assume that \(A[i] \geq 0\) for any \(i \in [0, |A| - 1]\). Furthermore, you may assume that \(|S| = |A|\). (That is, the length of \(S\) is the same as the length of \(A\).)

    Files to Submit: Problem2.java, Problem2Test.java