Question

In: Computer Science

Write the code for following problem. [6 marks] The class Movie is started below. An instance...

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”.

Solutions

Expert Solution

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:


Related Solutions

Code in Java Write a Student class which has two instance variables, ID and name. This...
Code in Java Write a Student class which has two instance variables, ID and name. This class should have a two-parameter constructor that will set the value of ID and name variables. Write setters and getters for both instance variables. The setter for ID should check if the length of ID lies between 6 to 8 and setter for name should check that the length of name should lie between 0 to 20. If the value could not be set,...
Given the main method of a driver class, write a Fraction class. Include the following instance...
Given the main method of a driver class, write a Fraction class. Include the following instance methods: add, multiply, print, printAsDouble, and a separate accessor method for each instance variable. Write a Fraction class that implements these methods: add ─ This method receives a Fraction parameter and adds the parameter fraction to the calling object fraction. multiply ─ This method receives a Fraction parameter and multiplies the parameter fraction by the calling object fraction. print ─ This method prints the...
Write pseudocode (3 Marks) and program structure (4 Marks) for the problem given below; In a...
Write pseudocode and program structure (4 Marks) for the problem given below; In a college, students are awarded a pass grade if their total mark is between 50-59, credit grade if the mark is between 60-69, distinction for marks between 70-79. High distinction if the mark is above or equal to 80 and fail if the mark is below 50.
Consider a class Movie that information about a movie. The class has the following attributes: • ...
Consider a class Movie that information about a movie. The class has the following attributes: • The movie name • The MPAA rating (for example, G, PG, PG-13, R) • The number of people that have rated this movie as a 1 (Terrible) • The number of people that have rated this movie as a 2 (Bad) • The number of people that have rated this movie as a 3 (OK) • The number of people that have rated this...
Please write about the documentary movie of (Inside Job) Movie Assignment as following questions below ....
Please write about the documentary movie of (Inside Job) Movie Assignment as following questions below . Don't write about Wall Street . This is the 2nd question posting 1. Discuss the serious ethical concerns for business in the "Inside Job movie". 1. How business was portrayed in the film of Inside Job. What you should find is that business is generally portrayed in negative terms. For example, most business people in these films are motivated by greed. There are exceptions....
#Write a class called "Burrito". A Burrito should have the #following attributes (instance variables): # #...
#Write a class called "Burrito". A Burrito should have the #following attributes (instance variables): # # - meat # - to_go # - rice # - beans # - extra_meat (default: False) # - guacamole (default: False) # - cheese (default: False) # - pico (default: False) # - corn (default: False) # #The constructor should let any of these attributes be #changed when the object is instantiated. The attributes #with a default value should be optional. Both positional #and...
Write a class called Pen that contains the following information: Private instance variables for the price...
Write a class called Pen that contains the following information: Private instance variables for the price of the pen (float) and color of the pen (String). A two-argument constructor to set each of the instance variables above. If the price is negative, throw an IllegalArgumentException stating the argument that is not correct. Get and Set methods for each instance variable with the same error detection as the constructor. public class Pen {
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document...
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document { private int words; /** * constructor * pre: none * post: A Document object created. Words initialized to 0. */ public Document() { words = 0; //default words } /** * Changes the number of document words. * pre: none * post: Words has been changed. */ public void setWords(int numWords) { words = numWords; } /** * Calculates the number of pages....
C++ Consider a class Movie that information about a movie. The class has the following attributes:...
C++ Consider a class Movie that information about a movie. The class has the following attributes: • The movie name • The MPAA rating (for example, G, PG, PG-13, R) • Array of size 5 called Ratings, each index will hold the following. [0] The number of people that have rated this movie as a 1 (Terrible) [1] The number of people that have rated this movie as a 2 (Bad) [2] The number of people that have rated this...
Write html code: 1. Define constructor functions Faculty and Course. An instance of Course has instance...
Write html code: 1. Define constructor functions Faculty and Course. An instance of Course has instance of Faculty as the value if its instructor instance variable. Download testProb1.js and testProb1.html Write two files (faculty.js and course.js) Constructor function Faculty. Arguments name and dept; Instance variables, name (a string, with default ("name unknown") Write get_ and set_ methods for both instance variables; toString() return strings of form "<name> of <dept>" Constructor function Course Arguments name, times, instructorName, and instructorDept, all with...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT