?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

Text Representation of Object of Movie Class

APCSA-PKENL6

Consider the following partially-defined class.

public class Movie
{
    private String name;          //instance variable
    private String rating;        //instance variable
    private int year;

    public Movie(String nam, String rat, int yr)
    {
        name = nam;
        rating = rat;
        year = yr;
    }

    public String getName()
    {
        return name;
    }

    public String getRating()
    {
        return rating;
    }

    public int getYear()
    {
        return year;
    }
    // other data and methods not shown
}

The programmer wants the following statement to print out the year of the movie, name of a movie and its rating as follows:

In 1984, the following movie was made: Terminator with a rating of PG

What would need to be coded in the Movie class to make the following code (from a different class) print out that exact sentence?

Movie movie = new Movie("Terminator", "PG", 1984);
System.out.println("In " + movie.getYear() + ", the following movie was made: " + movie);
A
public String toString()
{
   return name + " with a rating of " + rating;  
}
B
public void toString()
{
   System.out.println(name + " with a rating of " + rating);  
}
C
public void toString()
{
   System.out.println("In " + movie.getYear() + ", the following movie was made: " + 
           name + " with a rating of " + rating);  
}
D
public void printString()
{
   System.out.println(name + " with a rating of " + rating);  
}
E
public String toString()
{
   return "In " + movie.getYear() + ", the following movie was made: " + name + 
              " with a rating of " + rating;  
}