CPSC 170 Lab 1: More Arrays

As usual, create a lab1 subdirectory for today's lab, open this document in Mozilla, and start emacs.

Arrays of Objects

Write a program to check your answers to question 1 on the prelab. This is easy to do by copying the code from 1(a)-1(e) of the prelab into a single Java program. (Hint: You can copy between any two windows under Gnome by highlighting the text you want to copy with the mouse, going to where you want to put it, and pressing the middle mouse button.) Then change the names of the variables so the first array is namesA, the second is namesB, and so on. This way you are working with five separate arrays. Before you declare each new array, print a message saying which one you are working with (e.g., "Testing array A"). That will make it easier for you to understand the output. Note that if any of the code causes a compiletime error, you will have to comment it out before you can proceed. Similarly, if any of the code causes a runtime error, you will have to comment it out before you can get results for the later parts.

Print your program and write the output from each part on it, including any error messages you get. Don't change the answers on your prelab!

Collecting Tunes

File CDCollection.java contains the code for the CDCollection class (p.337-338) that we discussed in class. File CD.java contains the code for the CD class (p. 340) that CDCollection uses, and Tunes.java contains a slightly modified version of the Tunes class (p. 335) that uses CDCollection. Save these files to your directory.
  1. Compile and run Tunes.java. You should see a list of 6 CDs with a total value of $97.69 and an average cost of $16.28 followed by a menu that lets you add, search for, or remove a CD. Only the add part has been implemented, so add a few CDs to see what happens. When you add the 11th CD (total), you should get a message that says "Increasing size of array" (you may have to scroll back to see it), which comes from the increaseSize() method. I added this just to help you follow the program's execution.

  2. Now you'll add the search functionality:
    1. To find a CD by title you have to be able to get its title. Add a method public String getTitle() to the CD class that returns the title of the CD. Include simple documentation.

    2. Add the findCD method from the prelab to the CDCollection class. Remember our discussion in class, and don't look through the array any longer than you have to. Don't forget the documentation.

    3. Now run Tunes again and choose the search option. Also try adding some CDs, then searching for ones that you added.

    4. Your findCD method has the drawback that if there are two or more CDs with the same title (e.g., "Greatest Hits"), you can't distinguish among them. You could modify findCD to compare both the title and the artist, but it would be nicer to let the CD class decide when two CDs are the same with a boolean equals(CD otherCD) method.
      1. Add this equals method to the CD class, and have it require that both the title and the artist be the same for two CDs to be equal.
      2. Modify your findCD method so that it takes a CD object as a parameter and uses the equals method of the CD class to determine if it is in the collection.
      3. The current CD constructor requires the cost and number of tracks as well as title and artist. There's no need to make the user enters these values when they're not relevant to the search. You could pass dummy values for them, but better yet, add a new constructor to the CD class that takes just the title and artist.
      4. Now modify the Tunes class so that it prompts for both title and artist on the find function, then creates a new CD object (using the new two-parameter constructor) and passes it to findCD.
      Test your modified findCD method thoroughly!

  3. Now you'll add the remove functionality by adding a method public void removeCD(CD cd) to your CDCollection class. The removeCD method should take a CD and remove it from the collection. If it is not found in the collection, the collection should remain unchanged. Proceed as follows:

    Test your removeCD method by running Tunes and choosing the remove option from the menu. Try removing more than one CD, removing a CD that is not in the collection, and mixing removes with searches and adds.

  4. If you remove many CDs, you can end up with an unnecessarily large, nearly empty array. Address this by adding a method private void decreaseSize() that halves the size of the array. In removeCD, after you decrement the count, check to see if it has dropped below 1/3 the size of the current array; if so, call decreaseSize. Put a message at the beginning of decreaseSize like the one at the beginning of increaseSize so you can see when it is being called. Test your program thoroughly.

Be sure you have documented your new methods and updated the documentation on anything you changed.

What to turn in

Turn in hardcopy of Tunes.java, CDCollection.java and CD.java. Tar your lab1 directory and e-mail it to bloss with CPSC170 lab1 in the Subject line.