OOP_IO-2x_2023-mirror/Java/lab_5/Word.kt

22 lines
644 B
Kotlin
Raw Permalink Normal View History

2023-06-05 16:37:37 +03:00
package OOP.Java.lab_5
2023-06-07 15:38:48 +03:00
/**
* A class representing a single word is a sentence.
*
* @property letters an array of [Letter] objects that make up a word.
*
* @constructor a primary constructor accepts an array of [Letter] objects, a secondary one accepts a string representing the entire word.
*/
2023-06-05 16:37:37 +03:00
class Word(var letters: Array<Letter>) {
2023-06-07 15:38:48 +03:00
2023-06-05 16:37:37 +03:00
override fun toString(): String {
val wordString = StringBuilder()
this.letters.forEach { wordString.append(it) }
return wordString.toString()
}
2023-06-07 15:38:48 +03:00
2023-06-05 16:37:37 +03:00
constructor(
word: String
2023-06-05 17:11:49 +03:00
) : this((word.toCharArray().map { letter -> Letter(letter) }).toTypedArray())
2023-06-05 16:37:37 +03:00
}