Started Learning Android Development From Android Developer Official Site

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 = “‘-._,-‘”
val timesToRepeat = 5
val name = “AbdurRahman G”
val age = 22*365

//Let’s Print the Starter Banner using printBorder() function
printBorder(border, timesToRepeat)

println(“Happy Birthday, ${name}!”)

//Let’s End the Banner here using printBorder() function
printBorder(border, timesToRepeat)

//Let’s Print a Cake for AbdurRahman G to Celebrate Him
println(” ,,,,, “)
println(” ||||| “)
println(” ========= “)
println(“@@@@@@@@@@@”)
println(“{~@~@~@~@~}”)

//This Will Print an Empty Line
println(“”)

println(“You are already ${age} days Old, ${name}!”)
println(“${age} days Old is the Very best Age to Celebrate!”)

}

fun printBorder(border: String, timesToRepeat: Int) {

//Let’s Design the Border uisng printBorder() function
repeat(timesToRepeat) {
print(border)
}
//Let’s Print an Empty Line
println()

}

Output:

Output of Happy Birthday AbdurRahman G

 

 

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 another repeat(), 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

  1. Reset your code in the editor to the Hello, world! program.
  2. You can remove the argument to main(), because you won’t be using it.
  3. In main(), create a variable age and set it to 24.
  4. In main(), create a second variable layers and set it to 5.
  5. In main(), call a function printCakeCandles() and pass in the age. This will leave you with an error, because you have not created that function yet.
  6. Samewise, call a function printCakeTop() and also pass in the age.
  7. Finally, call a function printCakeBottom() and pass in the age and also the number of layers.
  8. 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.
  9. 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.

==========================
  1. Below the main() function, add a blank line, and then create a function, printCakeTop() that takes one argument, age, of type Int.
  2. Inside, 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.
  3. At the end, when the repeat() is done, print an empty line.
  4. In main(), remove the two // symbols from the beginning of the line of code for printCakeTop(), because the function now exists.
printCakeTop(age)

Here is your finished function.

fun printCakeTop(age: Int) {
    repeat(age + 2) {
        print("=")
    }
    println()
}
  1. 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.

  1. Below the main() function and the printCakeTop() function, create a new function, printCakeCandles() that takes one argument, age, of type Int.
  2. Inside, use a repeat() statement to print one comma , for the flame.
  3. Repeat this age times.
  4. At the end, print an empty line.
  5. Add a print() statement to print one space for insetting the candles.
  6. Below, repeat the steps to create a second repeat() statement to print the candle bodies with a vertical line |.
  7. In main(), remove the two // symbols from the beginning of the line of code for printCakeCandles().
printCakeCandles(age)
  1. 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 layer layers times.

Draw the at symbol age+2 times to create a layer

  1. Below the existing functions, create a function printCakeBottom() with two arguments, age and layers, both of type Int.
  2. Inside the function, use a repeat() statement to print one layer of ‘at’ @ symbols age + 2 times. Finish by printing an empty line, as shown below.
fun printCakeBottom(age: Int, layers: Int) {
    repeat(age + 2) {
        print("@")
    }
    println()
}
  1. 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

  1. Put a second repeat() statement around all of the code inside the function. Repeat this loop layers times.
  2. In main(), remove only the two // from the line of code for printCakeBottom().
printCakeBottom(age, layers)
  1. 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
repeat(age){
print(“|”)
}
println() //Print an empty line

}

//Let’s Create function for Cake Bottom in Using Nested repeat()
fun printCakeBottom(age: Int, layers: Int) {
repeat(layers) {
repeat (age + 2) {
print(“@”)
}
println() //Print an empty line
}
}

Output:

Print Birthday Cake assignment 2nd

 

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} where age 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("%") } or repeat (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 a repeat() 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)
printCakeTop(age, cakeTopSymbol)
printCakeBottom(age, layers, cakeBottomSymbol)

}

//Let’s Create function for Cake Top
fun printCakeTop(age: Int, cakeTopSymbol: String) {
repeat(age + 2){
print(cakeTopSymbol)
}
println() //Print an empty line

}

//Let’s Create another function for Candles
fun printCakeCandles(age: Int, cakeCandlesSymbol: String, cakeLayersSymbol: String) {
print(” “) //print space befor repeat() statement
repeat(age) {
print(cakeCandlesSymbol)
}
println() //Print an empty lline

print(” “) //Print the inset of the candles on the cake
repeat(age){
print(cakeLayersSymbol)
}
println() //Print an empty line

}

//Let’s Create function for Cake Bottom in Using Nested repeat()
fun printCakeBottom(age: Int, layers: Int, cakeBottomSymbol: String) {
repeat(layers) {
repeat (age + 2) {
print(cakeBottomSymbol)
}
println() //Print an empty line
}
}

Output:

Print Birthday Cake assignment 2nd

 

That’s It dear.

Thanks for Visiting;

Love You!

Assalamu Alaykum

and

Allah Hafiz.

by:

AbdurRahman G

Leave a Reply

Your email address will not be published. Required fields are marked *