KotlinCS 124 LogoJava
PrevIndexNext
Kotlin
Java
  • Implementing a Map : 04/26/2024

  • Streams : 04/25/2024

  • Generics : 04/24/2024

  • Hashing : 04/23/2024

  • Binary Search : 04/22/2024

  • MP3: Course Ratings : 04/19/2024

  • Quicksort : 04/18/2024

  • Merge Sort : 04/17/2024

  • Sorting Algorithms : 04/16/2024

  • MP Debugging Part 1 : 04/15/2024

  • MP2: Course Activity : 04/12/2024

  • Practice with Recursion : 04/11/2024

  • MP Debugging Part 0 : 04/10/2024

  • MP2: API Client : 04/09/2024

  • MP2: API Server : 04/08/2024

  • Trees and Recursion : 04/05/2024

  • Trees : 04/04/2024

  • Recursion : 04/03/2024

  • MP1: Filtering and Search : 04/02/2024

  • MP1: Loading and Sorting : 04/01/2024

  • Lists Review and Performance : 03/29/2024

  • Linked Lists : 03/28/2024

  • Algorithms and Lists : 03/27/2024

  • Continuing MP0 : 03/26/2024

  • Getting Started with MP0 : 03/25/2024

  • Lambda Expressions : 03/22/2024

  • Anonymous Classes : 03/21/2024

  • Practice with Interfaces : 03/20/2024

  • Implementing Interfaces : 03/19/2024

  • Using Interfaces : 03/18/2024

  • Working with Exceptions : 03/08/2024

  • Throwing Exceptions : 03/07/2024

  • Catching Exceptions : 03/06/2024

  • References and Polymorphism : 03/05/2024

  • References : 03/04/2024

  • Data Modeling 2 : 03/01/2024

  • Equality and Object Copying : 02/29/2024

  • Polymorphism : 02/28/2024

  • Inheritance : 02/27/2024

  • Data Modeling 1 : 02/26/2024

  • Static : 02/23/2024

  • Encapsulation : 02/22/2024

  • Constructors : 02/21/2024

  • Objects, Continued : 02/20/2024

  • Introduction to Objects : 02/19/2024

  • Compilation and Type Inference : 02/16/2024

  • Practice with Collections : 02/15/2024

  • Maps and Sets : 02/14/2024

  • Lists and Type Parameters : 02/13/2024

  • Imports and Libraries : 02/12/2024

  • Multidimensional Arrays : 02/09/2024

  • Practice with Strings : 02/08/2024

  • null : 02/07/2024

  • Algorithms and Strings : 02/06/2024

  • Strings : 02/05/2024

  • Functions and Algorithms : 02/02/2024

  • Practice with Functions : 02/01/2024

  • More About Functions : 01/31/2024

  • Errors and Debugging : 01/30/2024

  • Functions : 01/29/2024

  • Practice with Loops and Algorithms : 01/26/2024

  • Algorithms : 01/25/2024

  • Loops : 01/24/2024

  • Arrays : 01/23/2024

  • Compound Conditionals : 01/22/2024

  • Conditional Expressions and Statements : 01/19/2024

  • Operations on Variables : 01/18/2024

  • Variables and Types : 01/17/2024

  • Welcome to CS 124 : 01/16/2024

Practice with Loops and Algorithms

Next we’ll pause to reinforce what we’ve learned about the basic building blocks of computer science: storing data, making decisions, and repeating operations. We’ll also get some more practice with loops!

Debugging Practice
Debugging Practice

Let’s warm up with a bit of graded debugging practice.

continue
continue

Last time we discussed how we can exit a loop early using the break statement:

for (int i = 0; i < 10; i++) {
if (i > 5) {
break;
}
System.out.println(i);
}

break causes the enclosing loop—either while or for—to immediately exit, regardless of whether the loop condition is true or not.

continue is another control statement that we can use inside a loop. Let’s explore how it works together!

for (int i = 0; i < 8; i++) {
System.out.println(i);
}

So while break causes the loop to exit, continue causes the code to return to the top of the loop immediately, check the loop condition, and then continue if appropriate.

continue versus if
continue versus if

continue is used less frequently than break. One of the reasons is that, we can always rewrite any loop that uses continue to instead use an if statement. Let’s see how:

for (int i = 0; i < 8; i++) {
System.out.println(i);
}

What’s more clear: continue versus an if statement? It really depends on the problem. If the loop body is long and complicated, it can be better to use continue at the beginning to avoid values that shouldn’t be considered by the rest of the loop. However, overall continue can make your code hard to read. So for shorter loops, it’s usually better to just put the entire loop body inside an if statement instead.

Counting Array Values
Counting Array Values

To get some additional practice, let’s work through an example together that is similar to this lesson’s homework. It also provides us a chance to practice with a very common array and loop programming pattern.

Given an array of values, how would we count the number of values that have some property? For example, let’s say that we need to count the number of values which are greater than zero.

Solution Outline
Solution Outline

Let’s work step-by-step. First, let’s outline what we need to do.

int[] array = {-1, 42, 8, -12, 7};
// Goal: count the number of positive values in array

for Loop Setup
for Loop Setup

Next, let’s get our loop set up! It’s always a good idea to make sure that we can actually access all of the values in the array before doing anything more complicated.

int[] array = {-1, 42, 8, -12, 7};

Testing Values Using if
Testing Values Using if

As the third step, let’s review how we determine if a value is greater than zero, and integrate that into what we have so far.

int[] array = {-1, 42, 8, -12, 7};

Identifying a Design Pattern
Identifying a Design Pattern

Finally, let’s examine the design pattern that emerges from this example: a combination of iteration (our for loop) and selection (our if statement). By combining these two and using different conditions and data, we can solve a lot of different problems! We can also combine results in different ways as well.

int[] array = {-1, 42, 8, -12, 7};

Practice: Array Sum Even Snippet

Created By: Geoffrey Challen
/ Version: 2022.1.0

Given an int[] named array that is already declared and initialized, write a snippet of code (not a method), that prints a line with the sum of the values in array that are even.

Practice: Array Count Odd Snippet

Created By: Geoffrey Challen
/ Version: 2022.1.0

Given an int[] named array that is previously declared and initialized, write a snippet of code (not a method), that prints a line with the count of the number of values in array that are odd.

Even and Odd
Even and Odd

Before we wrap up, let’s review one useful bit of Java that we’ll use regularly during some of our upcoming examples: How to test whether a value is even or odd.

int even = 6;
int odd = 5;

Homework: Number Sum Game Snippet

Created By: Geoffrey Challen
/ Version: 2022.8.0

Your friend invented a new game and wants your help to create the computer implementation. Of course, you're happy to help!

Here's how the game works. A set of cards with positive values are placed face down on a table. The player is allowed to turn over some number, but not all. They then guess the sum of all the cards. At that point, all the values are revealed. The players score is equal to how far off their guess was—the distance between their guess and the actual sum. (Lower is better.)

As an example, if the values were 1, 2, and 4, and the guess was 7, the score would be 0—a perfect score! However, if the values were 1, 2, and 8, and the guess was 7, the score would be 4. And if the the values were 2, 2, and 5, and the guess was 124, the score would be 115.

Write a snippet of code (not a method) that computes the score of a game. The values on the cards will be stored in an existing int array named values, and the guess in an int variable named guess. You should declare an int variable score and set its value to the score of that game.

You will need to compute the sum of the values in the array! You may also find the Math.abs method helpful when completing this problem, but it is not required.

More Practice

Need more practice? Head over to the practice page.