JamesMadisonUniversity

Computer Science Department


Lab 09-03 (CS239 Lab 22):Fun with Arrays of Objects


Objectives:

  • Be able to declare and instantiate arrays
  • Be able to fill an array using a for loop
  • Be able to access and process data in an array using a for each loop.
  • Be able to "deep copy" elements of an array.
  • Be able to write a sorting method
  • Be able to use an array of objects
  • Learn about the Arrays class
  • Review interfaces

Background:

We are about to begin a section on data structures. One of the first “collections” you have worked with is the single-dimensional array. This lab will provide practice in using single dimensional arrays of objects. Our next topic is multi-dimensional arrays.

New Terms:

No new terms. We will be using the Comparable interface.

Materials:

Song.java
CompactDisc.java
Classics.txt
Lab22_worksheet.doc
Acknowledgment

This lab is adapted from the lab from the Gaddis textbook from Chapter 8.


1 General Instructions:

  1. Download the files Song.java,CompactDisc.java, and Classics.txt from the list above in Materials. Classics.txt is the data file that will be used by CompactDisc.java, one the files you will be editing. For part 2 and 3, Song is complete and will not be edited. For part 4 you will be changing Song.
  2. Your general steps are:

    At each step you will be printing one of the arrays, first the unsorted original, the unsorted copy, the sorted original, then the sorted copy.

2 Create and process an array of objects:

  1. In CompactDisc.java, there are comments indicating where the missing code is to be placed. Do each part separately. When putting your output into the worksheet, put all of the output into the worksheet even that of earlier parts.
  2. Declare and instantiate an array of Song objects, called cd, to be of size 6.
  3. Fill each array element by creating a new Song with the title and artist and storing it in the appropriate position in the array. (The code to read from the file has already been written.
  4. Write statements to print the contents of the array to the console.
  5. Compile Song.java and CompactDisc.java and correct any compiler errors.
  6. Run your application.
  7. What output did you get?

3 Create a copy of your original array so that you can test two different sort methods:

  1. In CompactDisc.java, copy the elements of the cd array to a new array called cd2. NOTE: To make a deep copy you need to copy each element from one array to the other; you cannot simply assign one array to another. WHY ?
  2. What code did you add?
  3. Print the contents of the new array to the console. Test your application for cd2.
  4. What output did you get?

4 Sorting your array of objects (creating your own sort):

  1. Open your Song class.
  2. Add a static void method, sortSongs, which will take in an array of Song objects and will sort the contents first by artist, then title.
  3. Why can this method be void?
  4. Add code to your CompactDisc.java to test this method using the cd array and test.
  5. What output did you get?
  6. Copy your sortSongs method here (including your javadocs).

5 Sorting your array (using Array.sort and the Comparable interface):

  1. The Arrays class in java provides some methods that can be used to process arrays. Before proceeding, review the Java API's related to the Arrays class and specifically the sort method for Objects. In addition, review the API's for the Comparable interface. (http://java.sun.com/j2se/1.5.0/docs/api/)
  2. Implement the Comparable interface for your Song class. (Note, remember the requirements of an interface implementation.) Your method should return a -1 if this object is smaller than the passed object, 0 if they are the same, and 1 if this object is bigger than the passed object. For Song objects, the lexicographic ordering of first the artist String and then the title String will determine order.
  3. Add code to your CompactDisc.java to test this method using the cd2 array and test.
  4. What output did you get?
  5. Copy your Song class header here (including your javadocs).
  6. Copy your Song compareTo method here (including your javadocs).

6 Upload your completed worksheet to Blackboard.


Updated 04/03/2007 (nlh)