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!
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 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
- Now you'll add the search functionality:
- 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.
- 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.
- Now run Tunes again and choose the search option. Also try adding
some CDs, then searching for ones that you added.
- 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.
Test your modified findCD method thoroughly!
- 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.
- 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.
- 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.
- 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
- 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:
- 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(CD cd)
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.
- Don't forget to decrement the count.
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.
- 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.