I am new to Kotlin, coming from JS (but am anyway a very amateur, self-taught programmer). In the past, my approach has been to just get it done but I have been loving the conciseness of Kotlin and would like to avoid unnecessary replications throughout similar elements in my program. I am wondering if I might be able to get some guidance with respect to child classes of a parent class. Currently, I am working with two types :
class Ratio(var num: Int, var den: Int) {
var monzo: MutableList<Int> = calculateMonzo(num, den)
var sizeInCents: Double = calculateCents(num, den)
var centDeviation: Pair<Double, String> = calculateCentDeviation(num, den, monzo)
var notation: Triple<String, String, String> = calculateNotation(monzo)
var frequency: Double = calculateFrequency(num, den)
}
and
class Monzo(var monzo: List<Int>) {
var num: Int = calculateRatio(monzo).first
var den: Int = calculateRatio(monzo).second
var sizeInCents: Double = calculateCents(num, den)
var centDeviation: Pair<Double, String> = calculateCentDeviation(num, den, monzo)
var hejiString: Triple<String, String, String> = calculateNotation(monzo)
var frequency: Double = calculateFrequency(num, den)
}
Basically, Ratio() or Monzo() both represent the same "thing" (a fraction or a prime factorisation of a fraction) and are merely dependent on the user's preferred input. In the end, I calculate the "missing" information, then from that point, the properties and calculations are the same. It makes sense that these would be in a parent class e.g. Input(), but I am unsure about how one best sets up the parent class, then accesses those parent properties when calling an instance of one of the child classes.