Problem Statement
Musicians play a variety of instruments, and in a variety of different keys. One of the many things required of piano players is the ability to produce a scale in any given key. A scale is a series of eight notes, the first and last of which have the same name. On a piano, the keys are arranged in alphabetical order from A to G. Note that A follows G, and this pattern repeats throughout the keyboard.
These keys represent notes, which have letter names which have the following specification:
The first character is called the "letter name" of the note and is one of the letters of the musical alphabet, which are, in order, "ABCDEFG".
The second character, if it exists, is either 'b' (meaning "flat") or '#' (meaning "sharp"). If the second character does not exist, the note is called "natural".
Thus, "Ab" is read "A flat", and "F#" is read "F sharp". The pattern of notes is twelve notes long and reads as follows. Note that some notes have more than one name. If "-" is listed instead of a second name, then the note has only one name.
#: Name 1 Name 2 1: C B# 2: C# Db 3: D - 4: D# Eb 5: E Fb 6: F E# 7: F# Gb 8: G - 9: G# Ab 10: A - 11: A# Bb 12: B Cb
The distance between two adjacent notes (and consequently also the distance between two adjacent keys) is one half-step, and two adjacent half-steps make a whole step. Thus, there is one half-step between C and C# or between E and F, and there is one whole step between D and E or between A and B (remember that this pattern repeats ad infinitum).
A scale is eight notes whose letter names successively and consecutively increment throughout the musical alphabet, the first and last of which have the same letter name, and increment using the following pattern: whole step, whole step, half step, whole step, whole step, whole step, half step. A scale in the key of "D" means that the first and last notes are both "D", similarly, a scale in the key of "F#" means that the first and last notes are both "F#".
Thus, the scale in the key of "D" is: "D E F# G A B C# D". Note that although "F#" is the same note as "Gb", the third note of the scale is "F#", not "Gb".
Given the name of the key (which follows the same specifications as a note), you are to return a
Definition
- Class:
- Piano
- Method:
- scale
- Parameters:
- String
- Returns:
- String
- Method signature:
- String scale(String key)
- (be sure your method is public)
Constraints
- key will contain exactly one or two characters, the first of which will be a capital letter from A to G, and the second of which will be either '#' or 'b'.
Examples
"Cb"
Returns: "Cb Db Eb Fb Gb Ab Bb Cb"
"Ab"
Returns: "Ab Bb C Db Eb F G Ab"
"B"
Returns: "B C# D# E F# G# A# B"
"B#"
Returns: "ERROR"
If we go up one whole step from "B#", we have "D", and since the letter names must successively and consecutively increment, it is not possible to make a scale from "B#". Method returns "ERROR".
"A"
Returns: "A B C# D E F# G# A"
"A#"
Returns: "ERROR"
"Ab"
Returns: "Ab Bb C Db Eb F G Ab"
"Bb"
Returns: "Bb C D Eb F G A Bb"
"B#"
Returns: "ERROR"
"B"
Returns: "B C# D# E F# G# A# B"
"C"
Returns: "C D E F G A B C"
"C#"
Returns: "C# D# E# F# G# A# B# C#"
"Cb"
Returns: "Cb Db Eb Fb Gb Ab Bb Cb"
"D"
Returns: "D E F# G A B C# D"
"D#"
Returns: "ERROR"
"Db"
Returns: "Db Eb F Gb Ab Bb C Db"
"Eb"
Returns: "Eb F G Ab Bb C D Eb"
"E#"
Returns: "ERROR"
"E"
Returns: "E F# G# A B C# D# E"
"F"
Returns: "F G A Bb C D E F"
"F#"
Returns: "F# G# A# B C# D# E# F#"
"Fb"
Returns: "ERROR"
"G"
Returns: "G A B C D E F# G"
"G#"
Returns: "ERROR"
"Gb"
Returns: "Gb Ab Bb Cb Db Eb F Gb"
"D"
Returns: "D E F# G A B C# D"
"G#"
Returns: "ERROR"
"E#"
Returns: "ERROR"
"B#"
Returns: "ERROR"
"C#"
Returns: "C# D# E# F# G# A# B# C#"
"Ab"
Returns: "Ab Bb C Db Eb F G Ab"
"F#"
Returns: "F# G# A# B C# D# E# F#"
"Fb"
Returns: "ERROR"
"C"
Returns: "C D E F G A B C"
"Eb"
Returns: "Eb F G Ab Bb C D Eb"
"D#"
Returns: "ERROR"
"Bb"
Returns: "Bb C D Eb F G A Bb"