1 answer

JAVA The Comparable interface is defined as follows: public interface Comparable<T> {         int compareTo(T other);...

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.


Answers

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

  1. Modified the class Film.java to implement Comparable interface, and implemented the function compareTo() to compare two Film objects by release year (descending order), and in case of equal years, by title (alphabetical order).
  2. Overridden the equals() method as per the needs.
  3. Created a Test class, defined an ArrayList of Film objects, created a few Film objects, added it to the list, and then sorted it. Finally displayed the sorted list.
  4. Comments are included, If you have any doubts, feel free to ask, Thanks

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

.

Similar Solved Questions

1 answer
Identify the vertices of the cycle in the digraph. B C A E B, D, E,...
Identify the vertices of the cycle in the digraph. B C A E B, D, E, C B, E, C A, B, D A, D, E, C, B...
1 answer
4-What are the 8 allergens that must be listed on a food label? (8 pts) Styles...
4-What are the 8 allergens that must be listed on a food label? (8 pts) Styles 5-The percent Daily Value is based on a calorie diet. diet 6-What are the 2 types of digestion? What are the phases of digestion? What happens in each phase? 7-What are enzymes? 8.What is Bicarbonate; Mucus; Sphincter; Li...
1 answer
7. Determine the quiescent levels of Ic and Vce for the network of Figure 25. 10...
7. Determine the quiescent levels of Ic and Vce for the network of Figure 25. 10 V 4.7 k92 250 k22 16 10 uF B = 90 OH 10 uF 1.2 ΚΩ Figure 25 8. A stage is a transistor and the passive components connected to it. Figure 26 shows a three-stage circuit using two-supply emitter bias. Vec +15...
1 answer
The following is available information regarding Beller Co.'s five products that it sells: Product Cost $210...
The following is available information regarding Beller Co.'s five products that it sells: Product Cost $210 311 Net Realizable Value $232 286 414 367 391 ZOA 496 343 411 Which of the following would be part of the journal entry to adjust the year-end inventory? Select one: O a. Credit Inventory...
1 answer
A. In which instance would "crowding out likely become a concern? O Prior-year budget surpluses allow...
a. In which instance would "crowding out likely become a concern? O Prior-year budget surpluses allow the government to use saved funds to reduce taxes. In order to increase spending on infrastructure, the federal government decides to borrow funds. O A balanced budget law prevents the governmen...
1 answer
Uiz Instructions e home quiz-to be taken on Canvas DQuestion 6 Two forms of energy transfer...
uiz Instructions e home quiz-to be taken on Canvas DQuestion 6 Two forms of energy transfer are O heat and work O pressure and volume O none O calorie and joule Previous No new data to save. Last checked a...
1 answer
The distance between polarizers and between the second polarizer and the light sensor is not taken...
The distance between polarizers and between the second polarizer and the light sensor is not taken into account in Malus’ Law. Which part/parts of (Malus' Law) Equation I=Io cos^2 (θ) would be affected by distances? What will not be affected? Clearly state and explain....
1 answer
Let R be a commutative ring with unity 1 and let I be a minimal ideal...
Let R be a commutative ring with unity 1 and let I be a minimal ideal in R i.e. a nonzero ideal which does not properly contain another non-zero ideal. Show that either the product of two elements in I is always zero or there is an element in I that serves as unity in the ring I. Show also that in t...
1 answer
If #A = <1 ,6 ,9 >#, #B = <-9 ,-6 ,7 ># and #C=A-B#, what is the angle between A and C?
If #A = <1 ,6 ,9 >#, #B = <-9 ,-6 ,7 ># and #C=A-B#, what is the angle between A and C?...
1 answer
A 185.0 mL solution of 2.714 M strontium nitrate is mixed with 215.0 mL of a...
A 185.0 mL solution of 2.714 M strontium nitrate is mixed with 215.0 mL of a 3.460 M sodium fluoride solution. Calculate the mass of the resulting strontium fluoride precipitate. mass: g Assuming complete precipitation, calculate the final concentration of each ion. If the ion is no longer in soluti...
1 answer
20 points (Q2) In the following three phase system van(t) = 220 cos(2x50t + 20°) VRMS....
20 points (Q2) In the following three phase system van(t) = 220 cos(2x50t + 20°) VRMS. Van 122 + 1Н 10000 12 2004F Von 200F 112 1Н 112 12 ? M 111 112 200F V 112 1Н 0000 Figure 2: Circuit for Question 2 (a) (15 points) Find the line currents ia, i, and i (b) (5 points) Find the ...
1 answer
Need a 200-300 word answer to the mini case question Mini case. Need 200-300 words answer...
Need a 200-300 word answer to the mini case question Mini case. Need 200-300 words answer to questions carnational marketing expansion Natura's inter in cosmete proti underbas w we of me The company produce und sa Brazilian company spe products. Since its creation the basic marketing concept o...
1 answer
ALL QUESTIONS ARE TRUE OR FALSE, also they are business law questions 8. Wrongs and compensation...
ALL QUESTIONS ARE TRUE OR FALSE, also they are business law questions 8. Wrongs and compensation are the basis of all torts. 9. Conversion is not the civil side of theft crimes. 10. Negligence torts require duty, breach, causation, and damages. 11. The law imposes strict product liability as a matte...
1 answer
Support Department Cost Allocation-Direct Method Christmas Timber, Inc., produces Christmas trees. The trees are pr...
Support Department Cost Allocation-Direct Method Christmas Timber, Inc., produces Christmas trees. The trees are produced through a cutting and pruning process. Machine maintenance and janitorial labors are performed throughout the production process by nonproduction employees. Maintenance and janit...
1 answer
II Review Part A Find the work done by a monatomic ideal gas as it expands...
II Review Part A Find the work done by a monatomic ideal gas as it expands from point A to point C along the path shown in the figure(Figure 1). Express your answer using two significant figures. W= = 2.6 MJ Figure < 1 of 1 Submit Previous Answers Correct B 600 Pressure, P (kPa) 400 Part B 200 If...
1 answer
Please recommend me a website to solve this function e^(-2.15x) = (1-x)^2 as this is hard...
Please recommend me a website to solve this function e^(-2.15x) = (1-x)^2 as this is hard to solve by hand. Please recommend the website...
1 answer
Owing chart shows the tax brockets and standard deductions for fing taxes in 2018 obtained from...
owing chart shows the tax brockets and standard deductions for fing taxes in 2018 obtained from nups wwwhrblock.com/tax-centerirsitax brackets and rates what-are-the-tax-brockets and m s www.doucnoler.net/taxes/federal income tax cockets douchons and exemptions are no longer personal exemptions for ...