a.Create a class named CarRental that contains fields that hold a renter’s name,
ID: 3641368 • Letter: A
Question
a.Create a class named CarRental that contains fields that hold a renter’s name, zip code, size of the car rented, daily rental fee, length of rental in days, and total rental fee. The class contains a constructor that requires all the rental data except the daily rate and total fee, which are 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 displays all the rental data.
b.Create a subclass named LuxuryCarRental. This class sets the rental fee at $79.99 per day and prompts the user to respond to the option of including a chauffeur at $200 more per day. Override the parent class display() method to include chauffeur fee information. Write an application named UseCarRental that prompts the user for the data needed for a rental and creates an object of the correct type. Display the total rental fee.
c.Save the files as CarRental. java, LuxuryCarRental. java, and UseCarRental. java
--------------------------------------------------------------------------------
Explanation / Answer
public class CarRental { public String name; public int zipCode; public String size; public double dailyFee; public int rentalTime; public double totalFee; public CarRental(String name, int zipCode, String size, int rentalTime) { this.name = name; this.zipCode = zipCode; this.size = size; this.rentalTime = rentalTime; if(size.equals("economy")) { dailyFee = 29.99; } if(size.equals("midsize")) { dailyFee = 38.99; } if(size.equals("full size")) { dailyFee = 43.50; } totalFee = dailyFee * this.rentalTime; } public void display() { System.out.println("Customer: " + name + ", Zipcode: " + zipCode); System.out.println("Rental Size: " + size + " for $" + dailyFee + "/day"); System.out.println("For " + rentalTime + " days comes out to a total of: $" + totalFee); } } import java.util.Scanner; public class LuxuryCarRental extends CarRental { public LuxuryCarRental(String name, int zipCode, String size, int rentalTime) { super(name, zipCode, size, rentalTime); dailyFee = 79.99; Scanner in = new Scanner(System.in); System.out.print("Would you like a chauffeur for an extra $200/day? (Yes/No): "); String response = in.next(); if(response.equals("Yes") || response.equals("yes")) { dailyFee += 200; } totalFee = dailyFee * this.rentalTime; } public void display() { System.out.println("Customer: " + name + ", Zipcode: " + zipCode); System.out.print("Luxury car rental "); if (dailyFee > 79.99) { System.out.print(" with a chauffeur "); } System.out.println("for $" + dailyFee + "/day"); System.out.println("For " + rentalTime + " days comes out to a total of: $" + totalFee); } } import java.util.Scanner; public class UseCarRental { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a name: "); String name = in.next(); System.out.print("Enter a zip code: "); int zipCode = in.nextInt(); System.out.print("Enter a vehicle size(economy/midsize/full size/luxury): "); String size = in.next(); System.out.print("Enter the amount of days the car will be rented: "); int rentalTime = in.nextInt(); if(!size.equals("luxury")) { CarRental car = new CarRental(name, zipCode, size, rentalTime); car.display(); } else { LuxuryCarRental car = new LuxuryCarRental(name, zipCode, size, rentalTime); car.display(); } } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.