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

  • map-reduce-filter : 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

  • Companion Objects : 02/23/2024

  • Encapsulation : 02/22/2024

  • Constructors : 02/21/2024

  • Objects, Continued : 02/20/2024

  • Introduction to Objects : 02/19/2024

  • Compilation and Immutability : 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

More About Functions

fun printIt(count: Int) {
println(count)
}
fun printIt(first: Int, second: Int) {
println(first + second)
}
printIt(10)
printIt(40, 50)

Our next lesson continues our exploration of functions. We’ll reinforce material covered in our last lesson, and go through some example of exactly what happens during function execution. We’ll also learn about overloading, when two methods share the same name. Let’s get started!

Tracing Function Execution
Tracing Function Execution

While we’ve started to use and even write functions, we may be still a bit fuzzy about exactly what happens when a function runs. So let’s go through that slowly and carefully using a few examples.

fun completelyEven(values: IntArray): Boolean {
for (i in values.indices) {
if (values[i] % 2 != 0) {
return false
}
}
return true
}
println(completelyEven(intArrayOf(1, 9, 9)))
println(completelyEven(intArrayOf(2, 4, 8)))

Next, let’s think about what happens when one of our functions itself calls another function!

fun isEven(value: Int): Boolean {
// Finish me
}
fun completelyEven(values: IntArray): Boolean {
for (i in values.indices) {
// TODO: Use isEven function
if (values[i] % 2 != 0) {
return false
}
}
return true
}
println(completelyEven(intArrayOf(1, 9, 9)))
println(completelyEven(intArrayOf(2, 4, 8)))

Common Algorithm Patterns
Common Algorithm Patterns

Computer scientists and programmers love patterns. Particularly in code. Frequently, once you’ve learned how to do one thing with code, you can modify that skeleton to solve other similar problems.

Let’s take a concrete example. The following code determines the count of the number of elements in an array. Note that this is not a good way to do this—it’s better to just use array.size. But we can modify this basic template to solve other problems. Let’s see how.

fun arrayLength(values: IntArray): Int {
var len = 0
for (i in values.indices) {
len++
}
return len
}
println(arrayLength(intArrayOf(1, 9, 9)))

Searching arrays produces another common pattern. It’s different from the example above, because we don’t necessarily need to complete the loop. Again, let’s walk through the basic template and then consider how to expand it.

fun arrayFindFour(values: IntArray): Boolean {
for (i in values.indices) {
if (values[i] == 4) {
return true
}
}
return false
}

Through the rest of our lessons we’ll continue to point out patterns that emerge in our programming. And ways to modify the basic pattern to produce more interesting designs!

Method Overloading
Method Overloading

Perhaps you noticed something unsettling about the example that starts this lesson:

fun printIt(count: Int) {
println(count)
}
fun printIt(first: Int, second: Int) {
println(first + second)
}
printIt(10)
printIt(40, 50)

Note only do we have two functions with the same name—printIt—but they both work! Distinguish this from variables, where we can never use the same name twice in the same scope:

// This (still) doesn't work
var first = 0
var first = '8'

Having multiple methods with the same name is known as method overloading. We don’t use this feature often, but you may see it in other Kotlin code that you work with.

When two methods have the same name Kotlin must be able to distinguish them from each other when they are used. So, if two methods have the same name, something must be different about them. The walkthrough below describes how Kotlin can tell them apart.

fun printIt(count: Int) {
println(count)
}
fun printIt(first: Int, second: Int) {
println(first + second)
}
printIt(10)
printIt(40, 50)

Practice: Overloaded Multiply

Created By: Geoffrey Challen
/ Version: 2020.9.0

Let's practice with function overloading. Write two versions of a function called multiply. The first should take two Double arguments and return their product (as a Double). The second should take three Double arguments and return their product (as a Double).

Unit Return
Unit Return

If you have been watching carefully, you’ve already seen something new in this lesson: methods without a return type! When you omit the return type of your Kotlin method declaration, Kotlin declares it to return a special type Unit. We don’t use Unit explicitly: we just omit the return type. Like this:

// Look, no return type!
fun printCount(count: Int) {
println("The count is $count")
}
printCount(5)
printCount(8)

Functions that don’t return a value can still use return to complete their execution, but can’t return a value. The result of calling a Unit function can also not be used in an assignment or statement:

fun printCount(count: Int) {
println("The count is $count")
}
printCount(5)
printCount(8)

Practice: Array Is Doubled

Created By: Geoffrey Challen
/ Version: 2021.8.0

Write a method called arrayIsDoubled that takes two non-empty Int arrays (IntArray) and returns true if they are the same length and if every element of the second array is equal to the element of the first array in the same position, doubled.

So given {1, 2, 4} and {2, 4, 8}, you would return true, but given {1, 2, 4} and {2, 4} or {1, 2, 4} and {2, 4, 8, 10} you would return false.

We suggest you approach this problem by looking for a counterexample. First, examine the lengths of the two arrays. If they are different, you can return immediately! Otherwise, loop through each element of the array looking for one that is incorrect. As soon as you find one, you can return. And, once the loop concludes, you can also draw a conclusion.

Homework: Array Is Reversed

Created By: Geoffrey Challen
/ Version: 2022.8.0

Write a method called arrayIsReversed that takes two non-empty Int arrays and returns true if the second array contains the same values as the first, but in the opposite order.

So given {1, 2, 4} and {4, 2, 1}, you would return true, but given {1, 2, 4} and {4, 2} or {1, 2, 4} and {4, 2, 1, 0} you would return false.

We suggest you approach this problem by looking for a counterexample. First, examine the lengths of the two arrays. If they are different, you can return immediately! Otherwise, loop through each element of the array looking for one that is incorrect. As soon as you find one, you can return. And, once the loop concludes, you can also draw a conclusion.

More Practice

Need more practice? Head over to the practice page.