Now you'll add the remove functionality. A skeleton for
a method public void removeCD(CD cd) is already in the
CDCollection class. Your job is to complete it so that
it removes the given CD from the collection if it is in there; if not,
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 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,". Remember, 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.
- Don't forget to decrement the count.
You'll also need to add code in Tunes to get information from the user and
call removeCD when they choose the remove option.
Test the remove functionality. Be sure to try removing more than one CD, removing
a CD that is not in the collection, and mixing removes with searches and adds.
NOTE: You have added some extra methods to the CDCollection class.
This means that your javadoc file is out of date. If you have coded things
properly, you should be able to remedy this by simply recreating your javadoc.
(If you didn't add your comments along the way like you are supposed to!
you will need to go do some editing first...
Magic Squares
One interesting application of two dimensional arrays is magic squares.
A magic square is a square matrix in which the sum of every row, every column,
and both diagonals is the same. Magic squares have been studied for many
years, and there are some particularly famous magic squares. In this exercise
you will write code to determine whether a square is magic.
File Square.java contains the shell for a class that
represents a square matrix. It contains headers for a
constructor that takes the size
of the square and a scanner to read from, plus headers for
methods to read values into the square, print the square,
find the sum of a given row, find the sum of a given column,
find the sum of the main (or other) diagonal, and determine whether the square
is magic.
File SquareTest.java contains the shell for a
program that reads input for squares and tells whether each is a magic square.
Following the comments fill in the remaining code, but take it a piece
at a time as follows:
- First fill in code for
the constructor and the printSquare method in the Square class. Then
add code to SquareTest just to read and print
each square. (Note that the other methods in the Square class
have dummy return values so it will compile.)
Compile and run your program with the file
magicData to be sure you're reading
all 7 squares correctly.
- Now add code to compute and print the sums
of the rows and run your program again to make sure this is working correctly.
- Now add code to compute and print the sums of the columns and the sums of the
diagonals, testing as you go.
- Finally write the magic method
and indicate whether each square is magic.
You should find that the
first, second, and third squares in the input are magic, and that the
rest (fourth through seventh) are not. Note that the -1 at the bottom tells
the test program to stop reading.