So, recursion, one of the hardest units in APCS. The concept is pretty easy but the execution is a bit trickier. Today, I’m going to pass on a trick that my teacher taught me on how to trace your way through a recursion problem.

I’m going to assume that y’all already know what recursion is and how it works so I’ll jump straight into tracing. Let’s start off with a basic problem.

First, you have to start from the bottom of the page and go up from there. As you go through every iteration of the recursive statement, you make a new line. So, this method with an x = 5 and y = 2, if solved, would look like this:

The bottommost line contains the original call to the method with 5 and 2 being x and y, respectively. The right of the diagram shows the operations within the recursive call and all the way at the top, the 17 represents what is returned by the base case when x becomes 0.

Let’s look at a recursive method that also prints something out in each iteration of the recursive statement.

For these problems, you would have to see where the print statement is in relation to the recursive statement. If the print statement is before the recursive statement, then the order of the printed lines go up and vice versa. So the tracing would look like this if n = 3:

So the output would look like:

3

2

1

Blastoff!

If the println statement was after the recursive statement, you would have to write them next to the after arrow and Blastoff! would be printed first.

So, these are the basics. If you guys have any specific questions for specific problems, feel free to contact me and I’ll help however I can.

Alright, it’s time to tackle 2D arrays and arrayLists. 2D arrays are basically arrays of arrays. First off, 2D arrays:

2D Arrays

To start off with, the declaration and instantiation. Since 2D arrays are objects, we will use the new operator. There are several ways to do this:

The “normal” way:

data type [] [] name = new data type [# of rows] [# of collumns];

Example: I want to create a 2D array called nums containing int data with 2 rows and 3 collumns:

int[] [] nums = new int[2][3];

If you want to create a jagged array where each row is of a different length, then you can input your values directly into the 2D array. There are several ways to do this but I’m going to show you guys the simplest one:

int[] [] nums =

{

{4, 5, 6, 4},

{7, 234, 34, 1},

{23, 57, 2}

};

(Of course, you can write this all on one line but this layout looks more intuitive.)

So, this is what the 2D array would look like visually:

If this were a rectangular 2D array and not a jagged array, any attempt to retrieve a data value from the index position (3, 2) would return null so keep that in mind if you are manipulating or accessing a 2D array. There is also a thing called sparseArrays and an FRQ concerning sparseArrays has popped up on the AP exams (although I don’t think it’s likely to pop up again but hey, the more you know…) so here’s a link that kind of explains it but I don’t really think you need to know it for this class because it’s a little more high-level.

Traversing Through a 2D Array

With 2D arrays, you also need to know how to traverse through them. You can do this through the basic three types of loops. I’m going to show an example of each.

For Loops

You can use both the for loop and a for each loop. In my examples, I’m going to use the loops to add up all the numbers in the 2D array. I’m going to use the nums array that I instantiated above (all of the loops work with regular 2D arrays as well as jagged 2D arrays). This is how to use a regular nested for loop to go through every element in a 2D array and add them up.

int sum = 0;

for (int row = 0; row < nums.length; row++)

for (int col = 0; col < nums[row].length; col++)

sum += nums[row][col];

For Each Loop

For each loops are a little less intuitive to use but in the first part of the parameters denote which type of data type you’re trying to handle within the array and the second part is the array that you want to traverse through.

int sum = 0;

for (int[] num : nums) //this looks at each row of thes 2D array

for (int i : num) //this looks at all the data values within each row

sum+= num;

Both of these loops do the same thing.

While Loops

As a rule of thumb, while loops are used when you don’t know how many times iterations you need the loop to go through. I’m just going to use the basic example I used above and write a while loop instead.

Heya! This will be the first of the AP Com Sci lab solutions on Outlet. I will be uploading these labs starting from Chapter 2 because Chapter 1 is a no-brainer. For that matter, Chapter 2 is a no-brainer too. I will upload parts of the code because even though these posts are supposed to contain the “answers”, you’re still going to have to know how to write these programs and know the concepts behind the programs to pass this class. I will not contribute to your failure. If you have any questions, you can comment them below or shoot me an email on the contact page.

For Chapter 2, I will only post about three labs, the Base Conversion Lab (pg 2-4), the Circle Labs (of which I’ll cover the Circle2 part on pg 5-7) and the String Manipulation Lab (pg 8-10) since the rest are the most fundamental basics and don’t have much to cover.