Create a class named CarRental that contains the following fields: renterName, z
ID: 3750831 • Letter: C
Question
Create a class named CarRental that contains the following fields: renterName, zipCode, carSize, rentalFee, numDaysRented, and totalRentalFee. The class contains a constructor that requires all of the rental data except total fee, which is calculated based on the size of the car: economy at $29.99 per day, midsize at $38.99 per day, or full size at $43.50 per day. The class also includes a display() method that display all of the rental data. Create a subclass named LuxuryCarRental. This class sets the rental fee at $79.99 per day. Write an application named UseCarRental that prompts the user for the renter's name, zip code, the size of the car to rent, and the number of days they want to rent it. Your application should display the total rental fee. Create a class named CarRental that contains the following fields: renterName, zipCode, carSize, rentalFee, numDaysRented, and totalRentalFee. The class contains a constructor that requires all of the rental data except total fee, which is calculated based on the size of the car: economy at $29.99 per day, midsize at $38.99 per day, or full size at $43.50 per day. The class also includes a display() method that display all of the rental data. Create a subclass named LuxuryCarRental. This class sets the rental fee at $79.99 per day. Write an application named UseCarRental that prompts the user for the renter's name, zip code, the size of the car to rent, and the number of days they want to rent it. Your application should display the total rental fee.Explanation / Answer
CarRental class:
public class CarRental
{
//fields
private String renterName,carSize;
private int numDaysRented,zipCode;
private double totalRentalFee,rentalFee;
//Constructor
public CarRental(String renterName, String carSize, int numDaysRented, int zipCode) {
super();
this.renterName = renterName;
this.carSize = carSize;
this.numDaysRented = numDaysRented;
this.zipCode = zipCode;
}
//display() method that display all of the rental data.
public void display()
{
if("economy".equalsIgnoreCase(carSize))
{
rentalFee=29.99;
}else if("midsize".equalsIgnoreCase(carSize))
{
rentalFee=38.99;
}else if("fullsize".equalsIgnoreCase(carSize))
{
rentalFee=43.50;
}
totalRentalFee=numDaysRented*rentalFee;
System.out.print("Total Rental Fee:"+totalRentalFee);
}
}
LuxuryCarRental class
//subclass named LuxuryCarRental.
public class LuxuryCarRental extends CarRental
{
//fields
private double rentalFee;
//constructor
public LuxuryCarRental(String renterName, String carSize, int numDaysRented, int zipCode)
{
//sets the rental fee at $79.99 per day
super(renterName, carSize, numDaysRented, zipCode);
this.rentalFee=79.99;
}
//getter and setter methods
public double getRentalFee() {
return rentalFee;
}
public void setRentalFee(double rentalFee) {
this.rentalFee = rentalFee;
}
}
UseCarRental class:
import java.util.Scanner;
public class UseCarRental
{
//main method to run application
public static void main(String[]arg)
{
//Scanner object to read inputs
Scanner scan = new Scanner(System.in);
//prompts the user for rental details
System.out.print("Enter renter's name:");
String renterName= scan.nextLine();
System.out.print("Enter zip code:");
int zipcode= scan.nextInt();
System.out.print("Enter size of the car to rent(economy,midsize,fullsize):");
String size= scan.next();
scan.nextLine();
System.out.print("Enter how many number of days to rent it:");
int numDaysRented= scan.nextInt();
//Create an object of CarRental class using values entered by User
CarRental carRental= new CarRental(renterName,size,numDaysRented,zipcode);
//calling display method
carRental.display();
}
}
Output:
Enter renter's name:Manahatan
Enter zip code:45133
Enter size of the car to rent(economy,midsize,fullsize):fullsize
Enter how many number of days to rent it:4
Total Rental Fee:174.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.