Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

this is java problem This is the code that i have /////////////////////////////MOVIE.JAVA| public class Movie /* */ { /** The title of the Movie represented

this is java problem

image text in transcribed

image text in transcribed

This is the code that i have

/////////////////////////////MOVIE.JAVA|\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

public class Movie /* */

{

/** The title of the Movie represented by this class. */

private String title;

/** The release date of the movie. */

private int year;

/**

* This is the default constructor for the class Movie.

*/

public Movie()

{

year = 0;

title = "";

}

/**

* This is the constructor for the class Movie.

* It instantiates the class with user supplied values.

*

* @param title The title of the movie.

* @param year The release date of the movie.

*/

public Movie(String title, int year)

{

this.title = new String( title );

this.year = year;

}

/**

* Returns the title of the movie.

*

* @return The title of the movie as a string.

*/

public String getName()

{

return title;

}

/**

* This method returns the attributes of this movie as

* a single string.

*

* @return String representing the

* contents of this object.

*/

public String toString()

{

// Add the code for toString() here.

return Integer.toString(year); // Make sure you replace

// this return statement.

}

/**

* This method compares an instance of Movie with

* this instance of Movie to see if they are equal.

*

* Algorithm:

* ????

*

* @param obj The object we are comparing

* this instance of Movie with.

* @return Returns true if the two instances are

* equal, false otherwise.

*/

public boolean equals( Object obj )

{

// Add the code for equals() here.

return true; // Make sure you replace this return statement.

}

/**

* This method compares an instance of Movie with

* this instance of Movie to determine their relationship.

*

* Algorithm:

* ????

*

* @param other The object we are comparing

* this instanceof Movie with.

* @return ?????????

*/

public int compareTo( Object obj )

{

// Add the code for compareTo() here.

return (int)year; // Make sure you replace this

// return statement.

}

}

/////////////////////////Test.Java\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

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( " Unsorted 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.ht

ml#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( " Sorted 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( " Searching" );

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( " Testing 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.

*/

}

}

}

Step 1: Complete the implementation of the movie class. Make sure you fill in the class and method header comments and declarations where information is missing. First, read the entire Movie.java file. After reading the file, add code to complete the implementation of the tostring), equals), and compareTo () methods. Before you can search a List for an item, you must sort the List by calling the sort ) method of the Collections class. This method will call the compareTo method of each item that is present in the List. Sample code that uses sort 0 to sort a list is given below List myList new ArrayListo myList.add ( new Person (. ) ), Collectons.sort (myList In order to search a List to find a particular object you must call the binarysearch ) method of the Collections class. This method takes as a parameter an object (called the key) that represents the object we are searching for. If binarysearch ) finds the key in the list, it will return the index to the item in the list that matches the key, otherwise it will return a negative integer (we talked about how this negative integer is computed in class). Sample code that uses binarysearch ) to search for an item in a list is given below Person key = new Person( ); Person result int index; index = collections.binarysearch ( myList, key ), if( index> index "in the list." else t list!" result = List.get( index ); system.out.println( result.tostring) + " resides at index "+ System.out.println( key.tostring () was not found in the

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Database Modeling With Neo4j

Authors: Ajit Singh

2nd Edition

B0BDWT2XLR, 979-8351798783

More Books

Students also viewed these Databases questions

Question

What is the normal balance of Purchases Discount?

Answered: 1 week ago