w3resource

Java: Movie and Review

Java OOP: Exercise-17 with Solution

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:

//Movie.java
import java.util.ArrayList;

public class Movie {
  private String title;
  private String director;
  private ArrayList < String > actors;
  private ArrayList < Review > reviews;

  public Movie(String title, String director, ArrayList < String > actors) {
    this.title = title;
    this.director = director;
    this.actors = actors;
    this.reviews = new ArrayList < Review > ();
  }

  public void addReview(Review review) {
    this.reviews.add(review);
  }

  public ArrayList < Review > getReviews() {
    return this.reviews;
  }

  public String getTitle() {
    return this.title;
  }

  public String getDirector() {
    return this.director;
  }

  public ArrayList < String > getActors() {
    return this.actors;
  }
}

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.

//Review.java
class Review {
  private String reviewText;
  private String reviewerName;
  private double rating;

  public Review(String reviewText, String reviewerName, double rating) {
    this.reviewText = reviewText;
    this.reviewerName = reviewerName;
    this.rating = rating;
  }

  public String getReviewText() {
    return reviewText;
  }

  public String getReviewerName() {
    return reviewerName;
  }

  public double getRating() {
    return rating;
  }
}

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.

// Main.java
import java.util.ArrayList;
import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    // create a movie object
    Movie movie1 = new Movie("Titanic", "James Cameron",
      new ArrayList < String > (Arrays.asList("Leonardo DiCaprio", "Kate Winslet")));

    // add some reviews
    Review review1 = new Review("Great movie!", "Irvine Rolf", 4.5);
    Review review2 = new Review("Highly recommended!", "Ashkii Karlheinz", 4.5);
    Review review3 = new Review("A classic that never gets old.", "Nele Athol", 4.0);
    Review review4 = new Review("Great movie!", "Cipactli Anselma", 4.0);
    Review review5 = new Review("Highly recommended!", "Martin Nadine", 4.0);

    // add some reviews	for movie1
    movie1.addReview(review1);
    movie1.addReview(review2);
    movie1.addReview(review3);
    movie1.addReview(review4);

    Movie movie2 = new Movie("The Godfather", "Francis Ford Coppola",
      new ArrayList < String > (Arrays.asList("Marlon Brando", "Al Pacino", "James Caan")));

    // display all the reviews for the movie
    System.out.println("\nReviews for '" + movie1.getTitle() + "':");
    for (Review review: movie1.getReviews()) {
      System.out.println(review.getReviewText() + " by " + review.getReviewerName() + " - " + review.getRating());
    }

    // add some reviews	for movie2
    movie2.addReview(review1);
    movie2.addReview(review4);
    movie2.addReview(review5);

    // display all the reviews for the movie
    System.out.println("\nReviews for '" + movie2.getTitle() + "':");
    for (Review review: movie2.getReviews()) {
      System.out.println(review.getReviewText() + " by " + review.getReviewerName() + " - " + review.getRating());
    }
  }
}

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:

Flowchart: Java  OOP Exercises: Movie and Review.
Flowchart: Java  OOP Exercises: Movie and Review.
Flowchart: Java  OOP Exercises: Movie and Review.
Flowchart: Java  OOP Exercises: Movie and Review.

Live Demo:

Java Code Editor:

Improve this sample solution and post your code through Disqus.

Java OOP Previous: Calculate the area and perimeter of shapes.
Java OOP Next: Restaurant menu, average rating.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.