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 2.3. 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.
(30 points) Recall the replaceAB and replaceABTR methods from the last lab. Design the String replaceABLoop(String s) method that solves the same problem as those methods, but uses a loop.
Hint: use the translation pipeline from your tail recursive implementation!
Files to Submit:
Problem1.java, Problem1Test.java
(40 points) Design the int chickenCounterLoop(String s) method that counts the number of times the word "chicken" appears in a given non-null string s. You must account for overlapping occurrences of "chicken" that are created when removing another occurrence. For example, invoking countChickenLoop("abcchickechickenn") returns 2 because, after removing the substring "chicken" from the original string, we are left with "abcchicken", which itself contains another instance of "chicken".
Hint: it may be a very wise idea to first write this as a tail recursive algorithm, then mechanically translate it into a loop! Break it up into a base case and the recursive step: if the string doesn't contain "chicken", what do you do? On the other hand, if it does, what do you do?
Files to Submit:
Problem2.java, Problem2Test.java
This problem has two parts.
An impatient number is an integer \(n \geq 0\) where the sum of its digits each raised to the power of the number of digits in \(n\) is equal to \(n\). For example, 1634 is an impatient number because \(1^4+6^4+3^4+4^4 = 1634\).
(10 points) Design the boolean isImpatientNumberLoop(int n) method that returns whether a given integer \(n \geq 0\) is an impatient number using a loop.
Warning: you cannot convert the integer to a string or use any string methods.
Hint 1: use modulus and division to extract digits!
Hint 2: use Math.pow.
(20 points) Design the String findImpatientsLoop(int a, int b) method that, when given two integers \(1 \leq a < b\), returns a comma-separated string of all the impatient numbers in the range \([a, b]\), inclusive, using a loop.
If there are no impatient numbers in the range, return the empty string.
For example, findImpatientsLoop(10, 500) returns "153,370,371,407".
Files to Submit:
Problem3.java, Problem3Test.java