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.
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.
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.
For this problem we're expecting only a method.
Don't define a class, include modifiers like static or public, add import statements, or add code outside the method declaration.
This problem deadline has passed, but you can continue to practice. Experiment! You will not lose credit.
Homework Restricted to Current CS 124 Students
A publicly-accessible version of this content is available at learncs.online.
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.
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.
For this problem we're expecting only a method.
Don't define a class, include modifiers like static or public, add import statements, or add code outside the method declaration.
You may use the following packages for this problem without importing them: java.util.HashSet, java.util.Map, java.util.Set
This problem deadline has passed, but you can continue to practice. Experiment! You will not lose credit.
Homework Restricted to Current CS 124 Students
A publicly-accessible version of this content is available at learncs.online.
Homework: Map Find Missing Keys
Created By: CS 124 Staff
/ 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.
For this problem we're expecting only a method.
Don't define a class, include modifiers like static or public, add import statements, or add code outside the method declaration.
You may use the following packages for this problem without importing them: java.util.HashSet, java.util.Map, java.util.Set
This problem deadline has passed, but you can continue to practice. Experiment! You will not lose credit.
Homework Restricted to Current CS 124 Students
A publicly-accessible version of this content is available at learncs.online.
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: