Make a program the takes 3 notes and builds the possible chords out of the combi
ID: 3905857 • Letter: M
Question
Make a program the takes 3 notes and builds the possible chords out of the combination of the three notes, including the root.
Description:
Most Western music is based on the chromatic scale: a series of 12 pitches, represented as notes, each a semitone apart. Notes are named with the letters A-G. Some notes are named just a letter, while some have a suffix called an “accidental” meaning a semitione above or below the note with the letter-name.
Starting from A, the 12 tones can be named
0
1
2
3
4
5
6
7
8
9
10
11
With #'s
A
A#
B
C
C#
D
D#
E
F
F#
G
G#
With b' s
A
Bb
B
C
Db
D
Eb
E
F
Gb
G
Ab
Other names
Cb
B#
Fb
E#
The difference between two successive notes is called a half-step. The order of notes is cyclic. That is, the note one half-step higher than G#/Ab is again A, and the note one half-step lower than A is Ab/G#. Notes that are a multiple of 12 half-steps apart have the same name, and for our purposes we will consider them equivalent.
Each pitch may have multiple names: A# is the same as Bb, C# is the same as Db, etc. The alternate names of a pitch is called an enharmonic.
Intervals
The distance between two notes is called an interval. The names of the intervals are:
Distance in Half-Steps
Name of Interval
0
Perfect unison
1
Minor second
2
Major second
3
Minor third
4
Major third
5
Perfect fourth
6
Diminished fifth
7
Perfect fifth
8
Augmented fifth
9
Major sixth
10
Minor seventh
11
Major seventh
Chords
A chord in music is made of three or more notes that is heard as if sounding simultaneously. A three-note chord, called a triad is named made from a 7-note subset of the chromatic scale, called the diatonic scale. Each note is mapped to each of the letters A-G with potentially an accidental.
Chord Spelling
Chords are “spelled” by identifying the notes. For example
G B D
C Eb G
B D# G
Note: (no pun intended) the note might not be in order, that is, the first note might not be the root of the chord.
Chord Quality
The chord quality is determined by the interval between the root and the third note of the diatonic scale. When the third is 4 semi-tones from the root, it's a major chord; if it is 3 semi-tones, it is a minor chord.
The Altered 5th
The distance between the root and the fifth note of the diatonic scale is normally 7 semi-tones from the root. If the fifth is a semi-tone higher the chord is augmented, if a semi-tone lower, it is diminished.
Chord Name
A chord is named based on
The root note (e.g. “C”)
The chord quality(e.g. major or minor).
The alteration of fifth note of the diatonic scale (e.g. diminished or augmented).
The chord name can be determined two ways:
Method 1: by measuring the interval between the root and third, and then the interval between the third and fifth or,
Method 2: by measuring the interval between the root and third, then the interval between the root and the fifth.
Method 1
Method 2
Root-->Third
Third-->Fifth
Chord Name
Root-->Third
Root-->Fifth
Major third
Minor third
Major (“maj”)
Major third
Perfect fifth
Minor third
Major third
Minor (“min”)
Minor third
Perfect fifth
Major third
Major third
Augmented (“aug”)
Major third
Augmented fifth
Minor third
Minor third
Diminished (“dim”)
Minor third
Diminished fifth
Assignment:
Create a class named ChordFinder with the following features:
Feature
Signature
Requirement
Constructors
N/A
Will be a utility class—with just a static public method
Methods
static Set getChordName(String notes)
Input: Valid input must have exactly three discrete notes separated by whitespace
Accidentals will use “#” for sharp and “b” for flats.
too many or too few notes should throw an IllegalArgumentException
an invalid note, e.g. “Zb”, should throw an IllegalArgumentException
Output: A set that
containing the names of possible chords names of the given notes
The format must be where
is the root note, “A” – “G” optionally followed by a “b” or “#” representing a sharp or flat respectively. The note must be upper-case
a space
is one of “maj”,“min”,“aug”,“dim”; the name must be lower case
Precondition: Notes are recognized (character A-Goptionally followed by a ‘b’ for flats or ‘#’ for sharps).
Note: while double sharps and flats exist, they are not within scope.
Example Test Cases
Scenario
Expected results
getChordName("D G B")
[“G maj”]
Root : G
Interval G-->B : major third
Interval B-->D: minor third
getChordName(“C Eb G” )
[“C min”]
Root: C
Interval C--> Eb : minor third
Interval C--> G : perfect fifth
getChordName(“B D# G”)
[“B aug”, “D# aug”, “G aug”]
Roots : B, D#, or G
Intervals (B—>D#, D#-->G, and G-->B) : major third
Intervals (B-->G , D#-->B, G-->D#) : augmented fifth
0
1
2
3
4
5
6
7
8
9
10
11
With #'s
A
A#
B
C
C#
D
D#
E
F
F#
G
G#
With b' s
A
Bb
B
C
Db
D
Eb
E
F
Gb
G
Ab
Other names
Cb
B#
Fb
E#
Explanation / Answer
Answer: See the code below:
----------------------------------------------------------
package cordfinderdemo;
import java.util.HashSet;
import java.util.Set;
/**
* Game class
*
*/
public class CordFinder {
static String[] tonesWithHash = {"A","A#","B","C","C#","D","D#","E","F","F#","G","G#"};
static String[] tonesWithB = {"A","Bb","B","C","Db","D","Eb","E","F","Gb","G","Ab"};
static String[] tonesOtherNames = {"","","Cb","B#","","","","Fb","E#","","",""};
static String[] intervals = {"Perfect unison","Minor second",
"Major second","Minor third","Major third","Perfect fourth",
"Diminished fifth","Perfect fifth","Augmented fifth","Major sixth",
"Minor seventh","Major seventh"};
/**
* getCordName() method
*/
public static Set getCordName(String notes) {
Set set = new HashSet();
//individual notes
String[] noteTokens = notes.split(" ");
//check if right number of notes are passed
if (noteTokens.length < 3 || noteTokens.length > 3)
throw new IllegalArgumentException("Wrong number of notes");
return set;
// TODO Auto-generated constructor stub
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(getCordName("D G B").toString());
}
}
--------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.