0
0
Kotlinprogramming~15 mins

Array creation and usage in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
Array creation and usage
📖 Scenario: You are helping a small shop owner keep track of the prices of some fruits they sell. You will create an array to store the prices, then work with it to find useful information.
🎯 Goal: Build a Kotlin program that creates an array of fruit prices, sets a price threshold, finds which fruits are more expensive than that threshold, and prints those prices.
📋 What You'll Learn
Create an array with exact fruit prices
Create a price threshold variable
Use a loop or array function to find prices above the threshold
Print the filtered prices
💡 Why This Matters
🌍 Real World
Arrays are used to store lists of related data, like prices of items in a shop. Filtering arrays helps find items that meet certain conditions, such as expensive products.
💼 Career
Understanding arrays and how to filter them is essential for many programming jobs, including data analysis, app development, and backend services.
Progress0 / 4 steps
1
Create an array of fruit prices
Create an array called fruitPrices with these exact values: 50, 30, 70, 90, 20.
Kotlin
Need a hint?

Use arrayOf() to create an array with the given numbers.

2
Set a price threshold
Create an integer variable called priceThreshold and set it to 60.
Kotlin
Need a hint?

Use val priceThreshold = 60 to create the variable.

3
Find prices above the threshold
Create a new array called expensiveFruits that contains only the prices from fruitPrices which are greater than priceThreshold. Use the filter function.
Kotlin
Need a hint?

Use fruitPrices.filter { price -> price > priceThreshold } and convert the result to an array with toTypedArray().

4
Print the expensive fruit prices
Print the expensiveFruits array using println. Use joinToString() to display the prices separated by commas.
Kotlin
Need a hint?

Use println(expensiveFruits.joinToString(", ")) to print the prices separated by commas.