import java.util.ArrayList; /** * A class to hold details of audio files. * * @author David J. Barnes and Michael Kölling * @version 2011.07.31 */ public class MusicOrganizer { // An ArrayList for storing the file names of music files. private ArrayList files; /** * Create a MusicOrganizer */ public MusicOrganizer() { files = new ArrayList(); } /** * Add a file to the collection. * @param filename The file to be added. */ public void addFile(String filename) { files.add(filename); } /** * Return the number of files in the collection. * @return The number of files in the collection. */ public int getNumberOfFiles() { return files.size(); } /** * List a file from the collection. * @param index The index of the file to be listed. */ public void listFile(int index) { if(index >= 0 && index < files.size()) { String filename = files.get(index); System.out.println(filename); } } /** * Remove a file from the collection. * @param index The index of the file to be removed. */ public void removeFile(int index) { if(index >= 0 && index < files.size()) { files.remove(index); } } } QUESTION a. Use the music-organizer code above to answer the questions below b. Modify the listFile and removeFile methods so that they print out an error message if the file number entered was out of range. Test both your methods using numbers within and outwith the range of files entered. Test the boundaries carefully e.g. files numbered -1, 0, 1 c. Modify the listFile method so that it prints a number that corresponds to its index in the ArrayList in front of each file. For instance, after adding the 3 files given in part 1, listFile(1) will result in: 1: BlueBoy.mp3 Part d. to be marked at the Week 10 marking session. It is worth 3 practical marks. d. Change listFile method so that the files are numbered starting from 1 rather than 0. Remember that the ArrayList object will still be using indices starting from 0, but you can display the files numbered from 1 in your listing. Make sure you modify the listFile and removeFile methods appropriately i.e. listFile(1) should display the file at index 0 in the ArrayList. So now, after adding the 3 files given in part 1, listFile(1) will result in: 1: DidISay.mp3 Test that your changes work by adding a further file: “BrownBirdSinging.mp3” and then removing files 1 and 2. Check that you now get the following: listFile(0) : index out of range listFile(1) : 1: BlueBoy.mp3 listFile(2) : 2: BrownBirdSinging.mp3 listFile(3) : index out of range
import java.util.ArrayList;
/**
* A class to hold details of audio files.
*
* @author David J. Barnes and Michael Kölling
* @version 2011.07.31
*/
public class MusicOrganizer
{
// An ArrayList for storing the file names of music files.
private ArrayList<String> files;
/**
* Create a MusicOrganizer
*/
public MusicOrganizer()
{
files = new ArrayList<String>();
}
/**
* Add a file to the collection.
* @param filename The file to be added.
*/
public void addFile(String filename)
{
files.add(filename);
}
/**
* Return the number of files in the collection.
* @return The number of files in the collection.
*/
public int getNumberOfFiles()
{
return files.size();
}
/**
* List a file from the collection.
* @param index The index of the file to be listed.
*/
public void listFile(int index)
{
if(index >= 0 && index < files.size()) {
String filename = files.get(index);
System.out.println(filename);
}
}
/**
* Remove a file from the collection.
* @param index The index of the file to be removed.
*/
public void removeFile(int index)
{
if(index >= 0 && index < files.size()) {
files.remove(index);
}
}
}
QUESTION
a. Use the music-organizer code above to answer the questions below
b. Modify the listFile and removeFile methods so that they print out an error message if the file
number entered was out of range.
Test both your methods using numbers within and outwith the range of files entered.
Test the boundaries carefully e.g. files numbered -1, 0, 1
c. Modify the listFile method so that it prints a number that corresponds to its index in the ArrayList in
front of each file.
For instance, after adding the 3 files given in part 1, listFile(1) will result in:
1: BlueBoy.mp3
Part d. to be marked at the Week 10 marking session. It is worth 3 practical marks.
d. Change listFile method so that the files are numbered starting from 1 rather than 0. Remember that
the ArrayList object will still be using indices starting from 0, but you can display the files
numbered from 1 in your listing. Make sure you modify the listFile and removeFile methods
appropriately i.e. listFile(1) should display the file at index 0 in the ArrayList.
So now, after adding the 3 files given in part 1, listFile(1) will result in:
1: DidISay.mp3
Test that your changes work by adding a further file:
“BrownBirdSinging.mp3”
and then removing files 1 and 2.
Check that you now get the following:
listFile(0) : index out of range
listFile(1) : 1: BlueBoy.mp3
listFile(2) : 2: BrownBirdSinging.mp3
listFile(3) : index out of range
Trending now
This is a popular solution!
Step by step
Solved in 2 steps