Java: Movie and Review
Write a Java program to create a class called "Movie" with attributes for title, director, actors, and reviews, and methods for adding and retrieving reviews.
Sample Solution:
Java Code:
The above Java class has instance variables for the movie's title, director, actors, and reviews. The class constructor takes in the movie's title, director, and actors as parameters, and initializes the instance variables. The class also has methods to add a review to the movie's reviews list. It can retrieve the reviews list, and the movie's title, director, and actors. The reviews list is an ArrayList of Review objects, created outside of this class. These objects are added to the reviews list using the addReview() method.
The Review class represents a movie review, with attributes such as the review text, the reviewer's name, and the rating given to the movie. It has a constructor that takes these attributes as parameters and sets them as instance variables. The class also has three getter methods to retrieve review text, reviewer name, and rating.
The Main class is the entry point for the program. It creates two Movie objects: movie1 and movie2. It also creates five Review objects: review1, review2, review3, review4, and review5.
movie1 represents the movie "Titanic" directed by James Cameron and starring Leonardo DiCaprio and Kate Winslet. It adds four reviews to its reviews ArrayList.
movie2 represents the movie "The Godfather" directed by Francis Ford Coppola and starring Marlon Brando, Al Pacino, and James Caan. It adds three reviews to its reviews ArrayList.
The Main class then displays all the reviews for movie1 and movie2 by iterating over their reviews ArrayList and printing out the review text, reviewer name, and rating.
Sample Output:
Reviews for 'Titanic': Great movie! by Irvine Rolf - 4.5 Highly recommended! by Ashkii Karlheinz - 4.5 A classic that never gets old. by Nele Athol - 4.0 Great movie! by Cipactli Anselma - 4.0 Reviews for 'The Godfather': Great movie! by Irvine Rolf - 4.5 Great movie! by Cipactli Anselma - 4.0 Highly recommended! by Martin Nadine - 4.0
Flowchart:
For more Practice: Solve these Related Problems:
- Write a Java program where the "Movie" class keeps track of the most-watched movie in a given year.
- Write a Java program to implement a rating system where the "Movie" class adjusts the overall rating based on user reviews.
- Write a Java program where the "Movie" class filters reviews to detect and remove spam reviews automatically.
- Write a Java program to modify the "Movie" class to sort movies based on ratings and number of reviews.
Go to:
Live Demo:
Java Code Editor:
Improve this sample solution and post your code through Disqus.
PREV : Calculate the area and perimeter of shapes.
NEXT : Restaurant menu, average rating.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.