Computer scienceProgramming languagesKotlinObject-oriented programmingSpecial constructions / Special classes

Object declarations

Blocks in a box

Report a typo

Let's create a simple class with a nested object. There is a box of blocks for kids. All of them are the same size but their colors can be different. We will store these objects in a class Block with one property color and a nested object BlockProperties with properties length and width. Your task is to create this structure!

In addition, your nested object should have a blocksInBox() function that receives the length and width (parameters order is important) of a box and returns the number of blocks this box can store.

Make the properties of a nested object mutable for the validation system! You may initialize them with any numbers, for example, 6 and 4. as in the example. Don't worry about heights, solve it like a 2D problem. Don't rotate the blocks.

Block and Box with four nested Blocks

Tip: Don't forget about the order of precedence! (9/4) * (14/6) = 4 but (9*14) / (6*4) = 5

Write a program in Kotlin
class Block(val color: String)
___

Create a free account to access the full topic