COM1300 Lab #4

To complete the in-lab problems you should have read up to section 4.13 of your text. You may also
need to refer to the BlueJ tutorial I posted earlier on Angel. Please work on your lab results individually,
but collaborate to resolve operational difficulties with BlueJ or CodingBat.
There is no report template (answer sheet) this week. All you need to turn in to the drop box are your
two modified BlueJ projects. I will examine your projects and run them when grading this lab.
Style matters, too! In addition to checking indentation, naming conventions (variable/method names
start with lowercase, etc.), and brace placement, this week I will start checking finer points like having
spaces around all operators (+, -, ==, <=, etc.) except for ++ and --. Appendix J is our final authority on
style.
The BlueJ projects for this lab are zip files in Angel. Copy each project to your work space, extract it, edit
it and keep your work until you complete the next lab assignment. As before, make sure your Angel
dropbox submission works!
Objectives of this Lab:
 More Java String usage (Codingbat)
 Collecting multiple objects using the ArrayList<> class
 Learning the main methods of the the ArrayList<> class
 Iterating over collections using for-each and while loops
Most of the lab is taken directly from the textbook. You will be working on two different projects,
music-organizer-v2.zip and club.zip. You will be adding code to them, and when you are done, you
should zip up your finished projects and upload them to the drop box. Be sure to test all the methods I
ask you to write. From this lab on, using a fixture it will save you a bunch of time..

Part 1: BlueJ music-organizer-v2 project
Warning: the Audiotracks.zip file is large (compared with files you have been using in lab) You need
about 40MB free just to download and expand it. If you are working on a Lab machine or storing this on
your Z: drive , you may have to clean some work from prior semesters off your Z: drive.
1. Fixture Setup: Download and open the music-organizer-v2 project in BlueJ. Next download the
Audiotracks.zip file to the same directory where you put the music-organizer-v2.zip file and expand
this file as well. Examine the files in the audio directory. (These are all classic public-domain
recordings by pioneers of Southern Blues. ) For several of the subsequent exercises, you will want to
start with a MusicOrganizer object with the first 3 of the 4 songs already loaded into it. This takes
time to set up, so create a version of this and turn it into a fixture, as was shown in the video you
watched before the break.

Creating the fixture:
1.1. Click “New Class” and select a Unit Test Class; name this new class Fixture1.
1.2. Create a MusicOrganizer object on the object bench.

Lab #4 Page 1

3. It should take a single integer parameter index and should return a boolean: true for a valid index and false for an invalid one. It is important to keep the prefix “.e. Specify the name of each file by copying one line from audiotracks. 1. Test this method on a freshly restored Object Bench. do this part later on your laptop. Restore the Object Bench configuration by right-clicking the Fixture1 class and selecting "Test Fixture to Object Bench". Call this method validIndex.. Now call removeFile(0). so let's test the fixture you just built.g. Write a method for the MusicOrganizer class that checks whether an index is valid. For reasons not yet diagnosed. Call listFile(0) and listFile(1) on your music organizer. 2.2. COM1300 Lab #4 1. 4.. Test Audio (optional): It should be fun to make sure your player actually works. Add 3 tracks to the music organizer by calling the addFile method of the music organizer 3 times. but this has not yet been tracked down. Test the Fixture: Software development is at least 1/3 testing. Lab #4 Page 2 . This may seem unremarkable. then restore the bench configuration from the test fixture as in 1. 1. You can cut/paste from this file to save yourself some typing. This will save you some error-prone typing.5. but it's not the way methods normally work. -1. You will do (only) this last step when you need to restore the Object Bench configuration in the future (basically every time you recompile a file and want to test it). Whether the files play or not will not affect your grade. There is some clever code at work.4. Open the file audiotracks. Call startPlaying(0) to start playing the first song in the music organizer through your headphones. this contains a listing of the audio directory.6. Enhance your validIndex method to print an error message if the index is invalid. Right-click on the Fixture1 class and select "Object Bench to Test Fixture". What happened here? Try listFile(0) again. making sure they give you back the first two file names you pasted when you created the fixture. 3. although the song kept on playing in the background. 1. When you have heard enough. An index is valid if it is between 0 and files. The version of Java installed may be the explanation. Make sure it lists what was originally the second file in the organizer. and 2) and invalid (e. 0. 2. Notice that when you called startPlaying. If the previous two steps work.txt in Notepad.. 2.1.txt and pasting it into BlueJ as the parameter for addFile. select "Reset Java Virtual Machine" from the Tools menu to clear the Object Bench. 1.6. so if you don't do carry some with you. it returned almost immediately. Try both valid (i./audio” in front of each file name so that the audio files can be found by Java. but to do this. 2. call stopPlaying(). 3) arguments.size() – 1. 5. you must be using a machine with audio capabilities. inclusive. Most of the lab machines will only work with headphones or earphones.3. playing just does not work on some machines.

called listAllFiles2.g.mp3 1: BlindBlake-EarlyMorningBlues. Hint: I showed how to do this using a for-each loop in one of the Chapter 4 slides. (On Macs. Study this code so that you understand it. make sure that all indices are considered invalid. 8. After you have tested these versions thoroughly. 10. delete auditotracks. They should only call get or remove on the ArrayList if validIndex return true. Challenge: On page 117 is a version of a routine to find the first file that matches a given search string. uploading your project to Angel will be agonizingly slow (if it works at all). Further enhance your listAllFiles so that it prints out how many files are in the organizer before displaying them. COM1300 Lab #4 6.zip may have deleted itself. You may need to use the Windows Explorer or Mac Finder to do this. 7. e.. Write another version of listAllFiles. and you will lose points. 12. Compile and test this to make sure you have a correctly working version. but you will enhance it later. Test this with various numbers of tracks present in the organizer 11.mp3 .mp3 1: BlindBlake-EarlyMorningBlues. make the validIndex method private. 13.. but otherwise works exactly the same.zip and the audio folder from your project... Rewrite both the listFile and removeFile methods in MusicOrganizer so that they use your validIndex method to check their parameter. Before you create your jar file. instead of the current boolean expression.mp3 . 9. Using a fresh MusicOrganizer with no songs stored.: 3 tracks in the Organizer 0: BigBillBroonzy-BabyPleaseDontGo1. You can start with the version on page 108. In the next few steps. except modify it to use a while loop instead of a for-each loop.) Lab #4 Page 3 . you will write a new method listAllFiles to print all tracks in the organizer to the terminal window using a loop. audiotracks. where a match is counted whenever the search string is found anywhere in the file's name. Write a version of this code that uses a for-each loop with a break statement instead of the while loop. This version should be the same as the first version. If you do not remove this folder. Enhance listAllFiles to print the files in the organizer as a numbered list as so: 0: BigBillBroonzy-BabyPleaseDontGo1. but you should still check.

Challenge: Exercise 4. 18. Jar up your project and submit it to the drop box . This is an example of a routine that is best coded using an iterator (Section 4. Jar up your finished club project and submit it to the drop box. Lab #4 Page 4 .42.54.12)! 20. listAllFiles. you will need to complete some of its very basic code. listAllFiles2. Make sure that includes all the work you did: validFiles. Exercise 4. Exercise 4. 15. 16. COM1300 Lab #4 14. The Club project compiles.41 17. This requires you to return a separate collection consisting of only the purged members. Exercise 4.55. and findFirst (the challenge). 19. In this project. Hint: check the slides for Chapter 4 for a similar example. You need to complete the Club class using an ArrayList internally to store club membership information.40. plus the fixture you built. analogous to what was already in place in the MusicOrganizer when you started working with it. Then you will implement some more advanced functionality. You may use any type of loop you wish. Part 2: BlueJ Club project The following exercises refer to the Club project and its associated data. but it is not yet functional. Exercise 4.

Master your semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master your semester with Scribd & The New York Times

Cancel anytime.