this is java problem This is the code that i have /////////////////////////////M
ID: 3905259 • Letter: T
Question
this is java problem
This is the code that i have
/////////////////////////////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.
}
}
/////////////////////////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.
* <P>
* Algorithm<br>
* 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.
* </P>
* @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 theExplanation / Answer
package arraylist;
import java.util.Comparator;
/////////////////////////////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 "Title: " + title + " Year: " + 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.
// If the object is compared with itself then return true
if (obj == this)
{
return true;
}
/* Check if obj is an instance of Movie or not
"null instanceof [type]" also returns false */
if (!(obj instanceof Movie))
{
return false;
}
// type-cast o to Complex so that we can compare data members
Movie c = (Movie) obj;
// Compare the data members and return accordingly
return title.compareTo(c.title) == 0 && Integer.compare(year, c.year) == 0;
}
/**
* 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.
// type-cast o to Complex so that we can compare data members
Movie c = (Movie) obj;
return (this.title).compareTo(c.title);
//return statement.
}
}
----------------------------------------------------------------------------------------
package arraylist;
import java.util.*;
/////////////////////////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.
* <P>
* Algorithm<br>
* 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.
* </P>
* @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 );
Movie movie2 = new Movie( "Danger", 2000 );
Movie movie3 = new Movie( "Dil", 1996 );
Movie movie4 = new Movie( "Raid", 2018 );
Movie movie5 = new Movie( "Singham", 2006 );
Movie movie6 = new Movie( "Rangila", 1956 );
Movie movie7 = new Movie( "Goalmal", 2010 );
Movie movie8 = new Movie( "Airlift", 2017 );
/* 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 );
movies.add( movie2 );
movies.add( movie3 );
movies.add( movie4 );
movies.add( movie5 );
movies.add( movie6 );
movies.add( movie7 );
movies.add( movie8 );
/*
* 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.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
Collections.sort(movies, new Comparator<Movie>()
{
public int compare(Movie m1, Movie m2)
{
return m1.getName().compareTo(m2.getName());
}
});
/*
* 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
iterator = movies.iterator();
while( iterator.hasNext() )
{
//Note: This line of code will call toString of the Movie class.
System.out.println( iterator.next() );
}
/*
* 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 );
Comparator<Movie> c = new Comparator<Movie>()
{
public int compare(Movie u1, Movie u2)
{
return u1.getName().compareTo(u2.getName());
}
};
/*
* 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, c);
/*
* 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
if(index == -1)
System.out.println(key + " not found.");
else
System.out.println(key + " found at " + index);
/*
* 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.
*/
System.out.println(key + " and " + someMovie + " Both are same");
}
if( !key.equals( anotherMovie ) )
{
/*
* Add code here to inform the user that the movies key
* and anotherMovie are not equal.
*/
System.out.println(key + " and " + anotherMovie + " Both are not same");
}
}
}
Sample Output:
Unsorted List
Title: Short Circuit Year: 1986
Title: Danger Year: 2000
Title: Dil Year: 1996
Title: Raid Year: 2018
Title: Singham Year: 2006
Title: Rangila Year: 1956
Title: Goalmal Year: 2010
Title: Airlift Year: 2017
Sorted List
Title: Airlift Year: 2017
Title: Danger Year: 2000
Title: Dil Year: 1996
Title: Goalmal Year: 2010
Title: Raid Year: 2018
Title: Rangila Year: 1956
Title: Short Circuit Year: 1986
Title: Singham Year: 2006
Searching
Key is Title: Short Circuit Year: 1986
Title: Short Circuit Year: 1986 found at 6
Testing Equals
Title: Short Circuit Year: 1986 and Title: Short Circuit Year: 1986 Both are same
Title: Short Circuit Year: 1986 and Title: Terminator Year: 1984 Both are not same
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.