Solution to 9a9b: Transposing tunes
See code at solutions/code/tutorialquestions/question9a9b
The question was broken down into steps and accompanied by various hints. Look back at these steps and hints, and see how the questions have been implemented in the sample source code solutions. You will probably learn a lot by carefully studying the source code solution to this question. Here are just a few further notes:
In the NoteName
enumeration, notice that notes like C#
are represented by identifiers like C_SHARP
.
This is because #
cannot be used as part of a Java identifier. However, to allow note names to be printed nicely, I have
overridden toString
in NoteName
, so that an enumeration value is turned into an appropriate string.
Because all tune elements, whether notes or rests, have a value, my abstract TuneElement
class declares a field of type
NoteValue
. Notice that subclass Rest
does not add any further fields, or any behaviour other than providing a
toString
implementation. You might be tempted to get rid of Rest
, and make TuneElement
concrete:
using a plain old TuneElement
to represent a rest, and a more specific Note
for a note. However, I feel the
code is easier to understand if we provide an explicit Rest
class.