In: Computer Science
Write the code for following problem. [6 marks] The class Movie is started below. An instance of class Movie represents a film. This class has the following three class variables: title, which is a String representing the title of the movie studio, which is a String representing the studio that made the movie rating, which is a String representing the rating of the movie (i.e. PG13, R, etc) public class Movie { private String title; private String studio; private String rating; // your code goes here } a) Write a constructor for the class Movie, which takes a String representing the title of the movie, a String representing the studio, and a String representing the rating as its arguments, and sets the respective class variables to these values. b) Write a second constructor for the class Movie, which takes a String representing the title of the movie and a String representing the studio as its arguments, and sets the respective class variables to these values, while the class variable rating is set to "PG". c) Write a method get PG, which takes an array of base type Movie as its argument, and returns a new array of only those movies in the input array with a rating of "PG". You may assume the input array is full of Movie instances. The returned array need not be full. d) Write a piece of code that creates an instance of the class Movie with the title “XYZ Royale”, the studio “ABC Productions”, and the rating “PG13”.
Code:
*Compiled in eclipse**
CODE:
import java.util.ArrayList;
public class Movie
{
private String title;
private String studio;
private String rating;
// a - 3 parameter constructor.
public Movie(String title, String studio, String rating)
{
// 'this' keyword is used to differentiate between argument
variables
// and the same class variables.
this.title = title;
this.studio = studio;
this.rating = rating;
}
// b - 2 parameter constructor.
public Movie(String title, String studio)
{
this.title = title;
this.studio = studio;
rating = "PG";
}
// c - this methods returns an array of Movie objects which has
'PG' rating.
public Movie[] getPG(Movie[] movies)
{
// creating a new array of Movie objects of the length same as the
input array.
Movie[] pg_movies = new Movie[movies.length];
// counter for the new array.
int count = 0;
// using enhanced for loop to iterate over the input Movie
array.
for(Movie movie : movies)
{
// if a movie in the input array has the rating as 'PG'
if(movie.rating.equals("PG"))
{
// include that Movie in the new array.
// increment the counter.
pg_movies[count++] = movie;
}
}
// returning the new array of Movie objects.
return pg_movies;
}
// d - bonus question.
// this method returns a ArrayList of Movie objects which has 'PG'
rating.
public ArrayList<Movie> getPGAL(Movie[] movies)
{
// creating an object for ArrayList.
ArrayList<Movie> pg_movies = new ArrayList<>();
// using enhanced for loop to iterate over the input Movie
array.
for(Movie movie : movies)
{
// if a movie in the input array has the rating as 'PG'
if(movie.rating.equals("PG"))
{
// include that Movie in the ArrayList.
pg_movies.add(movie);
}
}
// returning ArrayList of PG movies.
return pg_movies;
}
// extra method to print the contents in the Movie object.
public String toString()
{
return "Title : "+ title + "\nStudio : " + studio + "\nRating : " +
rating + "\n";
}
public static void main(String[] args)
{
// Creating an instance of the class Movie with the title "Casino
Royale",
// the studio "Eon Productions" and the rating "PG-13".
Movie movie = new Movie("Casino Royale", "Eon Productions",
"PG-13");
System.out.println(movie);
}
}
Screenshot: