Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

write enum Pitch that enumerates the 1 2 pitches of a musical scale. Seven of the pitches are labeled A , B , C ,

write enum Pitch that enumerates the
12 pitches of a musical scale. Seven of the pitches are labeled A, B, C, D, E, F, and G, and five are "half-way"
pitches written as a letter and a following "flat" symbol that is actually shown in the class diagram (we'll use a
lowercase b for the full credit solution). Just write what the UML diagram tells you - you needn't sing them! :D
Then write class Note with 2 private fields, 2 public constructors, and a public method as shown in the UML
class diagram. When correctly written and compiled with class Song in file Song.java (that includes a public static main method, the final
program will print the notes to "Mary Had a Little Lamb" to the console.
Class Note represents a Pitch along with an octave. The seven main pitches are often named Do, Re, Mi, Fa,
So, La, Ti, and then repeat to Do again, but the second Do is higher than the first (for us engineers, the sound
has exactly twice the frequency). We call each repeated Do a new "octave", so our Note class has both a
pitch and an octave field. The octave must be between -5 and 4, inclusive- so that's 7 regular and 5 flat
pitches over 10 octaves, or 120 different notes. That should just about cover what our ears can hear!
That's relative to "middle C", for your music majors!
The default constructor should set the pitch field to null. This indicates no sound at all.
The other constructor should assign each parameter to its corresponding field. For example, to assign
parameter pitch to field pitch you would write this.pitch = pitch; because this.pitch
always means "the member of the current class named pitch". Assigning constructor parameters to
fields using this pattern is very common in Java constructors! Also validate the octave parameter - if it's
less than -5, use -5, and if it's greater than 4, use 4. Don't worry, we'll learn how to correctly report
parameter errors soon!
Method toString()(be sure to use @Override) should return a space if field pitch is null. Otherwise,
return a string representation of the Note by concatenating the pitch and (only if not zero) octave. So
for a pitch of Eb and an octave of 2, you would return Eb2. Note that toString has NO PARAMETERS
- listen to the old guy with the flashy light stick and use the fields, Luke.
You may NOT add any getters to class ``Note`` to retrieve fields for your main method. Those fields are
private - hands off! Instead, you may ONLY use the toString() method to format the fields for printing.
Encapsulation!
When compiled with the provided Song.java (you may NOT modify this file), our output should be exactly as
shown - the notes for "Mary Had a Little Lamb". You do NOT need to provide screenshots, however, we'll run it
ourselves

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions