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

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY