Sunteți pe pagina 1din 4

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
COM1300 Lab #4

1.3. Open the file audiotracks.txt in Notepad; this contains a listing of the audio directory. You can
cut/paste from this file to save yourself some typing. It is important to keep the prefix
../audio in front of each file name so that the audio files can be found by Java.
1.4. Add 3 tracks to the music organizer by calling the addFile method of the music organizer 3
times. Specify the name of each file by copying one line from audiotracks.txt and pasting it into
BlueJ as the parameter for addFile. This will save you some error-prone typing.
1.5. Right-click on the Fixture1 class and select "Object Bench to Test Fixture".
1.6. Restore the Object Bench configuration by right-clicking the Fixture1 class and selecting "Test
Fixture to Object Bench". 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).

2. Test the Fixture: Software development is at least 1/3 testing, so let's test the fixture you just built.

2.1. Call listFile(0) and listFile(1) on your music organizer, making sure they give you back the first
two file names you pasted when you created the fixture.
2.2. Now call removeFile(0). What happened here? Try listFile(0) again. Make sure it lists what was
originally the second file in the organizer.
2.3. If the previous two steps work, select "Reset Java Virtual Machine" from the Tools menu to clear
the Object Bench, then restore the bench configuration from the test fixture as in 1.6.

3. Test Audio (optional): It should be fun to make sure your player actually works, but to do this, you
must be using a machine with audio capabilities. Most of the lab machines will only work with
headphones or earphones, so if you don't do carry some with you, do this part later on your laptop.

Call startPlaying(0) to start playing the first song in the music organizer through your headphones.
When you have heard enough, call stopPlaying(). Notice that when you called startPlaying, it
returned almost immediately, although the song kept on playing in the background. This may seem
unremarkable, but it's not the way methods normally work. There is some clever code at work.

For reasons not yet diagnosed, playing just does not work on some machines. The version of Java
installed may be the explanation, but this has not yet been tracked down. Whether the files play or
not will not affect your grade.

4. Write a method for the MusicOrganizer class that checks whether an index is valid. Call this method
validIndex. It should take a single integer parameter index and should return a boolean: true for a
valid index and false for an invalid one. An index is valid if it is between 0 and files.size() 1,
inclusive. Test this method on a freshly restored Object Bench. Try both valid (i.e., 0, 1, and 2) and
invalid (e.g., -1, 3) arguments.

5. Enhance your validIndex method to print an error message if the index is invalid.

Lab #4 Page 2
COM1300 Lab #4

6. Using a fresh MusicOrganizer with no songs stored, make sure that all indices are considered invalid.

7. Rewrite both the listFile and removeFile methods in MusicOrganizer so that they use your validIndex
method to check their parameter, instead of the current boolean expression. They should only call
get or remove on the ArrayList if validIndex return true. After you have tested these versions
thoroughly, make the validIndex method private.

8. In the next few steps, you will write a new method listAllFiles to print all tracks in the organizer to
the terminal window using a loop. You can start with the version on page 108, but you will enhance
it later. Compile and test this to make sure you have a correctly working version.

9. Enhance listAllFiles to print the files in the organizer as a numbered list as so:

0: BigBillBroonzy-BabyPleaseDontGo1.mp3
1: BlindBlake-EarlyMorningBlues.mp3
...

Hint: I showed how to do this using a for-each loop in one of the Chapter 4 slides.

10. Further enhance your listAllFiles so that it prints out how many files are in the organizer before
displaying them, e.g.:

3 tracks in the Organizer

0: BigBillBroonzy-BabyPleaseDontGo1.mp3
1: BlindBlake-EarlyMorningBlues.mp3
...

Test this with various numbers of tracks present in the organizer

11. Write another version of listAllFiles, called listAllFiles2. This version should be the same as the first
version, except modify it to use a while loop instead of a for-each loop.

12. Challenge: On page 117 is a version of a routine to find the first file that matches a given search
string, where a match is counted whenever the search string is found anywhere in the file's name.
Study this code so that you understand it. Write a version of this code that uses a for-each loop with
a break statement instead of the while loop, but otherwise works exactly the same.

13. Before you create your jar file, delete auditotracks.zip and the audio folder from your project. You
may need to use the Windows Explorer or Mac Finder to do this. If you do not remove this folder,
uploading your project to Angel will be agonizingly slow (if it works at all), and you will lose points.
(On Macs, audiotracks.zip may have deleted itself, but you should still check.)

Lab #4 Page 3
COM1300 Lab #4

14. Jar up your project and submit it to the drop box . Make sure that includes all the work you did:
validFiles, listAllFiles, listAllFiles2, and findFirst (the challenge), plus the fixture you built.

Part 2: BlueJ Club project

The following exercises refer to the Club project and its associated data. The Club project compiles, but it
is not yet functional. You need to complete the Club class using an ArrayList internally to store club
membership information. In this project, you will need to complete some of its very basic code,
analogous to what was already in place in the MusicOrganizer when you started working with it. Then
you will implement some more advanced functionality.

15. Exercise 4.40.

16. Exercise 4.41

17. Exercise 4.42.

18. Exercise 4.54. You may use any type of loop you wish.

19. Challenge: Exercise 4.55. This requires you to return a separate collection consisting of only the
purged members. Hint: check the slides for Chapter 4 for a similar example. This is an example of a
routine that is best coded using an iterator (Section 4.12)!

20. Jar up your finished club project and submit it to the drop box.

Lab #4 Page 4

S-ar putea să vă placă și