Code Your Cake and Eat It – 93 of…
With the basics down, another chapter of the Kotlin journey is complete!
Well, the first chapter anyway. For more on this, watch the video at the bottom of the page.
In a previous article I mentioned that most data types from Java aren’t used for variables in Kotlin. As suspected, this saved me a lot of time with learning the basics.
Now, there’s still conditionals, relational operators, and other things the Introduction to Kotlin Course didn’t review. These are usually similar across languages so I can look up the exact syntax when I need them.
The last exercise of the course involved using ascii art to create the image of a birthday cake. The prompt used a series of functions and repeat statements to display the cake.
THE CODE
The main function declared two variables: age, and layers.
Main Function
The cake included candles, the cake top, and cake bottom. I created methods for printing each of these sections of the cake respectively.
fun main(){
//1. Create a variable age and set it to 24
//2. Create a variable layers set to 5
//3. In main(), call a function printCakeCandles() and pass age
//4. In main(), call a function printCakeTop and also pass in the age
//5. In main(), call a function printCakeBottom and pass in the age and number of layers
val age = 24
val layers = 5
printCakeCandles(age)
printCakeTop(age)
printCakeBottom(age, layers)
}
Cake Candles
The printcakeCandles( ) method was used to for the candles of the cake. It accepted the person’s age and printed that many commas on one line and vertical lines | on the next. The instructions say to add a space before and after the candles for styling.
fun printCakeCandles(age: Int){
/*
Create a function, printCakeCandles() that takes one argument,
age, of type Int.
Inside, use a repeat() statement to print one comma
for the flame. Repeat this age times.
At the end, print an empty line.
Add a print() statement to print one space for insetting
the candles.
Below, repeat the steps to create a second repeat() statement
to print the candle bodies with a vertical line |.
*/
print(" ")
repeat(age){
print(",")
}
println()
print(" ")
repeat(age){
print("|")
}
println()
}
Cake Top
The printcakeTop( ) method was used to for the decorations on top of the cake. It accepted the person’s age and printed that many equal signs. Notice how the spaces from the printCakeCandles method create the image of candles atop a cake.
fun printCakeTop(age: Int){
/*
Use a repeat() statement to print one equal sign age
times plus 2. The extra two equals signs are so that
the candles won't fall off the side of the cake.
At the end, when the repeat statement is done, print an
empty line.
*/
repeat(age + 2){
print("=")
}
println()
}
Cake Bottom
The printCakeBottom( ) method was used to print the layers for the cake. This method accepted the age and layers and printed the same number of @ symbols as the age for each layer. This completed the image of the ASCII cake
fun printCakeBottom(age: Int, layers: Int){
/*
The cake width is age + 2
The cake height is the number of layers
*/
repeat(layers){
repeat(age + 2){
print("@")
}
println()
}
}
This short course gave me a lot to work with. I wanted to learn some basic Kotlin syntax before building apps with it. I still believe the quickest way to learn a language is to use it. But without the basics, you won’t know heads from tails.
These exercises gave me an idea of how Kotlin is used in general. No need to get too in-depth just yet. I just wanted short breathers set up to get my feet wet, and it worked!
My next step is to find a tutorial on building apps with Kotlin. I should be able to follow along easily now that I have the basics down.
Join the mailing list to see updates like this every week!
Coding Fanatic
Sources
Android Basics: Introduction to Kotlin
Birthday Cake
Image by Jill Wellington from Pixabay