cutrightjm
cutrightjm

Reputation: 657

Make a string of letters and numbers?

I've been wanting to write a music program for a while, but I don't really know that much about programming so that probably won't happen. The goal of this program at first would be to represent guitar chords with letter names (Ex: CMaj = CEG) and there could be a few different types of chords (majors, minors, diminished, etc.) To make this the easiest, I think I'm going to start with a C major scale because in that there are no sharps or flats. My first chord would probably be a C major, which is the 1st, 3rd, and 5th of the scale ( C D E F G A B ). My question is, using strings and characters (or some other way, I'm kind of guessing), is there a way to represent C as String(?) C, where C=1, D=2, E=3,..etc. so that when the program inputs for a major chord, it requests the 1st, 3rd and fifth of the respective scale? So basically the user picks the chord C Maj, which converts to 1, 3, 5, and then the scales are matched up and the program calls those 3 specific intervals of that scale.

Any help would be appreciated, or a different way to think about this entirely.

Using what little knowledge I've had, I used Oracles website and found their example of enumeration and put this together:

public enum Chord {
CHORD, MAJOR, MINOR, DIMINISHED, BASS, BASS2

}

public enum Scales {
C, D, E, F, G, A

}

public class EnumTest {
Chord chord;

public EnumTest(Chord chord) {
    this.chord = chord;
}

public void tellItLikeItIs() {
    switch (chord) {
        case MAJOR: System.out.println("C, E, G");
                     break;

        case MINOR: System.out.println("C, Eb, G");
                     break;



        default:     System.out.println("I screwed up");
                     break;
    }
}

public static void main(String[] args) {
    EnumTest firstDay = new EnumTest(Chord.MAJOR);
    firstDay.tellItLikeItIs();
    EnumTest thirdDay = new EnumTest(Chord.MINOR);
    thirdDay.tellItLikeItIs();


}

}

I still don't understand, using Olivers example, how to specify when I make a C major chord that I want the 1st, 3rd, and 5th value of the Scales enum. I like the idea about using the semi-tones from C and building off each chord, but I couldn't figure out how to put the #s into the Scales class (illegal character).. So basically my question is how do I get the Chord class to get the scale degrees from the Scales class and plug it into the EnumTest class?

(If there is somewhere else I can go and ask dumb questions without bugging people I can try asking there =p)

Upvotes: 0

Views: 246

Answers (2)

Olivier
Olivier

Reputation: 3495

Basically, notes, chords and chords progression are imho a combination of enumerations.

I'd go for this :

First, an enum of Note. It's far more easier to think in terms of semi-tones instead of notes.

enum Note { C (1), C# (2), D (3), D# (4), E (5), F (6), F# (7), G (8), G# (8), A (9), A# (10), B (11)}

with the implementation of Note.ordinal() as suggested by mishaddof

Second, i'll have a Chord enum containing all possible Chord Type, that gives the offset of the semi-tones to add to the root note. For a exemple :

enum Chord { MAJOR(4, 7), MINOR (3, 6)}

Finding CMaj is as simple as getting the C Note and getting the Notes that lies 4 (E) and 7 (G) semi-tones further in the Note enum. For G#Maj(8), its C(8+4 =12(-11)=1 then D# (8+7=15-11=4). For Dmin, D(3),F(3+3 =6) and A#(3+7=9)

Upvotes: 2

mishadoff
mishadoff

Reputation: 10789

If you need limited set of values and capability bind some parameters to these values, use Enums.

My first look into this problem:

enum Note {
 C, D, E, F, G, A, B
}

class Chord {
   List<Note> notes;
}

In that case notes list contains your notes for chord in insertion order. If you don't care about order and as i suppose chord can't contain similar notes, use Set for notes holder.

Also, if you need codes use Note.ordinal() that returns code of your note.

Upvotes: 3

Related Questions