Starting from:

$30

CPT120-Tutorial 7 Music Library Solved

1.                Create a constructor and move all of the code from the main method to the constructor and get the code to work as before. When written correctly, the main method should only contain the following two lines:

MusicLibrary ml; ml=new MusicLibrary();

2.                Move the declarations of maxNumSongs, songTitles, songLocations and currentNumSongs from the constructor to the class block and make them private. These will be known as object member variables. E.g.

public class MusicLibrary { private int maxNumSongs; private String[] songTitles; private String[] songLocations; private int currentNumSongs;

Note: You must not have any = (equal) signs in front of the above declarations; All object member initialisations must be performed in the constructor. Also, the word ‘static’ must only be used in the main method’s definition.

3.                In your constructor, add this. (this dot) to the start of any mention of an object member variable name. E.g.

maxNumSongs = 3; Should be changed to:

this.maxNumSongs = 3;

… and so forth for all object member variables (there are many such places).

4.                Create a method each for the three menu items of the application.

5.                Move at least one segment of repeated code to its own method. This method should take one or more parameters and return an appropriate value.


6.  Make the necessary changes to make the constructor accept a parameter specifying the maximum number of songs.

7.  Make the necessary changes so that, when adding a song, if a song by the same title already exists, the user is able to specify a new location for the song. When written correctly, this should not result in duplicated entries for the same song title.

8.  Make the necessary changes so that, when adding a song and if out of room, the arrays are expanded. You must not use ArrayList or similar data structures to achieve this functionality. Ask your group tutors if you are not sure.

More products