340,167 views
28 votes
28 votes
Consider the following class definition.public class RentalCar{private double dailyRate; // the fee per rental dayprivate double mileageRate; // the fee per mile drivenpublic RentalCar(double daily, double mileage){dailyRate = daily;mileageRate = mileage;}public double calculateFee(int days, int miles){/ missing code /}}The calculateFee method is intended to calculate the total fee for renting a car. The total fee is equal to the number of days of the rental, days, times the daily rental rate plus the number of miles driven, miles, times the per mile rate.Which of the following code segments should replace / missing code / so that the calculateFee method will work as intended?A.return dailyRate + mileageRate;B.return (daily dailyRate) + (mileage mileageRate);C.return (daily days) + (mileage miles);D.return (days dailyRate) + (miles mileageRate);E.return (days + miles) * (dailyRate + mileageRate);

User Samarth Hattangady
by
2.8k points

1 Answer

17 votes
17 votes

Final answer:

Option D is the correct answer, as it accurately calculates the total fee by multiplying the number of rental days by the daily rate and adding it to the product of the number of miles driven and the mileage rate.

Step-by-step explanation:

The question refers to a RentalCar class in Java and requires the completion of the calculateFee method, which is intended to calculate the total fee for renting a car based on daily and mileage rates. The correct formula to calculate this total fee is the number of rental days multiplied by the daily rate, plus the number of miles driven multiplied by the per mile rate.

Therefore, the correct code to replace / missing code / is:

D.return (days * dailyRate) + (miles * mileageRate);

This code calculates the part of the fee based on the days and adds it to the part of the fee based on the miles driven.

User MMM
by
3.0k points