-4

just got some computer science from my friend, I believe it is a intro to programming assignment, and I was trying on it, but it seems there are so many issue, seems I am majoring CS, can someone help me on those questions step by step, would be appreciated. in instructions are following:

Create a public class Movie with private instance variables String title and int year. The class should declare that it implements the Comparable 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 interface. Movies should be compared first by title and then by year.

{ The Maltese Falcon 1941, The Thomas Crown Affair 1968, The Thomas Crown Affair 1999}

An equals() method that is compatible with the method that satisfies the Comparable 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: The Maltese Falcon (1941)

• A public static method getTestMovies(), which returns an array of 10
unique Movie instances. 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 must 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.

• A hashCode() method. Use the following:

public int hashCode()
{
return title.hashCode() + year;
 }

the following is what I have so far, I have the constructor and the starter, but I am sure how to do it.

public class Movie implements Comparable<Movie>{
private String title;
private int year;

public Movie(String title, int year){
    this.title = title;
    this.year = year;
}

@Override
public int compareTo(Movie that) {
    int value = 0;
    if(this.title == that.title)
    {
        if(this.year < that.year){
            value = 0;
        }
        else{
            value = -1;
        }
    }
    return value;
}

public static void getTestMovie(){

}

public boolean equals(Object x)
{

} }

Any Helps are appreciated!

K.cK
  • 1

2 Answers2

1

When you want sorting other than natural order, you should Comparator rather than Comparable. Please follow below link for more info : What is the difference between compare() and compareTo()?

Regarding string comparison use str1.equals(str2). For the reason follow below link : What is the difference between == vs equals() in Java?

For rest of the questions, please post some tried code.

Community
  • 1
  • 1
Suyash
  • 525
  • 1
  • 6
  • 10
0

You can't use this.title == that.title on Strings. You should use this.title.equals(that.title).

Except, for this code, you shouldn't. You should use this.title.compareTo(that.title).

If that return non-zero, return that value, because it represents the ordering when titles differ.

If it returned zero, the titles were equal, and you need the secondary check on year. For that use Integer.compare(this.year, that.year).

Andreas
  • 154,647
  • 11
  • 152
  • 247