Skip to content
Coding Fanatic
  • Android Development
  • News and Reviews
  • Road2Pro
  • Tutorials
  • Services
  • About

Code Your Cake and Eat It – 93 of…

  • February 11, 2021February 12, 2021
  • by Richard Clarke

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.

Copy Code Copied Use a different Browser

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.

Copy Code Copied Use a different Browser

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.

Copy Code Copied Use a different Browser

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

Copy Code Copied Use a different Browser

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

 

Related

Nobody Likes a Bad Recruiter – Road 2 Pro 8
Handling a Coding Test – Road to Pro 9
Richard Clarke
Richard is currently a developer using Java and Android Studio to build custom applications for Android. Richard tutored in mathematics at Montgomery College, and worked in Information Technology before entering the Software Development industry as a Quality Engineer.
100daysofcode Android Android Development AndroidDev development kotlin software software development

Related articles

Your New Favorite Stopwatch App!…
Two for Two BABY! 7…
…But the App is for…
Play Store Blues – 5…
Top 4 Tips for Android…
No Dice – 3 of…

Leave a Reply Cancel reply

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

Join the mailing list!

This field is required.

Check your inbox or spam folder to confirm your subscription.

Follow me on social media!

  • GitHub
  • YouTube
  • X
  • Instagram
  • LinkedIn
My Tweets

Join the mailing list!

This field is required.

Check your inbox or spam folder to confirm your subscription.

Follow me on social media!

  • X
  • GitHub
  • LinkedIn
  • YouTube

Archives

Theme by Colorlib Powered by WordPress