From 4a0d047e83141d3ebe48a2bf4ea58e1192a01333 Mon Sep 17 00:00:00 2001 From: pants Date: Mon, 21 Sep 2026 14:16:22 -0700 Subject: init --- Music.java | 132 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 Music.java (limited to 'Music.java') diff --git a/Music.java b/Music.java new file mode 100644 index 0000000..082c5b0 --- /dev/null +++ b/Music.java @@ -0,0 +1,132 @@ +// 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; + } + + +} -- cgit v1.2.3