Kotlinlearncs.online LogoJava

Practice with Functions

Let’s continue our exploration of functions. We’ll present a bit more Java syntax, and spend time reinforcing what we’ve learned about functions. Let’s get started!

Enhanced for Loop
Enhanced for Loop

To get us warmed up and ready to go, let’s check out a new bit of Java syntax! Remember how we started with this common while loop:

int[] values = {1, 2, 5};
int i = 0;
while (i < values.length) {
// Do something with each value, like print it
System.out.println(i);
i++;
}

and eventually arrived at this common for loop:

int[] values = {1, 2, 5};
for (int i = 0; i < values.length; i++) {
// Do something with each value, like print it
System.out.println(i);
}

Well, that for loop became so common that there’s an even simpler way to work through the values in array using Java’s enhanced for loop:

int[] values = {1, 2, 5};
for (int i = 0; i < values.length; i++) {
// Do something with each value
i++;
}

for (Indexed) versus Enhanced (Non-Indexed) for
for (Indexed) versus Enhanced (Non-Indexed) for

When you should use the indexed for (for (int i = 0;...) and when the enhanced for? Here are some things to consider:

For many common array-processing tasks that we’ve encountered, the enhanced for loop is a much better fit, since avoiding the extra index variable leads to a cleaner loop declaration and value access within the loop. For example, counting:

int[] values = {1, 2, 4};
// Indexed for
int count = 0;
for (int i = 0; i < values.length; i++) { // Longer loop declaration, extra variable i
if (values[i] > 1) { // must use bracket notation
count++;
}
}
System.out.println(count);
// Enhanced for
count = 0;
for (int value : values) { // Compact loop declaration
if (value > 1) { // No bracket notation!
count++;
}
}
System.out.println(count);

Practice: Array All Pairs

Created By: Geoffrey Challen
/ Version: 2022.1.0

Write a method arrayAllPairs that returns whether a passed int[] array is composed entirely of adjacent pairs of the same value. For example, the array {1, 1, 2, 2} and the array {4, 4, -1, -1} are composed of adjacent pairs of the same element, but {2, 1, 1, 2} and {4, 4, -1, 0} are not. To be composed entirely of pairs of the same element, the array must contain an even number of elements. If the passed array is empty, you should return false.

You will need to construct your loop carefully to complete this problem! We suggest that you examine the array looking for a counterexample: meaning a pair of adjacent elements that do not have the same value.

Practice with Functions
Practice with Functions

Next let’s get some more practice with functions! Together we’ll write a method that determines when an int array is a palindrome array: meaning that the values it contains are the same forward and backward. For example, {1, 2, 4} is not an array palindrome, but {1, 0, 2, 0, 1} is!

Let’s go step by step and see how to approach constructing this method. First, let’s determine our method signature, and practice calling it on some sample inputs.

Next, let’s begin work on the body of the method. Our array access pattern here is a bit different than what we’ve seen previously, so let’s proceed carefully.

As a next step, let’s complete the job by adding the decision-making logic we need to determine if the passed array is an array palindrome. This should remind us a bit of the search pattern that we covered last time.

Finally, let’s make one small improvement to our code.

Homework: Array Range Sum

Created By: Geoffrey Challen
/ Version: 2022.1.0

Write a method named arrayRangeSum. It receives an int[] and a positive int range as parameters. You should return the sum of all the elements in the array that are between range and range * -1, non-inclusive.

For example, given the array {1, -4, 2, 24, -124} and the range 8, you would return -1: 1 + -4 + 2, since these are the values in the array strictly greater than -8 and strictly less than 8. Given the range 124, you should return 23: 1 + -4 + 2 + 24, since these are the values in the array strictly greater than -124 and less than 124.

Note that this problem is a great fit for the enhanced for loop!

CS People: Ruchi Sanghvi
CS People: Ruchi Sanghvi

Early Facebook has a well-deserved reputation for being a male-dominated testosterone-driven workplace. So it may surprise you to find out that one of the software developers who was a core contributor to the first version of the News Feed—way back in 2006—was Ruchi Sanghvi.

She was also involved in Facebook’s response to the initial feedback on the News Feed. Users hated it! But Facebook kept it for one simple reason: Users were spending more time on the site. You should always keep this in mind when using Facebook, YouTube, TikTok, or any free site run by ad revenue. Their primary and sometimes only goal is for you to spend as much time as possible on their site. Regardless of whether that’s healthy or appropriate or useful to you.

Ruchi Sanghvi worked at several companies after leaving Facebook. In this video, she discusses some of what she learned along the way.

More Practice

Need more practice? Head over to the practice page.