/************************************************/ /*Eprog Runde 3, BSP.# 3146 - Akkord.java */ /*by Ernst Schwartz, Mat.# 0004444 */ /*mail: Ernst_Schwartz@gmx.at */ /************************************************/ import eprog.*; public class Akkord extends EprogIO { /******Statische Variablen******/ static String[] differences = { "Prim", "kleine Sekund", "grosse Sekund", "kleine Terz", "grosse Terz", "kleine Quart", "grosse Quart", "Quint", "kleine Sext", "grosse Sext", "kleine Sept", "grosse Sept", "Oktave" }; static int[] HTS_Ton = { 0, 0, 0 }; static boolean Error = false; static String Ausgabe; /******Assign-Methode******/ public static void Assign (String Ton1, String Ton2, String Ton3) { int errorcount = 0, i, j; String[] Notenwerte = {"C", "Cis", "D", "Dis", "E", "F", "Fis", "G", "Gis", "A", "Ais", "H"}; String[] Toene = { Ton1, Ton2, Ton3 }; for (j = 0; j < 3; j++) { errorcount = 0; for (i = 0; i < 12; i++) { if (Toene[j].equals(Notenwerte[i])) { HTS_Ton[j] = i; continue; } else errorcount++; if (Toene[j].equals(Notenwerte[i].toLowerCase())) { HTS_Ton[j] = i + 12; continue; } else errorcount++; if (Toene[j].equals(Notenwerte[i].toLowerCase() + "\'")) { HTS_Ton[j] = i + 24; continue; } else errorcount++; if (Toene[j].equals(Notenwerte[i].toLowerCase() + "\'\'")) { HTS_Ton[j] = i + 36; continue; } else errorcount++; if (Toene[j].equals(Notenwerte[i].toLowerCase() + "\'\'\'")) { HTS_Ton[j] = i + 48; continue; } else errorcount++; } if (errorcount == 60 || HTS_Ton[0] > 51) //errorcount wird nur dann 60, wenn keine der obrigen if-Anweisungen "greift", //und das ist nur dann der Fall wenn die Eingabe falsch war (also kein Ton), //...oder der erste Ton ist höher als dis''' { Error = true; //Error wird auf true gesetzt... continue; } } } public static int SmallestDifference (int HTS_Ton0, int HTS_Ton1, int HTS_Ton2) { int dif1 = Math.abs(HTS_Ton0 - HTS_Ton1); //die drei Intervalle werden errechnet int dif2 = Math.abs(HTS_Ton0 - HTS_Ton2); //... int dif3 = Math.abs(HTS_Ton1 - HTS_Ton2); //... if ( (dif1 <= dif2) && (dif1 <= dif3) ) //Dif1 ist der kleinste { return dif1; } else if ( (dif2 <= dif1) && (dif2 <= dif3) ) //Dif2 ist der kleinste { return dif2; } else if ( (dif3 <= dif1) && (dif3 <= dif2) ) //Dif3 ist der kleinste { return dif3; } else //falls aus irgendeinem Grund etwas anderes rauskommt...einen Standardwert zurückgeben { return 99; } } public static int getFrequency (int input) { int[] frequencies = //alle Frequenzen der Töne C (frequencies[0]) bis h''' (frequencies[59]) { 65 , 69 , 73 , 78 , 82 , 87 , 92 , 98 , 104 , 110 , 117 , 123 , 130 , 138 , 146 , 156 , 164 , 174 , 184 , 196 , 208 , 220 , 234 , 264 , 260 , 276 , 292 , 312 , 328 , 348 , 368 , 392 , 416 , 440 , 468 , 528 , 520 , 552 , 584 , 624 , 656 , 696 , 736 , 784 , 832 , 880 , 936 , 1056 , 1040 , 1104 , 1168 , 1248 , 1312 , 1392 , 1472 , 1568 , 1664 , 1760 , 1872 , 2112 , }; return frequencies[input]; //die Frequenz zurückgeben, die an der angegebenen Stelle steht. } public static void main (String args[]) { String Ton1 = readWord(); //Drei Strings (Töne) einlesen String Ton2 = readWord(); //.. String Ton3 = readWord(); //.. Assign(Ton1, Ton2, Ton3); int DurDreiklangFrequency0 = getFrequency(HTS_Ton[0]); //die Frequenzen des Dur-Dreiklangs berechnen int DurDreiklangFrequency1 = getFrequency(HTS_Ton[0] + 4); //... int DurDreiklangFrequency2 = getFrequency(HTS_Ton[0] + 7); //... if (SmallestDifference(HTS_Ton[0], HTS_Ton[1], HTS_Ton[2]) > 12) //wenn der kleinste Intervall größer als 12 ist... Error = true; //wird Error auf true gesetzt if (!Error) { Ausgabe = differences[SmallestDifference(HTS_Ton[0], HTS_Ton[1], HTS_Ton[2])] + " " + DurDreiklangFrequency0 + " " + DurDreiklangFrequency1 + " " + DurDreiklangFrequency2; } else { Ausgabe = "FALSCHE EINGABE"; } println(Ausgabe); } }