SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Polymorphism And Abstract Classes
Exercise:
Programming Projects
Question:2 | ISBN:9780132830317 | Edition: 5

Question

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 an equals() method that over rides Object ’s equals() method, where two movies are equal if their ID number is identical. Next, create three additional 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. Test your classes from a main method.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:

// Movie.java
public class Movie
{
	private String rating;
	private int ID;
	private String title;

	public Movie()
	{
		rating = "";
		ID = 0;
		title = "";
	}

	public Movie(String aRating, int aID, String aTitle)
	{
		rating = aRating;
		ID = aID;
		title = aTitle;
	}

	public String getRating()
	{
		return rating;
	}

	public void setRating(String aRating)
	{
		rating = aRating;
	}

	public int getID()
	{
		return ID;
	}

	public void setID(int aID)
	{
		ID = aID;
	}

	public String getTitle()
	{
		return title;
	}

	public void setTitle(String aTitle)
	{
		title = aTitle;
	}

	public double calcLateFees(int days)
	{
		return 2.0 * days;
	}

	public boolean equals(Object obj)
	{
		if(obj == null)
			return false;
		else if(getClass() != obj.getClass())
			return false;
		else
		{
			Movie other = (Movie)obj;

			return (rating.equals(other.rating) && ID == other.ID 
					&& title.equals(other.title));
		}
	}

	public String toString()
	{
		return "\nMPAA Rating: " + rating + "\nID Number: " + ID
				+ "\nMovie Title: " + title;
	}
}
// Action.java
public class Action extends Movie 
{
	public Action()
	{
		super();
	}

	public Action(String aRating, int aID, String aTitle)
	{
		super(aRating, aID, aTitle);
	}
	
	public double calcLateFees(int days)
	{
		return 3.0 * days;
	}
}
// Comedy.java
public class Comedy extends Movie
{
	public Comedy()
	{
		super();
	}

	public Comedy(String aRating, int aID, String aTitle)
	{
		super(aRating, aID, aTitle);
	}

	public double calcLateFees(int days)
	{
		return 2.5 * days;
	}
}
// Drama.java
public class Drama extends Movie 
{
	public Drama()
	{
		super();
	}

	public Drama(String aRating, int aID, String aTitle)
	{
		super(aRating, aID, aTitle);
	}
	
	public double calcLateFees(int days)
	{
		return 2.0 * days;
	}
}
// MovieTest.java
public class MovieTest
{
	public static void main(String[] args)
	{
		Movie movie = 
				new Movie("PG-13", 3691, "Norm of the North");
		Action action = new Action("G", 2587, "Eye In The Sky");
		Comedy comedy = new Comedy("R", 7989, "Kung Fu Panda 3");
		Drama drama = 
				new Drama("PG-13", 4563, "Batman v Superman");

		System.out.println(movie);
		System.out.println("Late Fee: $" + movie.calcLateFees(6));

		System.out.println(action);
		System.out
				.println("Late Fee: $" + action.calcLateFees(6));

		System.out.println(comedy);
		System.out
				.println("Late Fee: $" + comedy.calcLateFees(6));

		System.out.println(drama);
		System.out.println("Late Fee: $" + drama.calcLateFees(6));
	}
}

Output:


MPAA Rating: PG-13
ID Number: 3691
Movie Title: Norm of the North
Late Fee: $12.0

MPAA Rating: G
ID Number: 2587
Movie Title: Eye In The Sky
Late Fee: $18.0

MPAA Rating: R
ID Number: 7989
Movie Title: Kung Fu Panda 3
Late Fee: $15.0

MPAA Rating: PG-13
ID Number: 4563
Movie Title: Batman v Superman
Late Fee: $12.0
1 0

Discussions

Post the discussion to improve the above solution.