Lab 1: Kotlin Scavenger Hunt
In this lab, you will explore the difference between Python and Kotlin. You will generate a reference sheet for yourself to use throughout the term as you write your own Kotlin programs.
Submission
In general, any labs we do will not have required submissions. However, they are designed to help you complete your assignments faster and more easily, so you are strongly encouraged to complete them!
Kotlin
Learning your second programming language is a different experience from learning your first – you already know the basics of programming, including the concepts of if statements and for loops, defining variables and writing functions, and printing output for the user to see. For your second language, it’s much more about specific syntax, rather than also learning the fundamental programming concepts.
You will likely experience that learning Kotlin is faster than learning your first language, but that doesn’t make it inherently easy. You may not find it “easy” to learn a new language until your fourth or fifth time. Still, this lab is designed to help ease your transition into Kotlin.
Getting started
We’ll be working with a set of files in both Python and Kotlin. To get started, follow these steps:
-
Download this .zip file and then double-click to unzip it. You’ll now have a folder named
Comparing-Python-and-Kotlin. -
Mount the COURSES drive (if you didn’t already when you logged in) and copy the
Comparing-Python-and-Kotlinfolder into yourSTUWORK/usernamefolder. When you log out of the computer, everything not in this folder gets deleted! -
Open VS Code.
-
Open the
Comparing-Python-and-Kotlinfolder in VS Code, either by clicking and dragging the folder icon or by going toFile->Open Folder.
In-Class Exercises
Exercise 1
The files in Comparing-Python-and-Kotlin contain programs in both Python and Kotlin that do the same things. Your goal is to look through them to fill in a reference sheet of how to write Kotlin code. If you get stuck, look through the Python code first, then switch over to the corresponding Kotlin file.
-
First, it’s important to write pseudocode as comments. Find out how to do both multi-line and single-line comments in Kotlin and write down examples in your reference sheet.
-
Your first program in any language will typically be a “Hello, world!” program, in part because it teaches you how to write a main function, print statements, and specify strings. Find an example of the boiler-plate code to successfully print “Hello, world!” and put it in your reference sheet. (Hint: try
Hello.kt.) -
Finally, you’ll need to make lots of variables. Find an example of making a variable and add it to your reference sheet. Can you figure out why some are specified with
valand others withvar?
Exercise 2
Now, let’s explore some interesting things in Kotlin.
-
Find an example of printing a variable’s value and put it in your reference sheet.
-
Find examples of a for loop and a while loop and put them in your reference sheet.
-
Find an example of an if statement and put it in your reference sheet.
-
Find an example of a when statement and put it in your reference sheet. What’s going on here?
Exercise 3
Here are some other interesting things you might want to do. If any of these things aren’t familiar to you, please ask your neighbor, me, or the prefect! Find examples of:
-
Making a constructor for a class.
-
Using default parameter values in a constructor.
-
Instantiating an instance of a class.
-
Calling methods of a class.
If there is anything else that you see in these files that you’d like to keep track of, add it to your reference sheet – it’s for your own benefit!
Exercise 4
Finally, are you ready to run a Kotlin program?
First you need to compile it. We’ll do this in the terminal:
kotlinc Hello.ktThen you can run it:
kotlin HelloKt.classPut these two commands in your reference sheet.
Want more to do?
Exercise 5
We’ll talk about lists more in depth in the coming weeks, but you’ll probably want to use one sooner rather than later. Find examples of:
-
making a list
-
adding to a list
-
getting an item from a list
-
looping over the items in a list
Exercise 6
There are even more useful bits of functionality that we haven’t explored yet.
-
Find an exmaple of reading from a file and put it in your reference sheet.
-
Importing a Kotlin/Java library.
-
Getting a random number (this may require multiple steps…).
Exercise 7
If you finish that and have time, you can try changing the MultiSidedDie class so it rolls the dice one-by-one and waits for the user to tell it to roll the next one (or two?). (Hint: See Conditionals.kt for an example of getting a number from the user.)