JAVA language ! Create a class named Movie that can be used with your video rent
ID: 3834554 • Letter: J
Question
JAVA language !
Create a class named Movie that can be used with your video rental business. The Movie class should track the Motion Picture Association of America (MPAA) rating (e.g., Rated G, PG-13, R), ID Number, and movie title with appropriate accessor and mutator methods. Also create and equals() method that overrides Object's equals() method, where two movies are equal if their ID number is identical. Next, create three additional movie classes named Action, Comedy, and Drama that are derived from Movie. Finally, create an overridden method named calcLateFees that takes as input the number of days a movie is late and returns the late fee for that movie. The default late fee is $2/day. Action movies have a late fee of $3/day, comedies are $2.50/day, and dramas are $2/day. In Addition to the project create and add: All appropriate accessor and mutator methods(Getters and setters) an "equals ' method a toString method a default(noargument) constructor and Overloaded constructor a copy constructor a clone method a finalize method a dispose method
I already wrote this but I keep getting error :
public class Movie implements Cloneable{
private String title;
private String rating;
private int id;
public Movie ()
{
this("No Title","No Rating",0);
System.out.println("Movie Class- default constructor");
}
public Movie(String title, String rating, int id)
{
this.title = title;
this.rating = rating;
this.id = id;
}
public Movie(Movie movie)
{
this.id = movie.id;
this.rating = movie.rating;
this.title = movie.title;
}
public void setTitle(String t)
{
this.title = title;
}
public void setRating(String r)
{
this.rating = rating;
}
public void setId( int id )
{
this.id = id;
}
public String getTitle()
{
return title;
}
public String getRating()
{
return rating;
}
public int getId()
{
return id;
}
public double calcLateFees(int numDays)
{
return 2.0*numDays;
}
//Equals
public boolean equals (Object obj){
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass () !=obj.getClass( ) )
return false;
Movie m = (Movie) obj;
if ( id == m.id)
{
return true;
}
else
{
return false;
}
}
//Copy Constructer
public Movie (Movie original)
{
if (original == null)
throw new NullPointerException();
else
{
id = original.id;
}
}
//Clone
protected Movie clone()
{
Movie movie = null;
try
{
movie = (Movie)super.clone();
}
catch (CloneNotSupportedException e)
{
System.out.println("Error while cloning Movie");
}
return movie;
}
//toString
public String toString()
{
return "Movie Title: " + getTitle() + " Rating: " + getRating() + " ID: " + getId();
}
//Finalize method
protected void finalize() throws Throwable
{
this.title = null;
this.rating = null;
this.id = 0;
super.finalize();
}
public void dispose ()
{
System.exit(0);
}
}
public class Action extends Movie implements Cloneable {
public Action()
{
super();
}
public Action (String title, String rating, int id)
{
super(title, rating, id);
}
public double calculateLateFee(int numDays)
{
return 3.0*numDays;
}
protected Action clone()
{
Action movie = null;
movie = (Action) super.clone();
return movie;
}
protected void finalize() throws Throwable
{
super.finalize();
}
public void dispose()
{
super.dispose();
}
}
public class Comedy extends Movie implements Cloneable {
public Comedy()
{
super();
}
public Comedy ( String title, String rating, int id)
{
super(title, rating, id );
}
public Comedy(Action movie)
{
super(movie);
}
public double CalculateLateFee( int numDays)
{
return 2.50*numDays;
}
protected Comedy clone()
{
Comedy movie = null;
movie = (Comedy) super.clone();
return movie;
}
protected void finalize() throws Throwable
{
super.finalize();
}
public void dispose()
{
super.dispose();
}
}
public class Drama extends Movie implements Cloneable
{
public Drama()
{
super();
}
public Drama (String title , String rating, int id)
{
super( title, rating, id);
}
public Drama(Action movie)
{
super(movie);
}
public double CalculateLateFee (int numdays)
{
return 2*numDays;
}
protected Drama clone()
{
Drama movie = null;
movie = (Drama) super.clone();
return movie;
}
protected void finalize() throws Throwable
{
super.finalize();
}
public void dispose()
{
super.dispose();
}
}
public class MovieTest
{
public static void main(String[]args)
{
Movie movie1 = new Movie ( "The Salesman" , "PG-13", 100);
Movie movie2 = new Movie ( "about Ellie" , "R" , 200);
Movie movie3 = new Movie ();
//testing getters
System.out.println("Movie1 Title:" +getTitle + "Movie1 Rating: " +getRating+ "Movie1 ID:" +getRating);
System.out.println("Movie2 Title:" +getTitle + "Movie2 Rating: " +getRating+ "Movie2 ID:" +getRating);
//Testing setters
movie3.setTitle("Never say Never");
movie3.setRating("PG");
movie3.setId(100);
//toString test
System.out.println(movie3);
//Testing Copy Constructor
Movie movie4 = new Movie(movie1);
//Testig Equals method
System.out.println(" Testing Equals Method:");
if (movie4.equals(movie1))
System.out.println ("movie4 and movie1 ARE EQUAL!");
else
System.out.println ("movie4 and movie1 ARE NOT equal!");
if (movie2.equals(movie3))
System.out.println("movie2 and movie3 ARE equal!");
else
System.out.println ("movie2 and movie3 ARE NOT equal!");
//Finalize
try
{
movie.finalize();
Action.finalize();
Comedy.finalize();
DramaMovie.finalize();
}
catch (Throwable e)
{
e.printStackTrace();
}
System.out.println("Finalize complete");
movie.dispose();
}
}
Explanation / Answer
Hi buddy, I corrected compilation errors in your code and added a few snippets. Please find the below java program and the output.
JAVA PROGRAM :
import java.io.*;
import java.util.*;
class Movie implements Cloneable {
private String title;
private String rating;
private int id;
public Movie() {
this("No Title", "No Rating", 0);
System.out.println("Movie Class- default constructor");
}
public Movie(String title, String rating, int id) {
this.title = title;
this.rating = rating;
this.id = id;
}
public Movie(Movie movie) {
if(movie==null){
throw new NullPointerException("object received null");
}
this.id = movie.id;
this.rating = movie.rating;
this.title = movie.title;
}
public void setTitle(String t) {
this.title = title;
}
public void setRating(String r) {
this.rating = rating;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public String getRating() {
return rating;
}
public int getId() {
return id;
}
public double calcLateFees(int numDays) {
return 2.0 * numDays;
}
//Equals
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Movie m = (Movie) obj;
if (id == m.id) {
return true;
} else {
return false;
}
}
//Copy Constructer
//Clone
protected Movie clone() {
Movie movie = null;
try {
movie = (Movie) super.clone();
} catch (CloneNotSupportedException e) {
System.out.println("Error while cloning Movie");
}
return movie;
}
//toString
public String toString() {
return "Movie Title: " + getTitle() + " Rating: " + getRating() + " ID: " + getId();
}
//Finalize method
protected void finalize() throws Throwable {
this.title = null;
this.rating = null;
this.id = 0;
super.finalize();
}
public void dispose() {
System.exit(0);
}
}
class Action extends Movie implements Cloneable {
public Action() {
super();
}
public Action(String title, String rating, int id) {
super(title, rating, id);
}
public double calculateLateFee(int numDays) {
return 3.0 * numDays;
}
protected Action clone() {
Action movie = null;
movie = (Action) super.clone();
return movie;
}
protected void finalize() throws Throwable {
super.finalize();
}
public void dispose() {
super.dispose();
}
}
class Comedy extends Movie implements Cloneable {
public Comedy() {
super();
}
public Comedy(String title, String rating, int id) {
super(title, rating, id);
}
public Comedy(Action movie) {
super(movie);
}
public double CalculateLateFee(int numDays) {
return 2.50 * numDays;
}
protected Comedy clone() {
Comedy movie = null;
movie = (Comedy) super.clone();
return movie;
}
protected void finalize() throws Throwable {
super.finalize();
}
public void dispose() {
super.dispose();
}
}
class Drama extends Movie implements Cloneable {
public Drama() {
super();
}
public Drama(String title, String rating, int id) {
super(title, rating, id);
}
public Drama(Action movie) {
super(movie);
}
public double CalculateLateFee(int numdays) {
return 2 * numdays;
}
protected Drama clone() {
Drama movie = null;
movie = (Drama) super.clone();
return movie;
}
protected void finalize() throws Throwable {
super.finalize();
}
public void dispose() {
super.dispose();
}
}
class MovieTest {
public static void main(String[] args) {
Movie movie1 = new Movie("The Salesman", "PG-13", 100);
Movie movie2 = new Movie("about Ellie", "R", 200);
Movie movie3 = new Movie();
//testing getters
System.out.println("Movie1 Title:" + movie1.getTitle() + "Movie1 Rating: " + movie1.getRating() + "Movie1 ID:" + movie1.getId());
System.out.println("Movie2 Title:" + movie2.getTitle() + "Movie2 Rating: " + movie2.getRating() + "Movie2 ID:" + movie2.getId());
//Testing setters
movie3.setTitle("Never say Never");
movie3.setRating("PG");
movie3.setId(100);
//toString test
System.out.println(movie3);
//Testing Copy Constructor
Movie movie4 = new Movie(movie1);
//Testig Equals method
System.out.println(" Testing Equals Method:");
if (movie4.equals(movie1)) {
System.out.println("movie4 and movie1 ARE EQUAL!");
} else {
System.out.println("movie4 and movie1 ARE NOT equal!");
}
if (movie2.equals(movie3)) {
System.out.println("movie2 and movie3 ARE equal!");
} else {
System.out.println("movie2 and movie3 ARE NOT equal!");
}
Movie movie = new Movie();
Action action = new Action();
Comedy comedy = new Comedy();
Drama dramaMovie = new Drama();
//Finalize
try {
movie.finalize();
action.finalize();
comedy.finalize();
dramaMovie.finalize();
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("Finalize complete");
movie.dispose();
}
}
OUTPUT :
Movie Class- default constructor
Movie1 Title:The SalesmanMovie1 Rating: PG-13Movie1 ID:100
Movie2 Title:about EllieMovie2 Rating: RMovie2 ID:200
Movie Title: No Title Rating: No Rating ID: 100
Testing Equals Method:
movie4 and movie1 ARE EQUAL!
movie2 and movie3 ARE NOT equal!
Movie Class- default constructor
Movie Class- default constructor
Movie Class- default constructor
Movie Class- default constructor
Finalize complete
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.