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

Objects, Continued

class Dimensions(val width: Double, val height: Double) {
fun area(): Double {
return width * height
}
}
val room = Dimensions(8.8, 10.0)
println(room.area())

Next we continue our exploration of Kotlin objects. Objects combine state and behavior. Yesterday we showed how they can store data like variables. Now we’ll show how they can run algorithms like methods.

Debugging Challenge
Debugging Challenge

But let’s start with some debugging practice!

Object Methods
Object Methods

Yesterday we began experimenting with simple Kotlin objects. Consider an object that stores information about a room:

class Room(val width: Double, val height: Double, val name: String)

Our Room class allows us to model a Rooms height, width, and name. Let’s create a few instances!

class Room(val width: Double, val height: Double, val name: String)
val livingRoom = Room(10.0, 8.0, "Living Room")
val kitchen = Room(6.0, 12.0, "Kitchen")
println("The ${kitchen.name} is ${kitchen.width} by ${kitchen.height}")
println("The ${livingRoom.name} is ${livingRoom.width} by ${livingRoom.height}")

Cool! But we said that Kotlin objects combine state and behavior. Where’s the behavior?

To start, let’s see if we can have each room print out the String that we printed manually in the previous example. We’ll go through how to do that together.

class Room(val width: Double, val height: Double, val name: String)
val livingRoom = Room(10.0, 8.0, "Living Room")

Practice: Guessing Game

Created By: Geoffrey Challen
/ Version: 2021.9.0

Let's play a guessing game! Complete a method named getSecretValue which is passed an instance of a Secret?. The Secret class provides a single method guess which accepts an Int parameter. It returns true if you have guessed the secret value, and false otherwise. You can either require that the passed Secret is not null or mark the parameter as non-nullable.

Write code to determine the secret value between 0 and 31, inclusive. If the secret does not fall in that range, you should return -1.

However, note that the Secret class will fail if you guess again after you have already guessed the secret value! So as soon as you find the secret, your code should return it and not guess again. Additional guesses will cause your submission to be marked as incorrect.

Instance Methods
Instance Methods

What we’ve created above is called an instance method. In some ways it’s just like the other methods that we’ve written. But, because it is part of a class definition, it is also different.

Specifically, instance methods have access to the values of instance variables or properties. We saw that in the walkthrough above, since our print function could access that room’s width, height, and name. Let’s continue exploring this together, and look at how instance methods can both access instance variables and accept parameters.

class Room(val width: Double, val height: Double, val name: String)

Modifying Instance Variables
Modifying Instance Variables

Instance methods can both access and modify instance variables. Let’s look at example of how that works.

class Room(val width: Double, val height: Double, val name: String)

Practice: Map Has Duplicate Values

Created By: Geoffrey Challen
/ Version: 2021.9.0

Write a method hasDuplicateValues that, given a Map<String, String>, returns true if the map contains duplicate values—meaning that two different keys map to the same value—and false otherwise. Recall that a map can never contain duplicate keys, since the second mapping from the same key overwrites the first.

You should use a Set to solve this problem! And as usual, maps, sets, and lists are all built-in to Kotlin and available for you to use to solve this problem.

Homework: Map Find Missing Keys

Created By: Geoffrey Challen
/ Version: 2022.9.0

Declare and complete a method named findMissingKeys, which accepts a map from String to Int as its first argument and a set of Strings as its second. Return a set of Strings containing all the Strings in the passed set that do not appear as keys in the map.

For example, given the set containing the values "one" and "two" and the map {"three": 3, "two": 4}, you would return a set containing only "one".

You should not need to create a new map to solve this problem.

CS People: Marques Brownlee
CS People: Marques Brownlee

If you’ve ever found yourself searching for technology product reviews on YouTube, you may have already run across the work of Marques Brownlee. A YouTube technology reviewer with 16 million subscribers, Marques is also a high-level professional Ultimate frisbee player. In the video below he discusses some of the challenges with being a diverse individual in technology with another fairly well-known Black man, Barack Obama:

More Practice

Need more practice? Head over to the practice page.