// Nik Johnson // 1-7-2024 // CSE 122 import java.util.*; public class Music { public static final String NOTES = "CDEFGAB"; public static final String SHARP = "♯"; public static final String FLAT = "♭"; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String[][] song = composeSong(scanner); mostCommonNaturals(song); System.out.println(Arrays.toString(mostCommonNaturals(song))); } // "parent" method that calls helper methods getSongParams and composeMelodies // returns 2D array containing user-composed song public static String[][] composeSong(Scanner scanner) { int[] songParams = getSongParams(scanner); return composeMelodies(scanner, songParams[0], songParams[1]); } // get user input on song parameters, return for use with composeMelodies public static int[] getSongParams(Scanner scanner) { System.out.print("Enter the number of melodies: "); String inputNum = scanner.nextLine(); int numMelodies = Integer.parseInt(inputNum); System.out.print("Enter the length of each melody: "); String inputLength = scanner.nextLine(); int lengthMelodies = Integer.parseInt(inputLength); return new int[] {numMelodies, lengthMelodies}; } // iterate through array of dimensions defined by list songParams, storing user-inputted notes as we go public static String[][] composeMelodies(Scanner scanner, int numMelodies, int lengthMelodies) { System.out.println(""); String[][] songArray = new String[numMelodies][lengthMelodies]; for (int n = 0; n < numMelodies; n++) { System.out.println("Composing melody #" + (n+1)); for (int i = 0; i < lengthMelodies; i++) { System.out.print(" Enter note #" + (i+1) + ": "); String note = scanner.nextLine(); songArray[n][i] = note; if (numMelodies > 1) { } else if (lengthMelodies > 1) { System.out.println(); } } System.out.println(); } return songArray; } // parent method, use data from helper methods to assemble output array public static String[] mostCommonNaturals(String[][] song) { // new function // string array thats # of melodies long -> since there will be that many notes String[] notes = NOTES.split(""); String[] result = new String[song.length]; for (int i = 0; i < result.length; i++) { // get frequency list PER melody int[] numNaturals = getNumNaturals(song[i]); // get largest frequency # in list int largestFreq = getLargestFrequency(numNaturals); // put note matching that largest frequency (in order of NOTES) into result array // "CDEFGAB"; // [0, 1, 2, 1, 2, 0, 0] // 2 // result = ["E"] for (int n = 0; n < numNaturals.length; n++) { if (numNaturals[n] == largestFreq) { result[i] = notes[n]; break; } } } return result; } // look for frequency of note that appears most frequently in song public static int getLargestFrequency(int[] numNaturals) { int freq = 0; for (int j = 0; j < numNaturals.length; j++) { if (numNaturals[j] > freq) { freq = numNaturals[j]; } } return freq; } public static int[] getNumNaturals(String[] melody) { int[] numNaturals = new int[NOTES.length()]; for (int n = 0; n < melody.length; n++) { String note = melody[n]; if (note.length() == 1) { int index = NOTES.indexOf(note); numNaturals[index]++; } } return numNaturals; } }