These concepts are introduced in this lab. By the end of this lab you should be able to identify examples of each concept, and correctly use these terms, and use them in your code
In your repository, there is already a file located at "src/allSongs.txt"
💻 In the Melodify class, write a method readPostsFromFile() that reads all Song objects from the text file allSongs.txt into this.allSongs.Hint: reference lab_posts for an example of how to do this
Here is a reference of how to parse the different types of data in allSongs.txt:
Stringtitle=data[x];// read in a String from location x
intpopularity=Integer.parseInt(data[x]);// read in an Int from location x
booleanisExplicit=Boolean.parseBoolean(data[x]);// read in an boolean from location x
doublelength=Double.parseDouble(data[6]);// read in an double from location x
💻 Write a new method sortPopularity() that sorts allSongs by popularity score in descending order (most popular first). This method has access to allSongs so it does not need any parameters or to return anything.
💻 Write a new method getSongByTitle(String target) that finds a song with the given title, and returns that song. If it cannot find a song that matches, it should return null.
💻 In the main method of Melodify, test this method to be sure it works.
💻 Write a new method getSongByPopularity(double target) that finds a song with the given popularity, and returns that song. If it cannot find a song that matches, it should return null. Remember that binary search only works on sorted arrays, so be sure to call one of your sorting methods first.
💻 In the main method of Melodify, test this method to be sure it works.
This time, you’ll practice using a do...while loop. It’s very similar to a while loop, but you state the action first, before stating this condition, which ensures it will always run at least one time. Here’s an example:
💻 Write a new method getSongByValence(double target) that finds a song with the given valence, and returns that song. Use the do...while loop. If it cannot find a song that matches, it should return null. Remember that binary search only works on sorted arrays, so be sure to call one of your sorting methods first.
💻 In the main method of Melodify, test this method to be sure it works.
💻 Write a new method getRandomSong() that returns a random Song.
Here’s how to randomly generate a number 0-9:
Randomrand=newRandom();// you only need to run this once
intrandomIndex=rand.nextInt(10);//each time you need a new random number, run this line of code
Binary Search is a method that is commonly made recursive. In addition to the target value, it also takes the array, and the left and right indices as parameters.
💻 Create a new method getSongByEnergy(Song[] arr, int left, int right, double target) that uses recursive binary search to find the target song.
💻 Read in listening data. You can use teacher data, or data from Lawrence, Brendan, or Kenneth if they’re feeling generous.
💻 Try generating a “Melodify Wrapped” for them Some ideas:
Calculate their average energy and valence levels, and their most popular genre to give them their label, e.g. “Pink Pilates Princess”, “Indie Sleaze”, etc.