Learn Ethical Hacking from DROP Organization

Create a birthday message in Kotlin

 

 Create a birthday message in Kotlin



Set up your starter code

  1. In your browser, open https://developer.android.com/training/kotlinplayground. This opens a browser-based Kotlin programming tool.
  2. Inside the fun main() function, replace the "Hello, world!" text with "Happy Birthday, Rover!".
  3. Below that, still inside the curly braces, add two more lines to print: "You are already 5!" and "5 is the very best age to celebrate!".

Your finished code should look like this.

fun main() {
    println
("Happy Birthday, Rover!")
    println
("You are already 5!")
    println
("5 is the very best age to celebrate!")
}
  1. Run your code.
  2. Verify that the output pane shows Happy Birthday, Rover! and below that, You are already 5! and 5 is the very best age to celebrate!
Happy Birthday, Rover!
You are already 5!
5 is the very best age to celebrate!

Add a birthday cake

A birthday message needs a birthday-themed picture. Like, a cake. You can add a cake to your birthday message by printing additional lines that use the letters and symbols on your keyboards and println().

Continue from the solution code above.

  1. In your code, between the two println() statements for Happy Birthday and You are already 5, add the following lines of print statements, as shown below. This creates a cake. The last println() statement has no text between the quotes, which prints an empty line.
    println("   ,,,,,   ")
    println
("   |||||   ")
    println
(" =========")
    println
("@@@@@@@@@@@")
    println
("{~@~@~@~@~}")
    println
("@@@@@@@@@@@")
    println
("")

To help others understand your code, you can add a comment before you print the cake. If you run your code, the output won't look any different, because comments are just information for you and other developers, not commands for the system. An inline comment starts with // followed by text, as shown below.

// This is a comment line
// This is another comment
  1. Add a comment before you print the cake: // Let's print a cake!.
  2. Add a comment before printing the empty line: // This prints an empty line.

Your code should look like the code below.

fun main() {
    println
("Happy Birthday, Rover!")
   
   
// Let's print a cake!
    println
("   ,,,,,   ")
    println
("   |||||   ")
    println
(" =========")
    println
("@@@@@@@@@@@")
    println
("{~@~@~@~@~}")
    println
("@@@@@@@@@@@")
   
   
// This prints an empty line.
    println
("")

    println
("You are already 5!")
    println
("5 is the very best age to celebrate!")
}
  1. Run your code, and the output should look like below.
Happy Birthday, Rover!
   ,,,,,   
   |||||   
 =========
@@@@@@@@@@@
{~@~@~@~@~}
@@@@@@@@@@@

You are already 5!
5 is the very best age to celebrate!

About Roshan Burnwal

Roshan Burnwal
Recommended Posts × +

0 Comments: