Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is java problem this is the code: /////////////////////////////MOVIE.JAVA|\

ID: 3905958 • Letter: T

Question

This is java problem

this is the code:

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

public class Movie /* <does something go here?> */

{

/** 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.

* <P>

* @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.

* <P>

* @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.

* </P>

* @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.

* <P>

* Algorithm:<br>

* ????

* </P>

* @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.

* <P>

* Algorithm:<br>

* ????

* </P>

* @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.

}

}

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

Explanation / Answer


Given below is the completed code for the Movie class. I have written a test program to show how to use the Collections.sort() and Collecions.binarySearch() methods on the Movie objects.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you


Movie.java
========


public class Movie implements Comparable<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.

* <P>

* @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.
* <P>
* @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.
* </P>

* @return String representing the

* contents of this object.

*/

public String toString()
{

return "Title = " + title + ", Year = " + year;
}

/**

* This method compares an instance of Movie with
* this instance of Movie to see if they are equal.
* <P>
* Algorithm:<br>
* Compares the movie title, and compares them lexicograpically i.e alphabetic order
* If titles are same, compares the years
* </P>
* @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 )
{

if(obj instanceof Movie && this.compareTo((Movie)obj) == 0)
return true;
else
return false;

}

/**

* This method compares an instance of Movie with
* this instance of Movie to determine their relationship.
* <P>

* Algorithm:<br>
* Compares the movie title, and compares them lexicograpically i.e alphabetic order
* If titles are same, compares the years
* ????

* </P>

* @param other The object we are comparing

* this instanceof Movie with.

* @return -ve value if this movie is lesser than passed obj, 0 if both equal and +ve value if this is greater than other

*/
public int compareTo( Movie obj )
{


Movie m = (Movie) obj;
if(title.equalsIgnoreCase(m.title))
{
return year - m.year;
}
else
{
return title.compareToIgnoreCase(m.title);
}
}

}


TestMovie.java
======
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class TestMovie {

public static void main(String[] args) {
ArrayList<Movie> movies = new ArrayList<Movie>();
movies.add(new Movie("Predator",1987));
movies.add(new Movie("Aliens",1986));
movies.add(new Movie("Jurassic Park", 1993));
movies.add(new Movie("Song of the Sea", 2014));


System.out.println("Before sorting");
System.out.println(movies);

Collections.sort(movies);

System.out.println(" After sorting");
System.out.println(movies);

System.out.println("--------------");
Movie key = new Movie("Jurassic Park", 1993);
int index = Collections.binarySearch(movies, key);

if(index >= 0)
System.out.println(key + " found at index " + index );
else
System.out.println(key + " not found");


key = new Movie("Harry Potter and the Philosopher's Stone", 2001);
index = Collections.binarySearch(movies, key);
if(index >= 0)
System.out.println(key + " found at index " + index );
else
System.out.println(key + " not found");


}

}


output
====
Before sorting
[Title = Predator, Year = 1987, Title = Aliens, Year = 1986, Title = Jurassic Park, Year = 1993, Title = Song of the Sea, Year = 2014]

After sorting
[Title = Aliens, Year = 1986, Title = Jurassic Park, Year = 1993, Title = Predator, Year = 1987, Title = Song of the Sea, Year = 2014]
--------------
Title = Jurassic Park, Year = 1993 found at index 1
Title = Harry Potter and the Philosopher's Stone, Year = 2001 not found

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote