Untitled
unknown
java
a year ago
5.3 kB
25
Indexable
import java.util.*;
// Nimai Belur
// 09/30/2024
// CSE 122
// C0: Warm Up
// TA: Cora Supasatit
// This class is a Music Box. It gives the user the power to compose their own song by entering
// different notes and can find some of the most commonly used notes in the song
public class MusicBox {
public static final String NOTES = "CDEFGAB";
public static final String SHARP = "♯";
public static final String FLAT = "♭";
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
String[][] song = composeSong(console);
System.out.println("Returned song 2D array:");
for (int i = 0; i < song.length; i++) {
for (int j = 0; j < song[0].length; j++) {
System.out.print(song[i][j] + " ");
}
System.out.println();
}
}
// Behavior - Asks users to create a song by entering the number of melodies and their length,
// and creates an accordingly sized array
// Exceptions - N/a
// Returns - String[][] - Returns a 2D array comprised of the notes entered by the user
//Paramters - Scanner - Asks user for input about number of melodies and their length
public static String[][] composeSong(Scanner input){
//gets number of melodies from user
System.out.print("Enter the number of melodies: ");
String melodies = input.nextLine();
int numMelodies = Integer.parseInt(melodies);
//gets length of each melody from user
System.out.print("Enter the length of each melody: ");
melodies = input.nextLine();
int lenMelodies = Integer.parseInt(melodies);
System.out.println();
//Creating empty 2D array of notes to be filled with fillBoard
String[][] songArray = new String[numMelodies][lenMelodies];
//calls fillBoard to add in the notes for each song
return fillBoard(input, songArray);
}
// Behavior - Asks users to enter notes and places them into a 2D array
// Exceptions - N/a
// Returns - String[][] - Returns a 2D array comprised of the notes entered by the user
// Paramters - Scanner - Asks user for input about what notes should be entered into the array
// String[][] - Takes in an empty array that has the correct amount of rows for number melodies
// and columns for length of the melodies
public static String[][] fillBoard(Scanner object, String[][] songArray){
for(int i=0; i<songArray.length; i++){
//Shows users which melody is being composed
System.out.println("Composing melody #" + (i+1));
for(int j=0; j<songArray[0].length; j++){
//Shows which note the user is entering
System.out.print(" Enter note #" + (j+1) +": ");
String note = object.nextLine();
//Add note to array
songArray[i][j]=note;
}
System.out.println();
}
return songArray;
}
// Behavior - Finds the most common naturals within the song
// Exceptions - N/a
// Returns - String[] - Returns an array of the most common naturals within each line of the song
// Paramters - String[][] - Takes in a 2D array of notes that make up a song
public static String[] mostCommonNaturals(String[][] song){
//Array has size song.length to represent every line of the song
String[] commonNotes = new String[song.length];
for(int k=0; k<song.length; k++){
int[] noteCounts = countNotes(song[k]);
commonNotes[k] = mostCommonNote(noteCounts);
}
return commonNotes;
}
// Behavior - Counts the number of times each note occurs in the given line of the song
// Exceptions - N/a
// Returns - int[] - Returns an array with the number of times that each note occurs in the line
// Paramters - String[] - Array of notes that make up the line of the song
public static int[] countNotes(String[] songLine){
int[] noteCounts = new int[NOTES.length()];
for(int k=0; k<songLine.length; k++){
//checks if the given note is in NOTES (is a natural note)
if(NOTES.contains(songLine[k])){
//Increments noteCounts according to the position of the given note within NOTES
noteCounts[NOTES.indexOf(songLine[k])]++;
}
}
return noteCounts;
}
// Behavior - Finds the most repeated note within the array of notes
// Exceptions - N/a
// Returns - String - Returns the note that is repeated the most within the array of note Counts
// Paramters - int[] - Array containing the amount of times that each note occurs within a line
public static String mostCommonNote(int[] noteCounts){
int max=0;
int maxIndex=0;
for(int i=0; i<noteCounts.length; i++){
//Finds the maximum count and the corresponding index
if(noteCounts[i]>max){
max=noteCounts[i];
maxIndex=i;
}
}
//Uses maxIndex to return the note that corresponds to the max index
return NOTES.substring(maxIndex, maxIndex+1);
}
}Editor is loading...
Leave a Comment