Last Updated on December 18, 2020 by AbdurRahman G Official | Md Ghufran Salafi
Started Learning Android Development From Android Developer Official Site
Write Your First Program in Kotlin:
Summary
- https://developer.android.com/training/kotlinplayground is an interactive code editor on the web where you can practice writing Kotlin programs.
- All Kotlin programs need to have a
main()
function:fun main() {}
- Use the
println()
function to print a line of text. - Place text you want to print between double quotes. For example
"Hello"
. - Repeat the
println()
instruction to print multiple lines of text. - Errors are marked red in the program. There is an error message in the output pane to help you figure out where the error is and what might be causing it.
Vocabulary Link by Android Developer:
https://developer.android.com/courses/android-basics-kotlin/android-basics-kotlin-vocab
Style Guide:
https://developer.android.com/kotlin/style-guide
Official Documentation for Kotlin concepts I have learned in this Codelab:
Defining Variables:
https://kotlinlang.org/docs/reference/basic-syntax.html#defining-variables
Comments:
https://kotlinlang.org/docs/reference/basic-syntax.html#comments
Defining Functions:
https://kotlinlang.org/docs/reference/basic-syntax.html#defining-functions
repeat statement:
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/repeat.html
Create a birthday message in Kotlin:
Assignment 1st:
Code:
fun main() {
val border = “‘-._,-‘” //Let’s Print the Starter Banner using printBorder() function println(“Happy Birthday, ${name}!”) //Let’s End the Banner here using printBorder() function //Let’s Print a Cake for AbdurRahman G to Celebrate Him //This Will Print an Empty Line println(“You are already ${age} days Old, ${name}!”) } fun printBorder(border: String, timesToRepeat: Int) { //Let’s Design the Border uisng printBorder() function } |
Output:
Assignment 2nd:
Create a cake with the layers and candles
Instructions:
In this task, you are going to upgrade the birthday cake code to always be the right size with the right number of candles for any age.
- You will create a total of three functions for drawing a layered cake with candles.
- You will use a
repeat()
inside anotherrepeat()
, creating what’s called a “nested loop”. - The way you will build up this code is how you can build up any program, starting with the big picture and adding detail. This is called “top-down development”.
- The instructions are not as detailed for this practice, and you can refer to the finished code if you get stuck.
Here is a picture of the cake you will be baking:
,,,,,,,,,,,,,,,,,,,,,,,, |||||||||||||||||||||||| ========================== @@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@
And here are the instructions.
Create the main() function
- Reset your code in the editor to the
Hello, world!
program. - You can remove the argument to
main()
, because you won’t be using it. - In
main()
, create a variableage
and set it to 24. - In
main()
, create a second variablelayers
and set it to 5. - In
main()
, call a functionprintCakeCandles()
and pass in theage
. This will leave you with an error, because you have not created that function yet. - Samewise, call a function
printCakeTop()
and also pass in theage
. - Finally, call a function
printCakeBottom()
and pass in theage
and also the number oflayers
. - To get rid of the errors, comment out the three function calls by adding
//
at the beginning of each line, as shown below. This technique allows you to draft your code without triggering errors. - Run your program, and it should have no errors and do nothing.
Your main()
function should look like the code below.
fun main() {
val age = 24
val layers = 5
// printCakeCandles(age)
// printCakeTop(age)
// printCakeBottom(age, layers)
}
Create printCakeTop()
The printCakeTop()
function to print the top of the cake, a line of equal signs, is almost the same as the printBorder()
function you created earlier in this codelab.
==========================
- Below the
main()
function, add a blank line, and then create a function,printCakeTop()
that takes one argument,age
, of typeInt
. - Inside, use a
repeat()
statement to print one equal signage
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()
is done, print an empty line. - In
main()
, remove the two//
symbols from the beginning of the line of code forprintCakeTop()
, because the function now exists.
printCakeTop(age)
Here is your finished function.
fun printCakeTop(age: Int) {
repeat(age + 2) {
print("=")
}
println()
}
- Run your code to see the top of the cake.
Create printCakeCandles()
Each candle is made up of two symbols: a comma (,) for the flame, and a vertical line (|) for the candle body.
,,,,,,,,,,,,,,,,,,,,,,,,
||||||||||||||||||||||||
To accomplish this in one function, put two repeat()
statements, one for the flames and one for the bodies, into your function.
- Below the
main()
function and theprintCakeTop()
function, create a new function,printCakeCandles()
that takes one argument,age
, of typeInt
. - 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 |. - In
main()
, remove the two//
symbols from the beginning of the line of code forprintCakeCandles()
.
printCakeCandles(age)
- Run your code to see the top of the cake and the candles
Solution:
fun printCakeCandles(age: Int) {
print (" ")
repeat(age) {
print(",")
}
println() // Print an empty line
print(" ") // Print the inset of the candles on the cake
repeat(age) {
print("|")
}
println()
}
Create printCakeBottom()
In this function, you are drawing a cake bottom that is as wide as age + 2
, and you draw it for a height of a given number of layers.
@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@
- This means your function needs two arguments, one for the width (
age
) and one for the height (layers
). - To print the bottom of the cake, you first repeat the ‘at’ @ symbol
age + 2
times to print one layer. Then, you repeat printing one layerlayers
times.
Draw the at symbol age+2 times to create a layer
- Below the existing functions, create a function
printCakeBottom()
with two arguments,age
andlayers
, both of typeInt
. - Inside the function, use a
repeat()
statement to print one layer of ‘at’@
symbolsage + 2
times. Finish by printing an empty line, as shown below.
fun printCakeBottom(age: Int, layers: Int) {
repeat(age + 2) {
print("@")
}
println()
}
- Run your code to verify that it prints one line of cake bottom.
,,,,,,,,,,,,,,,,,,,,,,,, |||||||||||||||||||||||| ========================== @@@@@@@@@@@@@@@@@@@@@@@@@@
Nested repeat() statements
To print multiple identical layers of cake bottom, you could say:
For layer 1 repeat the symbol 12 times: @@@@@@@@@@@@
For layer 2 repeat the symbol 12 times: @@@@@@@@@@@@
For layer 3 repeat the symbol 12 times: @@@@@@@@@@@@
Or you can say this much more concisely as:
Repeat for three layers:
Repeat the symbol 12 times.
@@@@@@@@@@@@
@@@@@@@@@@@@
@@@@@@@@@@@@
Now, this is something neat you can do with repeat()
statements. You can put one repeat()
statement inside another repeat()
statement. So you could create a repeat()
statement within a repeat()
statement to print the symbol a certain number of times for a certain number of layers.
Use a nested repeat() to print cake layers
- Put a second
repeat()
statement around all of the code inside the function. Repeat this looplayers
times. - In
main()
, remove only the two//
from the line of code forprintCakeBottom()
.
printCakeBottom(age, layers)
- Run your code to see the whole cake.
Solution for printCakeBottom()
.
fun printCakeBottom(age: Int, layers: Int) {
repeat(layers) {
repeat(age + 2) {
print("@")
}
println()
}
}
Congratulations! You’ve just finished a pretty complex program with several functions and a nested repeat
statement. And your cake will always have the right number of candles!
The final output of your program should be:
,,,,,,,,,,,,,,,,,,,,,,,, |||||||||||||||||||||||| ========================== @@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@
My Code and Output with These Instructions:
Code:
//Let’s create main() function fun main () { //Define all variables val age = 24 val layers = 5// println(“Hi, World!”)//Calling to functions printCakeCandles(age) printCakeTop(age) printCakeBottom(age, layers)}//Let’s Create function for Cake Top fun printCakeTop(age: Int) { repeat(age + 2){ print(“=”) } println() //Print an empty line}//Let’s Create another function for Candles fun printCakeCandles(age: Int) { print(” “) //print space befor repeat() statement repeat(age) { print(“,”) } println() //Print an empty lline print(” “) //Print the inset of the candles on the cake } //Let’s Create function for Cake Bottom in Using Nested repeat() |
Output:
![]() |
Solutions by Android Developer Official Site:
Solution code
fun main() {
val age = 24
val layers = 5
printCakeCandles(age)
printCakeTop(age)
printCakeBottom(age, layers)
}
fun printCakeCandles(age: Int) {
print (" ")
repeat(age) {
print(",")
}
println() // Print an empty line
print(" ") // Print the inset of the candles on the cake
repeat(age) {
print("|")
}
println()
}
fun printCakeTop(age: Int) {
repeat(age + 2) {
print("=")
}
println()
}
fun printCakeBottom(age: Int, layers: Int) {
repeat(layers) {
repeat(age + 2) {
print("@")
}
println()
}
}
Summary
- Use
${}
to surround variables and calculations in the text of print statements. For example:${age}
whereage
is a variable. - Create a variable using the
val
keyword and a name. Once set, this value cannot be changed. Assign a value to a variable using the equal sign. Examples of values are text and numbers. - A
String
is text surrounded by quotes, such as"Hello"
. - An
Int
is a whole positive or negative number, such as 0, 23, or -1024. - You can pass one or more arguments into a function for the function to use, for example:
fun printCakeBottom(age:Int, layers:Int) {}
- Use a
repeat() {}
statement to repeat a set of instructions several times. For example:repeat (23) { print("%") }
orrepeat (layers) { print("@@@@@@@@@@") }
- A loop is an instruction to repeat instructions multiple times. A
repeat()
statement is an example of a loop. - You can nest loops, that is, put loops within loops. For example, you can create a
repeat()
statement within arepeat()
statement to print a symbol a number of times for a number of rows, like you did for the cake layers.
Summary of using function arguments: To use arguments with a function, you need to do three things:
- Add the argument and type to the function definition:
printBorder(border: String)
- Use the argument inside the function:
println(border)
- Supply the argument when you call the function:
printBorder(border)
Modified Solution Code by AbdurRahman G:
//Let’s create main() function fun main () { //Define all variables val age = 24 val layers = 5 val cakeTopSymbol = “=” val cakeCandlesSymbol = “,” val cakeLayersSymbol = “|” val cakeBottomSymbol = “@”// println(“Hi, World!”)//Calling to functions printCakeCandles(age, cakeCandlesSymbol, cakeLayersSymbol) } //Let’s Create function for Cake Top } //Let’s Create another function for Candles print(” “) //Print the inset of the candles on the cake } //Let’s Create function for Cake Bottom in Using Nested repeat() |
Output:
That’s It dear.
Thanks for Visiting;
Love You!
Assalamu Alaykum
and
Allah Hafiz.
by:
AbdurRahman G