Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Code the note class in java Note - note: Pitch get/set>> -octave : int get/ set>> - length : Beat get/set >> + Note() +
Code the note class in java
Note - note: Pitch get/set>> -octave : int get/ set>> - length : Beat get/set >> + Note() + Note(Pitch, int, Beat) + Note(String, Beat) + Note(int, Beat) + Note(double, Beat) + getSPNO): String + setSPN(String) : void + getMIDIO : int + setMIDI(int) : void + getFrequency0: double + setFrequency(double): void + toString(): String The class has five constructors: 1. The default constructor creates a Ct quarter note. 2. The main constructor takes a Pitch, an octave, and a Beat, and checks that the octave is within the range from - 1 to 9 - if the note is G*, A, A#, or B, then the maximum valid octave is reduced to 8. If the input is valid, then this constructor sets the pitch, octave, and beat length of the note directly. 3. The third constructor checks that a String is a valid note in scientific pitch notation (SPN), and if so parses it to set the pitch and octave. 4. The fourth constructor takes a MIDI number from 0 to 127, and converts it to set the note and octave. 5. And the final constructor takes a frequency and uses it to set the pitch and octave to the nearest corresponding MIDI number. If an input to any of the constructors does not correspond to a valid MIDI number, fallback to the default of creating a C4 note of the given length. Writing the constructors will be much easier after you have written the get and set methods to convert a note to and from its SPN string, MIDI number, and frequency. The SPN string is just the name of the note followed by its octave. Examples of valid SPN strings are "C-1", "D#4", and "E9". You can use a regular expression to check whether an SPN string is valid before parsing and setting the pitch and octave values: if (spn.matches (" [CDFGA] #? | [EB]) (-11[0-8])/([CDF] #?| [EG])9)")) { // Convert the String to a MIDI number The MIDI number numbers the notes from C-1 to G9 from 0 to 127 (See Figure 12). Any note outside that range is invalid! You can convert the pitch and octave to a MIDI number with this equation: midi(pitch, octave) = 69+offset [pitch] + 12. (octave - 4) And finding the reverse is simply a matter of dividing and taking the remainder: octave = [midi/12] - 1 value (pitch) = midi mod 12 The frequency is defined in terms of the MIDI number, m, with this equation: f = 440.2 (midi69)/12 And the inverse of this equation converts in the other direction: midi = 12 - log2 (10) +69 When setting the note based on any of these, it is important to check that the result is within the range of valid MIDI notes. The setters should do nothing if given a note higher than C-1 or lower than Gy, and the constructors should fallback to creating a C note. Likewise, set Octave () should always check that the new octave is between-1 and 9, (or between -1 and 8 if the pitch is > G"). Similarly, set Pitch() should avoid setting the note to G*, A, A#, or B if the current octave is 9. The toString() method retums SPN of the note, followed by the beat, separated by a single space. For example, a C quarter note would be displayed as "C4 (1/4)", and an Feighth note would be "F#3 (1/8)". Create an enumeration called Pitch.java that represents the 12 different notes in an octave: Cenumeration> Pitch CSHARP DSHARP FSHARP GSHARP ASHARP getOffset(): int toString(): String The enum class should have add two extra methods. The first, get Offset(), returns the offset shown in the table below; Pitch. A.getOffset() should retum 0, and Pitch.C.getOffset() should return -9. | Note || C Value 0 | Offset || -9 | C 1 -8 D 2 -7 D# 3 -6 E F 4 | 5 -5 -4 F# G G A A B | 6 | 7 | 8 | 9 | 10 | 11 | -3 -2 -1 0 1 2 Figure 11: Table of Note values and offsets The toString() method should return the note in the format "C#". That is, use a "#" symbol instead of the word "SHARP" when you print out the note. For non-sharp notes, toString() returns the same as name. Implement another enumeration, Beat.java to represent the duration of a note: Cenumeration> Beat WHOLE THREEQUARTER HALF THREEEIGHTH QUARTER THREESIXTEENTH EIGHTH SIXTEENTH getBeats(): double getDuration(int) : double toString(): String Assuming the music is in standard time, a quarter note plays for a duration of 1 beat. The other beats are defined predictably in terms of the quarter note: Beats Value WHOLE THREE QUARTER HALF THREEEIGHTH QUARTER THREESIXTEENTH EIGHTH SIXTEENTH 3/2 1 3/4 | 1/2 1/4 The get Beats() method simply returns the values from the above table. The method get Duration calculates the time, in seconds, that the note should be played given a tempo in units of beats per minute. The input says how many quarter notes can be played in 60 seconds, and the output is the number of seconds it takes to play the beat (60.0 * beats) / tempo). The toString method prints the fraction of a WHOLE note that the beat represents, wrapped in parentheses. For example, Beat .WHOLE.toString() retums "(1)", Beat.QUARTER.toString() returns "(1/4)", and Beat . THREESIXTEENTH.toString() returns "(3/16)". Note - note: Pitch get/set>> -octave : int get/ set>> - length : Beat get/set >> + Note() + Note(Pitch, int, Beat) + Note(String, Beat) + Note(int, Beat) + Note(double, Beat) + getSPNO): String + setSPN(String) : void + getMIDIO : int + setMIDI(int) : void + getFrequency0: double + setFrequency(double): void + toString(): String The class has five constructors: 1. The default constructor creates a Ct quarter note. 2. The main constructor takes a Pitch, an octave, and a Beat, and checks that the octave is within the range from - 1 to 9 - if the note is G*, A, A#, or B, then the maximum valid octave is reduced to 8. If the input is valid, then this constructor sets the pitch, octave, and beat length of the note directly. 3. The third constructor checks that a String is a valid note in scientific pitch notation (SPN), and if so parses it to set the pitch and octave. 4. The fourth constructor takes a MIDI number from 0 to 127, and converts it to set the note and octave. 5. And the final constructor takes a frequency and uses it to set the pitch and octave to the nearest corresponding MIDI number. If an input to any of the constructors does not correspond to a valid MIDI number, fallback to the default of creating a C4 note of the given length. Writing the constructors will be much easier after you have written the get and set methods to convert a note to and from its SPN string, MIDI number, and frequency. The SPN string is just the name of the note followed by its octave. Examples of valid SPN strings are "C-1", "D#4", and "E9". You can use a regular expression to check whether an SPN string is valid before parsing and setting the pitch and octave values: if (spn.matches (" [CDFGA] #? | [EB]) (-11[0-8])/([CDF] #?| [EG])9)")) { // Convert the String to a MIDI number The MIDI number numbers the notes from C-1 to G9 from 0 to 127 (See Figure 12). Any note outside that range is invalid! You can convert the pitch and octave to a MIDI number with this equation: midi(pitch, octave) = 69+offset [pitch] + 12. (octave - 4) And finding the reverse is simply a matter of dividing and taking the remainder: octave = [midi/12] - 1 value (pitch) = midi mod 12 The frequency is defined in terms of the MIDI number, m, with this equation: f = 440.2 (midi69)/12 And the inverse of this equation converts in the other direction: midi = 12 - log2 (10) +69 When setting the note based on any of these, it is important to check that the result is within the range of valid MIDI notes. The setters should do nothing if given a note higher than C-1 or lower than Gy, and the constructors should fallback to creating a C note. Likewise, set Octave () should always check that the new octave is between-1 and 9, (or between -1 and 8 if the pitch is > G"). Similarly, set Pitch() should avoid setting the note to G*, A, A#, or B if the current octave is 9. The toString() method retums SPN of the note, followed by the beat, separated by a single space. For example, a C quarter note would be displayed as "C4 (1/4)", and an Feighth note would be "F#3 (1/8)". Create an enumeration called Pitch.java that represents the 12 different notes in an octave: Cenumeration> Pitch CSHARP DSHARP FSHARP GSHARP ASHARP getOffset(): int toString(): String The enum class should have add two extra methods. The first, get Offset(), returns the offset shown in the table below; Pitch. A.getOffset() should retum 0, and Pitch.C.getOffset() should return -9. | Note || C Value 0 | Offset || -9 | C 1 -8 D 2 -7 D# 3 -6 E F 4 | 5 -5 -4 F# G G A A B | 6 | 7 | 8 | 9 | 10 | 11 | -3 -2 -1 0 1 2 Figure 11: Table of Note values and offsets The toString() method should return the note in the format "C#". That is, use a "#" symbol instead of the word "SHARP" when you print out the note. For non-sharp notes, toString() returns the same as name. Implement another enumeration, Beat.java to represent the duration of a note: Cenumeration> Beat WHOLE THREEQUARTER HALF THREEEIGHTH QUARTER THREESIXTEENTH EIGHTH SIXTEENTH getBeats(): double getDuration(int) : double toString(): String Assuming the music is in standard time, a quarter note plays for a duration of 1 beat. The other beats are defined predictably in terms of the quarter note: Beats Value WHOLE THREE QUARTER HALF THREEEIGHTH QUARTER THREESIXTEENTH EIGHTH SIXTEENTH 3/2 1 3/4 | 1/2 1/4 The get Beats() method simply returns the values from the above table. The method get Duration calculates the time, in seconds, that the note should be played given a tempo in units of beats per minute. The input says how many quarter notes can be played in 60 seconds, and the output is the number of seconds it takes to play the beat (60.0 * beats) / tempo). The toString method prints the fraction of a WHOLE note that the beat represents, wrapped in parentheses. For example, Beat .WHOLE.toString() retums "(1)", Beat.QUARTER.toString() returns "(1/4)", and Beat . THREESIXTEENTH.toString() returns "(3/16)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started