Before continuing on, make sure you’re caught up on the lectures through 5/17. Also, make sure you’ve completed the Java Warm-up, or EX01
“Pulling” course materials down from Upstream. As new lesson material or starter code for exercises is added to our central course repository, these are the steps you’ll take to download them into your own workspace repository.
git pull origin main
. We are pulling code from the course source repo into our local repo.You should see in your exercises src
folder an ex02
package with a Recursion
class. This exercise involves writing three recursive methods. All the methods you write as part of this exercise will be static, or class, methods. This means that we do not need an instance of the Recursion
class to invoke them, and can think of them as “globally” available to us.
The first recursive method you will write will perform some recursive String
manipulation to upgrade every occurrence of "110"
in a String
to "210"
. It returns a String
and takes in a single String
. Some example usages below.
upgrade("lol110lol") --> "lol210lol"
upgrade("1110110") --> "1210210"
upgrade("10101010") --> "10101010"
upgrade("") --> ""
The second task is to write a method that recursively counts identical char
s in String
that are separated by another char
. We are looking for pairs of matching char
s within a String
. So, the String
"kaki"
would have one pair – the two k’s. They are separated by the 'a'
char
. The String
"aBaBa"
has 3 total pairs. 2 pairs between 'a'
char
s and one pair of 'B'
s. Some example usages:
pairCount("kaki") --> 1
pairCount("10101010") --> 6
pairCount("zomg!1!1!") --> 3
pairCount("") --> 0
Pairs can also overlap, so we would expect the following:
pairCount("eeeee") --> 3
The final recursive method involves recursively determining if any group of elements in array can be summed to some target, given some starting index. Some example usages:
sumToTarget(new int[]{8, 9, 5}, 0, 22) --> true
sumToTarget(new int[]{8, 9, 5}, 2, 22) --> false
sumToTarget(new int[]{8, 9, 5}, 0, 17) --> true
sumToTarget(new int[]{8, 9, 5}, 0, 18) --> false
sumToTarget(new int[]{8, 9, 5}, 0, 14) --> true
sumToTarget(new int[]{8, 9, 5}, 0, 13) --> true
sumToTarget(new int[]{8, 9, 5}, 1, 14) --> true
sumToTarget(new int[]{8, 9, 5}, 1, 13) --> false
sumToTarget(new int[]{}, 0, 0) --> true
All of these problems could be solved without recursion, however, any solution that is not recursive will not receive credit.
You will probably want to make use of some of the String
methods we’ve been looking at in class, like charAt
and substring
.
“Push” your work up to GitHub for backup. By creating “commits”, which you can think of as versioned checkpoints in your workspace, you are not at risk of losing your work. It’s easy to revert back to an old version or to restore your entire workspace on a different computer.
main
branch, and needs to be pushed to your remote backup.USERNAME
with your GitHub username:
https://github.com/comp210-ss1-24/summer210-workspace-USERNAME
All that’s left now is to hand-in your work on Gradescope for grading!
Before doing so, you need to know that before an assignment’s deadline you can resubmit work as many times as you need to without penalty. Portions of assignments are autograded and will provide near-immediate feedback. We want you to resubmit as many times as it takes you in order to earn full autograding credit!
Login to Gradescope and select the assignment named “EX02 - Recursion” You’ll see an area to upload a zip file. To produce a zip file for autograding, return back to IntelliJ.
Along the bottom of your window, you should see an option to open a terminal integrated into IntelliJ.
Type the following command (all on a single line):
./submit.sh ex02
In the file explorer pane, look to find the zip file named “ex02_submission.zip”. If you right click on this file “Open in -> Finder” on Mac, the zip file’s location on your computer will open. Upload this file to Gradescope to submit your work for this exercise.
We are working on rewriting the script to work for Windows! Until then, please navigate to your course workspace in a File Explorer window. Then right click on the src
folder in your exercises directory and compress the directory into a zip folder. You can name it “ex02_submission.zip”
When you upload it to Gradescope, please delete any files that showed up in the src/
folder that were not actually part of ex02
.
Autograding will take a few moments to complete. For this exercise there will be no “human graded” component, but in future exercises and projects there will. Thus, you should expect to score 100 out of 100 possible points on this assignment. If there are issues reported, you are encouraged to try and resolve them and resubmit. If for any reason you aren’t receiving full credit and aren’t sure what to try next, come give us a visit in office hours!