At this point I have decided that I have enough requirements to start building a few objects. The aim is to be able to allow the creation of the first generation.
Chord |
- Notes[ ]: int |
+ Chord( ): void |
Melody |
- MelodyChords[ ]: Chords |
+ Melody( ): void |
To test the classes I created another class called Tester with only a main procedure.
See the code below:
/**
* @author Jean-Louis Lamacchia
* @Date 19/06/2011
* Chord is a set of distinct musical notes.
*/
public class Chord {
private int ChordLength;
public int[] Notes;
public Chord (){
// Default length is 3 notes
ChordLength = 3;
Populate();
}//END OF Chord default constructor
public Chord (int NumberOfNotes){
ChordLength = NumberOfNotes;
Populate();
}//END OF Chord (NumberOfNotes)
public void Populate(){
// populate array of Notes with random int values
// between 1 and 11.
// Ensure that we do not have any duplicate
Notes = new int[ChordLength];
int newValue = (int)(Math.random()*11)+ 1;
Notes[0] = newValue;
boolean same;
for (int i=1 ; i<ChordLength; i++){
do {
// Look for duplicates
same = false;
newValue = (int)(Math.random()*11)+ 1;
for (int j=0; j<i ;j++){
if (Notes[j]== newValue){
same = true;
}
}
}while (same == true);
// No duplicate
Notes[i] = newValue;
}// End of loop
}// END OF Populate
//-- Generate a String value for the Chord
public String ToString(){
String StringOutput = "";
for (int i=0 ; i<ChordLength; i++){
if (i == ChordLength -1){
// Last element, append String value of note and no comma
StringOutput = StringOutput.concat(String.valueOf(Notes[i])) ;
}else{
// Next element, append String value of note and comma
StringOutput = StringOutput.concat(String.valueOf(Notes[i]) + ",";
}
}// End of loop
return StringOutput;
}// END OF ToString
public void Mutate(){
// To come: Random mutation of the Chord
}
public int GetLength(){
// Return the number of notes in this Chord
return ChordLength;
}
}
/**
* @author Jean-Louis Lamacchia
* @date 19/06/2011
*/
public class Melody {
private int MelodyLength;
public Chord[] MelodyChords;
public Melody(){
MelodyLength = 12;
Populate();
}//END OF Chord default constructor
public void Populate(){MelodyChords = new Chord[MelodyLength];
for (int i = 0; i<MelodyLength;i++){
MelodyChords[i] = new Chord();
}
}// End of Populate()
public String ToString(){String StringOutput= "";
for (int i=0; i<MelodyLength;i++){
if (i == MelodyLength -1){
StringOutput = StringOutput.concat(MelodyChords[i].ToString();
}else{
StringOutput = StringOutput.concat(MelodyChords[i].ToString() + ",");
}
}
return StringOutput;
}//End of ToString()
public int GetMelodyLength(){
return MelodyLength;
}
}
public class Tester {
public static void main(String[] args) {
//Chord testChord = new Chord();
//System.out.println(testChord.ToString());
Melody myMelody = new Melody();
System.out.println("To String: "+ myMelody.ToString());
System.out.println("There are " + myMelody.GetMelodyLength() +" chords in this Melody.");
System.out.println("Each Chord has " + myMelody.MelodyChords[0].GetLength() + " Notes");
for (int c =0; c<myMelody.GetMelodyLength();c++){
System.out.println("Chord " + c + " ---");
for (int n = 0; n<myMelody.MelodyChords[0].GetLength();n++){
System.out.print("Note " + n + "->" + String.valueOf(myMelody.MelodyChords[c].Notes[n]));
}
System.out.println("");
}
System.out.println("-- END OF Tester--");
}// End of main
}
Output:
To String: 8,2,10,7,2,1,3,10,6,2,1,10,6,1,4,4,7,9,1,2,3,8,11,5,9,3,4,1,9,8,7,4,9,5,10,1
There are 12 chords in this Melody.
Each Chord has 3 Notes
- Chord 0 ---
Note 0->8 Note 1->2 Note 2->10
- Chord 1 ---
Note 0->7 Note 1->2 Note 2->1
- Chord 2 ---
Note 0->3 Note 1->10 Note 2->6
- Chord 3 ---
Note 0->2 Note 1->1 Note 2->10
- Chord 4 ---
Note 0->6 Note 1->1 Note 2->4
- Chord 5 ---
Note 0->4 Note 1->7 Note 2->9
- Chord 6 ---
Note 0->1 Note 1->2 Note 2->3
- Chord 7 ---
Note 0->8 Note 1->11 Note 2->5
- Chord 8 ---
Note 0->9 Note 1->3 Note 2->4
- Chord 9 ---
Note 0->1 Note 1->9 Note 2->8
- Chord 10 ---
Note 0->7 Note 1->4 Note 2->9
- Chord 11 ---
Note 0->5 Note 1->10 Note 2->1
Next stage will be play a Melody ...