Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Have a look at this method, in the M3UReader class: public static boolean isVali

ID: 672235 • Letter: H

Question

Have a look at this method, in the M3UReader class: public static boolean isValidHeader(String filename) The specification for this method is as follows: isValidHeader should return true if the first line of the file named filename starts with ‘#EXTM3U’, and false otherwise. It should also return false if the file can’t be opened for reading. Complete the implementation of this method. Currently, the method: - Uses filename to try to open a file, - Reads the first line of the file into a String variable called firstLine. - Compares the start of firstLine withthe String “#EXTM3U” Your task is to set the returnVal so that the method correctly implements the above specification. (A final static String field called M3UHeader is declared and set as #EXTM3U – this is a more organized way of storing this string in your program. You shouldn’t need to change this.) There is a working Unit Test for this method in the ‘test’ folder. You can run this test by choosing “Run -> Test Project (M3UTools)” (or, if you just want to test this file in future, select M3UReaderTest in the left-hand side and choose Run->Run File. When you have completed this method, you should see the ‘Test Results’ folder show that the test has been passed. Note 1: There is a slightly interesting discussion to be had about the error that gets printed out, even when the test is run successfully: isValidHeader:: error with file test-data estIsValidHeader oSuchFile.m3u This is printed out by the method, to the ‘error stream’, when it (understandably) can’t open the file. - Arguably this is useful behaviour, even though it isn’t part of the specification: an error has been found (and the unit tests are designed to probe these ‘edge cases’). - Just because this error is printed out, it doesn’t mean there is an error in your solution Note 2: The Unit Test should work on both Windows and Mac, using the file separator ‘/’. We will soon encounter formal ways of making file paths that are operating-system neutral. Question 2: The next task is to write a method for the M3UReader class that counts how many tracks are included in an M3U file. Its signature looks like this: public static int getNumberOfTracks(String filename) and is already included in the class. Its specification is as follows: getNumberOfTracks should return the total number of tracks listed in the playlist file, or 0 if the file doesn’t exist, or doesn’t have a valid header string. 2.1)Before you try to implement this method, first write its test method. (This is called ‘test-driven development’). NetBeans will generate the test if you ask it: with the M3UReader class selected on the left hand side class browser, choose ‘Tools -> Create Tests’ (either from the top menu or the right-click context menu). In the Create Tests Dialog Box, keep everything selected and hit ‘ok’. You then need to edit the method testGetNumberOfTracks, inside the TestM3UReader class. We’ve added three ‘test artefacts’ that you can use to help you write this method: - noSuchFile.m3u (return value should be 0) - zero_tracks.m3u (return value should be 0) - three_tracks.m3u (return value should be 3) - four_tracks.m3u (return value should be 4) - five_tracks.m3u (return value should be 5) Inside testGetNumberOfTracks, you can specify these filenames, to test the getNumberOfTracks method returns the right number correctly in each case. NB pay attention to the ‘TODO’ that is listed in the automatically generated test! 2.2)Now you can write getNumberOfTracks. After you have opened a BufferedReader, you can use a loop like this to read in each line: String nextLine; while((nextLine=br.readLine())!= null) { // you have to work out what to do in here } Once you have written the method, you can test it by running the tests for this class (Tools -> Run Tests) Question 3: Add the following method to the M3UReader class: public static int getTotalSeconds(String playList) getTotalSeconds should return the total number of seconds listed in the lines of meta-data in the playlist, or 0 if the file doesn’t exist, or is invalid. Here is a code fragment that may be useful, for extracting the duration of a track from a line of metadata, stored in the string nextLine int i1= nextLine.indexOf(":"); int i2 = nextLine.indexOf(","); String substr = nextLine.substring(i1+1, i2); int seconds = Integer.parseInt(substr); Add a unit test for this method. Some suggested test files are in the folder test-data estGetTotalSeconds. Question 4: Add the following method to the M3UReader class: public boolean copy( String inputFilename, String outputFilename, int numberOfSeconds) The copy method should create a valid playlist with the outputPlayList filename. It should contain the same tracks as the inputPlayList, in the same order and with the same meta-data, but the total number of minutes should not exceed numberOfSeconds, i.e. the output list should be finished if the inclusion of the next track would exceed this value. The method should return true unless the either input or output file can’t be opened, in which it returns false. What would be a good unit test could you write for this method? Hint: you can use the getTotalSeconds method from within your unit test. Demonstrate the copy method from the main method for the M3UReader class, that loads in a playlist.m3u from the test-data folder, and copies up to a given number of seconds, into an output

Explanation / Answer

CODE :

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote