1

For a computer science project I have a Movie class that implements Comparable with Comedy, Action, and MovieTrilogy extending it. The problem I am having is that MovieTrilogy takes 3 Movie objects in the constructor. The MovieTrilogy class needs to have the Movie objects as instance variables which means I need to declare an abstract object. I belive what my teacher would want is to declare the MovieTrilogy object with Comedy and Action objects, but I would still need to store them as instance variables. How would I do this? The movie constructor as well as the MovieTrilogy class are atatched below.

public class MovieTrilogy extends Movie{
   private Movie movie1;
   private Movie movie3;
   private Movie movie2;
   public MovieTrilogy(Movie Movie1, Movie Movie2, Movie Movie3){
      movie1 = Movie1;
      movie2 = Movie2;
      movie3 = Movie3;
   }

And this is the movie class.

public abstract class Movie implements Comparable<Movie>{
   private int Score;
   private String Title;
   public Movie(String title, int score){
      Title = title;
      Score = score;
   }
   public int getScore(){ return Score; }
   public String getTitle(){ return Title; }
   public int compareTo(Movie movie){
      return this.compareTo(movie);
   }
   public String getGenre(){
      return null;
   }
   public String toString(){
      return Title + " with a score of "  + Score;
   }
}

The code below is the error it gives me trying to compile MovieTrilogy.

MovieTrilogy.java:5: error: constructor Movie in class Movie cannot be applied to given types;
public MovieTrilogy(Movie Movie1, Movie Movie2, Movie Movie3){
                                                            ^
required: String,int
found:    no arguments
7
  • 2
    If the constructor is asking for the abstract base class, you can still pass it an instance of a subclass. E.g. if ActionMovie extends Movie, then new MovieTriology(new ActionMovie(), ...) is valid.
    – Rogue
    Mar 4, 2021 at 18:16
  • 1
    Movie#compareTo(Movie movie) is looking a little infinite. Mar 4, 2021 at 18:19
  • Thank you for your response. I am however getting an error when I try to do that because I cant declare the movie instance variables. The error is in the edit
    – BCurbs
    Mar 4, 2021 at 18:19
  • 2
    MovieTrilogy should not inherit from Movie, MovieTrilogy should be composed of three Movie instances. See Difference between Inheritance and Composition. Mar 4, 2021 at 18:25
  • 3
    @BCurbs It'll still be to your benefit to understand why this is a potentially questionable design decision. There are valid reasons why it might make sense to do it this way, but not many. Consider this: a trilogy doesn't have a director, a movie does. (It also makes little sense to specialize on "trilogy", but that's a separate issue :) Mar 4, 2021 at 18:37

1 Answer 1

4

Okay so first let me address your specific error. In order for MovieTrilogy to extend Movie it has to satisfy the constructor that you enforced in the Movie class. So in order to construct a Movie (and consequently a MovieTrilogy) you have to provide a title and a score via a super method in the constructor.

So something like this:

public MovieTrilogy(Movie Movie1, Movie Movie2, Movie Movie3) {

    super("Trilogy title", 0);

    movie1 = Movie1;
    movie2 = Movie2;
    movie3 = Movie3;
}

However I wonder whether it is a good idea for MovieTrilogy to extend a Movie, as a trilogy doesn't really represent a specific movie, so can't be expected to have the same properties a movie does. I would recommend researching into inheritance as a programming concept to get some more clarity on how you should be thinking about it.

From the fact your constructor takes title and score it seems like you might be trying to represent some form of media that is rated so maybe you could have a structure like this:

class RatedTitle(String title, int score);
class Movie(String title, int score) extends RatedTitle(title, score);
class MovieTrilogy(String title, int score, Movie[] movies) extends RatedTitle(title, score);

(I hope you can think of a much better name than RatedTitle)


Edit

As @JohnHenly pointed out and said so eloquently,

Movie#compareTo(Movie movie) is looking a little infinite.

You seem to have a misunderstanding of implementing the Comparable interface and at the moment using the comparison method will result in a StackOverflowException. By returning this.compareTo(movie) you are trying to call the compareTo method defined in Movie which will then attempt to return this.compareTo(movie) again and again until the JVM can't take it anymore.

There are many articles that explain how to override toCompare properly but you might want to read this one because of the amazing title: Add the Comparable flavor, and taste the feeling.

3
  • Thank you. I believe this is the answer.
    – BCurbs
    Mar 4, 2021 at 18:28
  • I would even add the title as an argument to the MovieTrilogy constructor
    – user15244370
    Mar 4, 2021 at 18:31
  • I think that most modern IDEs will warn you about a method recursing infinitely.
    – MC Emperor
    Mar 4, 2021 at 23:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.