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:3 | ISBN:9780132830317 | Edition: 5

Question

Extend the previous problem with a Rental class. This class should store a Movie that is rented, an integer representing the ID of the customer that rented the movie, and an integer indicating how many days late the movie is. Add a method that calculates the late fees for the rental. In your main method, create an array of type Rental filled with sample data of all types of movies. Then, create a method named lateFeesOwed that iterates through the array and returns the total amount of late fees that are outstanding.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

public class Movie 
{

	private String rating;
	private int ID;
	private String title;
	public Movie(String theRating, int theId, String theTitle)
	public String getRating()
	public int getID()	
	public String getTitle()
	public void setRating(String newRating)
	public void setID(int newID)
	public void setTitle(String newTitle)
	public boolean equals(Movie other)
	public double calcLateFees(int days)
	}
public class Comedy extends Movie
{
	public Comedy(String rating, int id, String title)
	public double calcLateFees(int days)
}
public class Action extends Movie
{
	public Action(String rating, int id, String title)
	public double calcLateFees(int days)
}
public class Drama extends Movie
{
	public Drama(String rating, int id, String title)
	public double calcLateFees(int days)
}

public class Rental
{
	private Movie rent;
	private int ID;
	private int lateDays;
	public Rental(Movie aMovie, int ID)
	{
		rent = aMovie;
		ID = ID;
		lateDays = 0;
	}
	public void setMovie(Movie x)
	{
		rent = x;
	}
	public void setID(int y)
	{
		ID = y;
	}
	public void setlateDays(int z)
	{
		lateDays = z;
	}
	public Movie getMovie()
	{
		return rent;
	}
	public int getID()
	{
		return ID;
	}
	public int getlateDays()
	{
		return lateDays;
	}
	public double calculateLateFee()
	{
		return rent.calcLateFees(lateDays);
	}
}

public class TestDemo {
	public static double lateFeesOwed(Rental[] rentals) {
		double total = 0;
		for (int i = 0; i < rentals.length; i++) {
			total += rentals[i].calculateLateFee();
		}

		return total;
	}

	public static void main(String args[]) {
		Movie movies[] = new Movie[6];
		movies[0] = new Action("PG-13", 1001, "Ronzer");
		movies[1] = new Comedy("dfdf", 1002, "Ted");
		movies[2] = new Drama("PG-13", 1003, "rose");
		movies[3] = new Action("PG-13", 1100, "spdider man");
		movies[4] = new Comedy("R", 2100, "jungle book");
		movies[5] = new Drama("R", 1003, "jamesbond");

		Rental rentals[] = new Rental[6];
		rentals[0] = new Rental(movies[0], 12);
		rentals[1] = new Rental(movies[1], 24);
		rentals[2] = new Rental(movies[2], 88);
		rentals[3] = new Rental(movies[3], 34);
		rentals[4] = new Rental(movies[4], 82);
		rentals[5] = new Rental(movies[5], 9);
		rentals[0].setDaysLate(1);
		rentals[1].setDaysLate(5);
		rentals[2].setDaysLate(0);
		rentals[3].setDaysLate(2);
		rentals[4].setDaysLate(3);
		rentals[5].setDaysLate(4);

		System.out.println("Total late fee: $" + lateFeesOwed(rentals));
	}
}

 

0 1

Discussions

Post the discussion to improve the above solution.