55.0k views
1 vote
In Java

Create a Rental class for a company who rents moving trucks for people with the following 3 attributes: name (of type string), number of days (of type integer), and additional fees (of type double). [The additional fees are for things like moving boxes purchased as part of the rental.] Make all the getters and setters as well as a full constructor for this class that takes all three variables and sets them. Make an abstract method called payRental() in this class as well.
Create a weekDayRental class that inherits from the Rental and takes all three values in its constructor and passes them to the parent. Override the payRental() method in this class that multiplies the number of days by $79 [fixed amount the company advertises] and adds the additional fees and prints this value for the user.
Create a weekEndRental class with a weekend rate (of type double) that inherits from the Rental and takes this value as well as the three values for a Rental in its constructor and passes them to the parent. Make a getter and setter for the weekend rate. Override the payRental() method in this class that multiplies the number or days by the weekend rate and adds the additional fees and prints this value for the user.

1 Answer

0 votes

Final answer:

The question is about implementing a Rental class in Java with subclasses for weekday and weekend rentals, including getters, setters, a constructor, and an abstract method. The subclasses must override the abstract method to calculate and print the rental cost.

Step-by-step explanation:

The task is to create a rental class in Java for a company that loans out moving trucks. This class should include the following attributes: name (string), number of days (int), and additional fees (double). Additionally, the class should have all the necessary getters and setters, a full constructor to initialize these attributes, and an abstract method payRental(). Following the Rental class, a subclass named weekDayRental needs to be created. This class inherits from Rental and uses its constructor to forward the values to the superclass. The payRental() method is overridden to calculate the total cost by multiplying the number of days by the fixed weekday rate of $79 and adding any additional fees before printing the final amount. Another subclass named weekEndRental should be created with an additional attribute, weekend rate (double). It should also have getters and setters for this new attribute and override the payRental() method to calculate costs based on the weekend rate plus any additional fees.

To create the Rental class, we need to declare three attributes: name (string), number of days (integer), and additional fees (double). We also need to create getters and setters for these attributes, as well as a constructor that takes all three variables and sets them. After that, we create an abstract method called payRental() in the Rental class. This method will be implemented differently in the subclasses. Creating the weekday rental subclass is straightforward. It inherits from the Rental class and passes all three values to the parent constructor. The payRental() method in this subclass multiplies the number of days by $79 and adds the additional fees, printing the result for the user. The weekEndRental subclass also inherits from the Rental class and takes an additional weekend rate from its constructor. It has a getter and a setter for the weekend rate. The payRental() method in this subclass multiplies the number of days by the weekend rate and adds the additional fees, printing the result for the user.

User Ansar Maleki
by
7.8k points