import java.util.*;
/**
* Lab #3
* CS 2334, Section 0??
* September 11, 2007
*
* This class implements a program that tests the movie ADT.
*
* @author Group #?
* @version 1.0
*/
public class Test
{
/**
* This is the main method for this test program. Since this is a
* simple test program, all of our code will be in the main method.
* Typically this would be a bad design, but we are just testing out
* some features of Java.
*
* Algorithm
* 1. Instantiate eight objects of type Movie and add
* them to the list movies.
* 2. Print out the unsorted list of movies.
* 3. Sort the list of movies using Collections.sort().
* 4. Print out the sorted list of movies.
* 5. Search for a particular movie in the list.
* 6. Test your equals method.
*
* @param args Contains the command line arguments.
*/
public static void main(String[] args)
{
List movies = new ArrayList();
/*
* 1. Instantiate eight objects of type Movie and add them
* to the list movies.
* I have created and added the first movie for you. :)
*/
Movie movie1 = new Movie( "Short Circuit", 1986 );
/* The list is referenced to by the variable movies. You can add to
* the list by invoking the add method if ArrayList.
*/
movies.add( movie1 );
/*
* 2. Print out the unsorted list of movies.
* This uses an iterator to "iterate" through the list.
*/
System.out.println( "\n\nUnsorted List" );
Iterator iterator = movies.iterator();
while( iterator.hasNext() )
{
// Note: This line of code will call toString of the Movie class.
System.out.println( iterator.next() );
}
/*
* 3. Sort the list of movies using Collections.sort().
* Take a look at Collections.sort() in the API at
* http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collections.html#sort(java.util.List,%20java.util.Comparator)
* You need to call Collections.sort() and pass it the list of movies.
* SEE THE LAB HANDOUT FOR MORE INFORMATION.
*/
// ADD CODE TO SORT HERE
/*
* 4. Print out the sorted list of movies.
*/
System.out.println( "\n\nSorted List" );
/* HINT: Use an iterator the same way I used one above when
* the unsorted list of movies was printed.
*/
// ADD CODE TO PRINT THE LIST HERE
/*
* 5. Search for a particular movie in the list.
*/
System.out.println( "\n\nSearching" );
Movie key = new Movie( "Short Circuit", 1986 );
System.out.println( "Key is " + key );
/*
* Call Collections.binarySearch() to find the index of key.
* Make sure you test the value of index to see if it negative,
* which indicates that the key was not found in the list.
*/
int index = Collections.binarySearch( movies, key );
/*
* Print out whether the movie was found or not and the index
* at which it was found.
* HINT: If index is negative print a statement saying that the
* movie searched for is not in the list. Otherwise, print
* out a statement telling that the movie was found in the list
* and give the index of the movie in the list as well.
*/
// ADD CODE HERE
/*
* 6. Test your equals method.
*/
System.out.println( "\n\nTesting Equals" );
Movie someMovie = new Movie( "Short Circuit", 1986 );
Movie anotherMovie = new Movie( "Terminator", 1984 );
if( key.equals( someMovie ) )
{
/*
* Add code here to inform the user that the
* movies key and someMovie are equal.
*/
}
if( !key.equals( anotherMovie ) )
{
/*
* Add code here to inform the user that the movies key
* and anotherMovie are not equal.
*/
}
}
}