0
0
Kotlinprogramming~30 mins

Data classes for value holders in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
Data classes for value holders
📖 Scenario: You are creating a simple app to store information about books in a library. Each book has a title, an author, and a year of publication.
🎯 Goal: Build a Kotlin program that uses a data class to hold book information, creates a list of books, and prints their details.
📋 What You'll Learn
Create a data class called Book with properties title, author, and year
Create a list called library containing three Book objects with exact values
Use a for loop with variables book to iterate over library
Print each book's details in the format: Title: [title], Author: [author], Year: [year]
💡 Why This Matters
🌍 Real World
Data classes help organize related information clearly, like storing book details in a library app.
💼 Career
Understanding data classes is essential for Kotlin developers to write clean, maintainable code that models real-world data.
Progress0 / 4 steps
1
Create the data class Book
Create a Kotlin data class called Book with three properties: title of type String, author of type String, and year of type Int.
Kotlin
Need a hint?

Use the data class keyword followed by the class name and properties inside parentheses.

2
Create a list of books called library
Create a list called library containing three Book objects with these exact values:
1. Title: "1984", Author: "George Orwell", Year: 1949
2. Title: "To Kill a Mockingbird", Author: "Harper Lee", Year: 1960
3. Title: "The Great Gatsby", Author: "F. Scott Fitzgerald", Year: 1925
Kotlin
Need a hint?

Use listOf() to create the list and call the Book constructor for each item.

3
Loop through library to access each book
Use a for loop with variable book to iterate over the library list.
Kotlin
Need a hint?

Use for (book in library) to loop through the list.

4
Print each book's details
Inside the for loop, write a println statement to print each book's details in this exact format:
Title: [title], Author: [author], Year: [year]
Use the book variable's properties.
Kotlin
Need a hint?

Use println with string templates like "${book.title}" to insert values.