0
0
Kotlinprogramming~15 mins

Constant values with const val in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Constant Values with const val in Kotlin
📖 Scenario: You are creating a simple program to calculate the area of a rectangle. To keep your code clean and easy to update, you want to use a constant value for the number of sides of a rectangle.
🎯 Goal: Learn how to declare and use constant values with const val in Kotlin by creating a constant for the number of sides of a rectangle and using it in a calculation.
📋 What You'll Learn
Declare a constant using const val
Use the constant in a calculation
Print the result
💡 Why This Matters
🌍 Real World
Constants are used in real-world programs to store values that should not change, like configuration settings, fixed measurements, or limits.
💼 Career
Understanding how to use constants helps you write safer and clearer code, which is important in software development jobs.
Progress0 / 4 steps
1
Create a constant for the number of sides
Write a const val named SIDES and set it to 4.
Kotlin
Need a hint?

Use const val to declare a constant value that does not change.

2
Create variables for length and width
Create two variables: length with value 5 and width with value 3.
Kotlin
Need a hint?

Use val to create variables that hold the rectangle's dimensions.

3
Calculate the area of the rectangle
Create a variable area and set it to the product of length and width.
Kotlin
Need a hint?

Multiply length and width to get the area.

4
Print the number of sides and the area
Print the text "A rectangle has " followed by the constant SIDES, then " sides and area " followed by the variable area.
Kotlin
Need a hint?

Use println and string templates with $ to include variables in the output.