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!
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)
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.
- 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
- Now you'll add the search functionality:
- 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.
- 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.
- Now run Tunes again and choose the search option. Also try adding
some CDs, then searching for ones that you added.
- 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:
- When you remove a CD, you'll need to update the collection's
total cost. To find the cost of the CD you are removing you will
need to add a getCost method to the CD class. Add this method.
- To remove a CD, you first need to find it. You already have a
findCD method but it just returns a boolean, not the location
of the CD. Write a new method private int findCDIndex(String title)
that takes the title of a CD and returns the index of its location in the
array, or -1 if it is not in the array. Now modify findCD so that
it just calls findCDIndex and returns false if the result is -1,
true otherwise. The body of findCD should now be just one line!
And you can use the findCDIndex in removeCD as well.
- Now you can start writing code for removeCD. First, find the
index of the CD in the array. If it is there (index != -1) you will need to "take it
out," but what does that mean?
You can't leave an empty slot in the middle of the array -- if there
are n CDs in the collection
they should be in array locations 0..n-1, not
0..n with a hole in the middle.
The solution is to
shift over everything that comes after the CD you are removing in the array.
For example, if
there are 10 CDs in the collection and you remove the CD in array location
6, you will have to move the CD in location 7 to 6, the CD in location 8 to 7,
and the CD in location 9 to 8. Think carefully about how to do this; for
this example, you would need a loop where the index i runs from 7 to 9,
copying a[i] to a[i-1] on each iteration.
- Be sure to update the count.
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.
- 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.