I want to declare an array of objects for a clas...
# announcements
a
I want to declare an array of objects for a class and then declare its properties and use the getter statement to assign a value to another property using the declared properties, I understood what is being asked but I am sure about the syntax, help me out
g
I don’t understand what you’re asking, can you provide the code you wrote?
a
Copy code
class Spice(var Spicename:String,var Spiceness:String="Mild"){


init{
    println("Spice name is ${Spicename)} and Spiciness is ${Spiceness}")
}
}
val s= listOf<Spice>()
I want a another parameter heat which would be assigned a value according to data in list
For example if spiceness is "Mild" than heat would be 5
g
have you tried writing a function that compute that value?
a
So I will create a list where each level of spiceness has heat value and get will assign it to heat property
Hope you understood What I said
Copy code
Create a new class, Spice.
Pass in a mandatory String argument for the name, and a String argument for the level of spiciness where the default value is mild for not spicy.
Add a variable, heat, to your class, with a getter that returns a numeric value for each type of spiciness.
Instead of the list of spices as Strings you used earlier, create a list of Spice objects and give each object a name and a spiciness level.
Add an init block that prints out the values for the object after it has been created. Create a spice.
Create a list of spices that are spicy or less than spicy. Hint: Use a filter and the heat property.
Because salt is a very common spice, create a helper function called makeSalt().
This the question
please help out I know this is long but help me out
g
You can try something like this
Copy code
val heat: Int
    get() {
        // implement your logic
    }
a
how to write the list
the syntax....
g
if you want a list you can use
listOf(1, 2, 3)
If you need the syntax you should refer to the documentation https://kotlinlang.org/docs/reference/collections-overview.html#list