Lab 1 In-Class: More Arrays

As usual, create a lab1 subdirectory for today's lab, open this document in Netscape, 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 notice a message that says "Increasing size of array," 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 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.

    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.

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

  3. Now you'll add the remove functionality by adding a method public void removeCD(String title) to your CDCollection class. The removeCD method should take a String and remove the CD with that title from the collection. If no such CD is 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. Be sure you try removing more than one CD as well as searching and adding after you remove.

  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.

What to turn in

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