You are in the rental car business. Create a class called RentalCar that include
ID: 3674957 • Letter: Y
Question
You are in the rental car business. Create a class called RentalCar that includes a string type for the car type (e.g., passenger car, van, truck, etc.) and a double field for the rental rate per day. Include get methods for these fields and a constructor that requires a string argument representing the car type. The constructor sets the room rate as follows – cars $49.99 per day, vans $69.99 per day, trucks $89.99 per day. Create an extended class, Luxury, whose constructor requires a car type and adds $20 per day to the appropriate rental rate for each type of vehicle. Write an application named UseRentalCar which creates an object of each class. Demonstrate that all methods work as they should.
Explanation / Answer
public class RentalCar {
private String name;
private double rate;
public RentalCar(String name, double rate) {
this.name = name;
this.rate = rate;
}
public String getName() {
return name;
}
public double getRate() {
return rate;
}
@Override
public String toString() {
return "RentalCar - [Name: "+getName()+", Rent: "+getRate()+"]";
}
}
// Luxury class
class Luxury extends RentalCar{
public Luxury(String name, double rate) {
super(name, rate+20);
}
@Override
public String toString() {
return "LuxutyCar - [Name: "+getName()+", Rent: "+getRate()+"]";
}
}
class UseRentalCar {
public static void main(String[] args) {
RentalCar rc1 = new RentalCar("car", 49.99);
RentalCar rc2 = new RentalCar("Truck", 89.99);
RentalCar rc3 = new RentalCar("Van", 69.99);
Luxury lc1 = new Luxury("Truck", 89.99);
System.out.println(rc1.toString());
System.out.println(rc2.toString());
System.out.println(rc3.toString());
System.out.println(lc1.toString());
}
}
/*
Output:
RentalCar - [Name: car, Rent: 49.99]
RentalCar - [Name: Truck, Rent: 89.99]
RentalCar - [Name: Van, Rent: 69.99]
LuxutyCar - [Name: Truck, Rent: 109.99]
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.