15-121 SPRING 2010 [CORTINA]

HOMEWORK 2 - due Tuesday, January 26

WRITTEN PROBLEMS (10 pts)

For each of the following problems, write up your answers on paper (or use a word processor). Remember that the answers you write or type MUST be your own.

Use the phone directory program discussed in class to answer problems 1-4.

  1. (1 pt) Modify the lookupEntry method so that if the name is found in the phone directory, it is moved to the first position in the directory array, shifting all prior entries over one position to make room for the matching entry in position 0 of the array.

  2. (1 pt) Recall that the phone directory in class started with an empty array with 10 cells. If we start with an empty phone directory and add 280 phone entries and then remove 100 phone entries, how large is the phone directory array? Explain.

  3. (1 pt) In the removeEntry method, why can't we just set the array cell that contains the reference to the matching entry to null and subtract one from numEntries? Give an example that shows how this will cause the application to malfunction subsequently.

  4. (2 pts) Suppose we want to add a new option to the application that allows us to get the last phone number in the phone directory. Show the new code that is needed and the code that needs to be changed to add this new functionality. For each addition or change, indicate which class it occurs in.

  5. (1 pt) What is the advantage of using a Java interface to specify the behavior of a class rather than just implementing the behavior as a class directly?

  6. (1.5 pts) What is the difference between a checked exception and an unchecked exception? Which type of exception does the compiler force us to deal with in our code? Give an example of each from the examples shown in class.

  7. (1.5 pts) Consider the following simple search method that returns the index of the first occurrence of the desired target if it is found and -1 if not found:

    public int find(int[] data, int target) {
    	// code not shown
    }
    

    List a set of test conditions to use to test this method as completely as possible.

  8. (1 pt) Write a set of Javadoc comments to document the method in the previous problem.

PROGRAMMING PROJECT (10 pts)

A DVD movie consists of a title, a rating (e.g. PG, R, etc.), and a running time in minutes.

In this project, you will create an application to allow the user to maintain a collection of DVD movies using an array.

When the application starts up, it will read in the DVD data from a text file to initialize the array, if the file is available. If the file is not there, then the program starts with an empty array. If the file is corrupted (has an invalid or missing value), then the program stops its initialization at the point of the error. The data file should contain one DVD per line, with title followed by rating followed by running time in minutes, all separated by commas. You may assume titles are all in uppercase and do not contain commas. For example:

ANGELS AND DEMONS,PG-13,138
STAR TREK,R,127
UP,PG,96

The titles may not be in alphabetical order, but the DVDs should be inserted into the array in alphabetical order.

The application will then allow its user to perform the following operations:

Assignment

Download the Program2.zip project file. This file contains six Java files for this assignment. The organization of this project is very similar to the example that is discussed in lecture.

Java Files

Your assignment is to complete the first two classes (DVD and DVDCollection) to create a correctly functioning application.

Complete the DVD class by completing the given methods (constructor, accessors and mutators). Add javadoc comments so you can generate a javadoc documentation file showing how to use this class.

The DVDCollection class uses an array to maintain the collection of DVDs. The DVDs should be stored in alphabetical order based on title starting at position 0 in the array, using adjacent cells. All titles should be stored in uppercase only and are assumed to be unique (no duplicates). You may assume titles do not contain commas. Complete the following methods in the DVDCollection class in the order shown below and test each one before moving on.

toString - returns a string containing all of the DVDs in the collection, separated by newlines characters, along with the value of numdvds and the length of the DVD array for debugging purposes. The string should be formatted as shown in the example below:

numdvds = 3
dvdArray.length = 7
dvdArray[0] = ANGELS AND DEMONS/PG-13/138min
dvdArray[1] = STAR TREK/R/127min
dvdArray[2] = UP/PG/96min

addOrModifyDVD - given the title, rating and running time of a DVD, add this DVD to the collection if the title is not present in the DVD collection or modify the DVD's rating and running time if the title is present in the collection. Do this operation only if the rating and running time are valid. If a new DVD is added to this collection, insert the DVD so that all DVDs are in alphabetical order by title. (NOTE: The collection should already be in alphabetical order when this method is called since this is the only method available to insert DVDs.)

removeDVD - given the title, this method should remove the DVD with this title from the collection if present. The title must match exactly (in uppercase). If no title matches, do not change the collection.

getDVDsByRating - given the rating, this method should return a string containing all DVDs that match the given rating in the order that they appear in the collection, separated by newlines.

getTotalRunningTime - this method should return the total running time of all DVDs in the collection. If there are no DVDs in the collection, return 0.

loadData - given a file name, this method should try to open this file and read the DVD data contained inside to create an initial alphabetized DVD collection. HINT: Read each set of three values (title, rating, running time) and use the addOrModifyDVD method above to insert it into the collection. If the file cannot be found, start with an empty array for your collection. If the data in the file is corrupted, stop initializing the collection at the point of corruption.

save - save the DVDs currently in the array into the same file specified during the load operation, overwriting whatever data was originally there.

Do not try to write all of these methods before testing your program. Instead, write the first two methods (and any associated helper methods) and test your program. Once you feel your program is tested enough, then move on to the other methods, one by one. Do the file input/output methods last. This way, if they don't work correctly, you can always go back to the previous version and hand in something that works correctly without using files, rather than hand in something that does not work at all.

Additional Requirements

Include documentation with all of your code. Your documentation should conform to the javadoc format when specified. One point of the ten points for this program will be based on the quality and form of your documentation.

Additionally, you may not change any code given to you already. That is, do not alter the methods so that your methods will work. You should be able to complete the assignment starting from the code given to you.

Testing

Using the data file given to you, run the program in console mode and capture the output of a sample run as a text file (cut and paste from the console window). Save this file in your submission as samplerun.txt. The file should illustrate your test cases that you used to test the correctness of the methods in the DVDCollection class. This file will also be worth 1 point of your program grade.

Hand-In Instructions & Late Submissions

See the course website for instructions on how to hand in your program and the policy for late submissions.