Create a public class Movie in java with private instance variables String title
ID: 3834247 • Letter: C
Question
Create a public class Movie in java with private instance variables String title
and int year. The class should declare that it implements the
Comparable<Movie> interface, and should provide the following:
• A constructor that takes 2 arguments: a String and an int (in that order)
for initializing title and year.
• A method that satisfies the Comparable<Movie> interface. Movies
should be compared first by title and then by year.
• Methods getTitle() and getYear() that do the right thing.
• An equals() method that is compatible with the method that satisfies the
Comparable<Movie> interface.
• A toString() method that prints “Movie” followed by 1 space followed by
the title followed by 1 space followed by open-parenthesis followed by
the year followed by close-parenthesis. Example:
Movie The Maltese Falcon (1941)
• A public static method getTestMovies(), which returns an array of 10
unique Movie instances. They don’t have to be real movies – it’s ok to
make them up. The 0th and 1st array elements must be 2 movies with the
same title but from different years (e.g. The Thomas Crown Affair 1968
and The Thomas Crown Affair 1999, or True Grit 1969 and True Grit
2010). The 2nd and 3rd elements (counting from 0) must be 2 movies with
different titles but from the same year (e.g. The Martian 2015 and Bridge
of Spies 2015). The 4th and 5th elements must be 2 different objects that
represent the same movie (same title and same year).
• A hashCode() method. Use the following:
public int hashCode()
{
return title.hashCode() + year;
}
Explanation / Answer
HI,Please find my implementation.
Please let me know in case of any issue.
public class Movie implements Comparable<Movie> {
// others instance members
private String title;
private int year;
public Movie(String title, int year) {
this.title = title;
this.year = year;
}
public String getTitle() {
return title;
}
public int getYear() {
return year;
}
@Override
public String toString() {
return "Movie "+title+" ("+year+")";
}
public int hashCode()
{
return title.hashCode() + year;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof Movie){
Movie v = (Movie)obj;
return title.equalsIgnoreCase(v.title) && year == v.year;
}
return false;
}
// implementation of compareTo method
@Override
public int compareTo(Movie o) {
if(title.compareTo(o.title) < 0)
return -1;
else if(title.compareTo(o.title) > 0)
return 1;
else{ // title is equal so sort based on year
if(year < o.year)
return -1;
else if(year > o.year)
return 1;
else
return 0;
}
}
public static Movie[] getTestMovies(){
Movie[] movies = {
new Movie("The Thomas Crown Affair", 1968),
new Movie("The Thomas Crown Affair", 1999),
new Movie("The Martian", 2015),
new Movie("Bridge of Spies", 2015),
new Movie("True Grit", 2010),
new Movie("True Grit", 2010),
new Movie("7th Movie", 1978),
new Movie("8th Movie", 2001),
new Movie("9th Movie", 1988),
new Movie("10th Movie", 1950)
};
return movies;
}
public static void main(String[] args) {
Movie[] movies = getTestMovies();
for(Movie m : movies)
System.out.println(m);
}
}
/*
Sample run:
Movie The Thomas Crown Affair (1968)
Movie The Thomas Crown Affair (1999)
Movie The Martian (2015)
Movie Bridge of Spies (2015)
Movie True Grit (2010)
Movie True Grit (2010)
Movie 7th Movie (1978)
Movie 8th Movie (2001)
Movie 9th Movie (1988)
Movie 10th Movie (1950)
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.