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

JAVA The Comparable interface is defined as follows: public interface Comparable

ID: 3876256 • Letter: J

Question

JAVA

The Comparable interface is defined as follows:

public interface Comparable<T> {

        int compareTo(T other);

}

A Film class is defined as

public class Film {

     private String title;

     private int yearOfRelease;

    

     public Film(String title, int yearOfRelease) {

          super();

          this.title = title;

          this.yearOfRelease = yearOfRelease;

     }

    

     public void display()

     {

System.out.println("Title " + title +". Release" + yearOfRelease);

     }

}

Rewrite the Film class so that it implements the Comparable interface that would cause films to be sorted by year of release and in the event that the year is the same then it would sort by title. Implement an equals method that overrides the equals method in the Object class to test for equality in the Film class. A song is said to be equal if the year and the title are equal.

Explanation / Answer

Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.

//Film.java

public class Film implements Comparable<Film> {

      private String title;

      private int yearOfRelease;

      public Film(String title, int yearOfRelease) {

            super();

            this.title = title;

            this.yearOfRelease = yearOfRelease;

      }

      public void display() {

            System.out.println("Title: " + title + ", Release: " + yearOfRelease);

      }

      /**

      * method to compare two Film objects for sorting. Will help to sort by

      * descending order of release year, if the year is same, sorts by the title

      * (alphabetically)

      */

      public int compareTo(Film other) {

            if (this.yearOfRelease > other.yearOfRelease) {

                  return -1;

            } else if (this.yearOfRelease < other.yearOfRelease) {

                  return 1;

            } else {

                  /**

                  * year of release is the same, so comparing titles

                  */

                  return this.title.compareTo(other.title);

            }

      }

      /**

      * method to check if two objects are equal (only if title and release year

      * are same for both Film objects

      */

      @Override

      public boolean equals(Object o) {

            /**

            * Checking if the object passed in the argument is a Film object

            */

            if (o instanceof Film) {

                  /**

                  * Type Casting

                  */

                  Film f = (Film) o;

                  if (this.title.equals(f.title)

                              && this.yearOfRelease == f.yearOfRelease) {

                        /**

                        * both titles are same, and both release years are same

                        */

                        return true;

                  }

            }

            return false;

      }

}

//Test.java

import java.util.ArrayList;

import java.util.Collections;

public class Test {

      public static void main(String[] args) {

            /**

            * Defining an ArrayList of Film

            */

            ArrayList<Film> filmsList=new ArrayList<Film>();

            /**

            * Creating a few Film objects

            */

            Film film1=new Film("Hello", 2000);

            Film film2=new Film("Wonder Woman", 2016);

            Film film3=new Film("Space Odeyssey", 1968);

            Film film4=new Film("Matrix", 1999);

            Film film5=new Film("Matrix Reloaded", 2010);

            Film film6=new Film("Avengers 2", 2016);

           

            /**

            * Adding films to the arraylist

            */

            filmsList.add(film1);

            filmsList.add(film2);

            filmsList.add(film3);

            filmsList.add(film4);

            filmsList.add(film5);

            filmsList.add(film6);

            /**

            * Sorting

            */

            Collections.sort(filmsList);

            /**

            * Displaying

            */

            for(Film f:filmsList){

                  f.display();

            }

      }

}

/*OUTPUT*/

Title: Avengers 2, Release: 2016

Title: Wonder Woman, Release: 2016

Title: Matrix Reloaded, Release: 2010

Title: Hello, Release: 2000

Title: Matrix, Release: 1999

Title: Space Odeyssey, Release: 1968