summaryrefslogtreecommitdiff
path: root/Music.java
blob: 082c5b0a287f8bcb9faaafd2a9e84b761df2c042 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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;
    }


}