0
0
Kotlinprogramming~15 mins

Class declaration syntax in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
Class declaration syntax
📖 Scenario: You are creating a simple program to represent a book in a library system.
🎯 Goal: Build a Kotlin class called Book with properties for the book's title and author.
📋 What You'll Learn
Create a class named Book
Add two properties: title and author of type String
Create an instance of Book with specific values
Print the book's title and author
💡 Why This Matters
🌍 Real World
Classes help organize data about real-world things like books, making programs easier to understand and maintain.
💼 Career
Understanding class declaration is fundamental for Kotlin developers building apps, especially for Android development.
Progress0 / 4 steps
1
Create the Book class with properties
Write a Kotlin class called Book with two properties: title and author, both of type String. Use the primary constructor to declare these properties.
Kotlin
Need a hint?

Use class Book(val title: String, val author: String) to declare the class and its properties.

2
Create an instance of the Book class
Create a variable called myBook and assign it a new Book object with the title "The Hobbit" and author "J.R.R. Tolkien".
Kotlin
Need a hint?

Use val myBook = Book("The Hobbit", "J.R.R. Tolkien") to create the instance.

3
Access the properties of the Book instance
Create two variables called bookTitle and bookAuthor and assign them the title and author properties of myBook respectively.
Kotlin
Need a hint?

Use val bookTitle = myBook.title and val bookAuthor = myBook.author to get the properties.

4
Print the book's title and author
Write a println statement to display the text: "Title: The Hobbit, Author: J.R.R. Tolkien" using the variables bookTitle and bookAuthor.
Kotlin
Need a hint?

Use println("Title: $bookTitle, Author: $bookAuthor") to print the output.