Q2

In this question, we'll work with objects to develop a movie tracker.

A

Write a Movie class. It should have the following attributes:

  • title: string title of the movie
  • starring: list of actors and actresses (strings) in the movie
  • year: integer year of release
  • stars: number of stars, 0-5
  • genre: string indicating the genre of the movie

It should have the following methods:

  • A constructor with arguments for all the class attributes
  • getTitle(): returns the title of the movie
  • getStarring(): returns the list of starring actors and actresses
  • addActor(actor): adds an actor's name to the list of those starring
  • remActor(actor): removes the specified actor from the list of those starring (case-insensitive)
  • getStars(): returns the number of stars for the movie

No import statements allowed.


In [ ]:


In [ ]:
assert Movie

In [ ]:
m = Movie(title = "Avengers: Infinity War", year = 2018, stars = 4.9, genre = "Action/Adventure", "Chris Evans", "Robert Downey, Jr.", "Scarlett Johansson")
assert m.title == "Avengers: Infinity War"
assert m.year == 2018
assert set(m.starring) == set(["Chris Evans", "Robert Downey, Jr.", "Scarlett Johansson"])
assert m.genre == "Action/Adventure"
assert int(m.stars) == 4

In [ ]:
m = Movie(title = "Avengers: Untitled", year = 2019, stars = 4.95, genre = "Action/Adventure", "Person 1", "Person 2", "Unnamed Avenger", "Who now?")
assert m.getTitle() == "Avengers: Untitled"
assert set(m.getStarring()) == set(["Person 1", "Person 2", "Unnamed Avenger", "Who now?"])
assert int(m.getStars()) == int(4.95)

m.addActor("Probably Robert Downey")
assert set(m.getStarring()) == set(["Person 1", "Person 2", "Unnamed Avenger", "Who now?", "Probably Robert Downey"])

m.remActor("person 2")
assert set(m.getStarring()) == set(["Person 1", "Unnamed Avenger", "Who now?", "Probably Robert Downey"])

B

Write a MovieTracker class. It will be used to track multiple Movie instances simultaneously. It should have the following attributes:

  • movies: a list of Movie instances

It should have the following methods:

  • A constructor that takes no arguments and initializes the class attribute to be an empty list
  • addMovie(movie): adds a new Movie object to its internal list of movies
  • remMovie(movie): removes the specified Movie object; equality is determined by movie title (case-insensitive)
  • avgRating(): returns the average rating (stars) of all the movies in the tracker
  • getGenre(genre): returns all the movies in the tracker that belong to the specified genre (case-insensitive), otherwise returns an empty list
  • ratingAtLeast(rating): returns all the movies in the tracker with at least the specified rating, otherwise returns an empty list

In [ ]:


In [ ]:
assert MovieTracker

In [ ]:
m1 = Movie(title = "Avengers: Infinity War", year = 2018, stars = 4.9, genre = "Action/Adventure", "Chris Evans", "Robert Downey, Jr.", "Scarlett Johansson")

mt = MovieTracker()
mt.addMovie(m1)
assert len(mt.movies) == 1
assert mt.avgRating() == 4.9
assert len(mt.ratingAtLeast(5.0)) == 0

In [ ]:
m1 = Movie(title = "Avengers: Infinity War", year = 2018, stars = 4.9, genre = "Action/Adventure", "Chris Evans", "Robert Downey, Jr.", "Scarlett Johansson")
m2 = Movie(title = "Avengers: Untitled", year = 2019, stars = 4.95, genre = "Action/Adventure", "Person 1", "Person 2", "Unnamed Avenger", "Who now?")

mt = MovieTracker()
mt.addMovie(m1)
mt.addMovie(m2)
assert len(mt.movies) == 2
assert mt.avgRating() == 4.925
assert len(mt.ratingAtLeast(4)) == 2
assert len(mt.getGenre("action/adventure")) == 2
mt.remMovie("avengers: untitled")
assert len(mt.movies) == 1

C

In this part, you'll write children classes of the Movie class based on genre. Create at least three children classes that specify a genre in their name (e.g. ComedyMovie, DramaMovie, etc).

Make sure to write a constructor for each class, but otherwise no need to override any existing functionality or add new functionality.


In [ ]: