1. Classes and Objects

[THEORY]

[PREDICT]

Read the code carefully before you run it.

prediction.kt
class Customer {
    var name: String = ""
}

fun main() {
    val customer1 = Customer()
    customer1.name = "Anna"

    val customer2 = customer1
    customer2.name = "Ben"

    println("${customer1.name} ${customer2.name}")
}

What does the code print? Does customer1 change even though only customer2.name is modified — just as with `var b = a` (Int), where only b changes and not a?

[TASK]

Define a class Product with two mutable properties: name of type String (initial value "") and price of type Double (initial value 0.0). Add a method showInfo() that prints "Product: <name>, price: <price>€". In main(), create a product, set name to "Keyboard" and price to 49.99, and call showInfo().

task.kt
fun main() { // TODO: create Product, set name and price, call showInfo() } // TODO: define class Product

Before you get a hint: what have you tried, and where exactly are you stuck?

[COMPREHENSION CHECK]

1. What is the difference between a class and an object?

2. Two variables a and b point to the same object after val b = a. What happens when you change a property through b?