Create a class named RentalCar that includes an integer field for the car type (
ID: 3655651 • Letter: C
Question
Create a class named RentalCar that includes an integer field for the car type (1 = compact car, 2 = mid-sized car, 3 = full sized car) and a double field for the daily rental rate. Include get methods for these fields and a constructor method that requires an integer arguement representing the car type. The contstructor sets the rental rate based on the car type; compact cars are $69.95 per day, mid-size cars are $89.95 per day, and full sized cars are $99.99 per day. Create an extended class named Luxury whose constructor requires a car type and has a double field named Surcharge and sets the surcharge to $40.00. Write an application named UseRentalCar that allows the program user to create 4 cars and then displays to the screen a summary of the 4 cars chosen. Display to the summary information using a toString() method.Explanation / Answer
import java.util.*; class RentalCar { int carType; double rentalRate; int getCarType() { return carType; } double getRentalRate() { return rentalRate; } public RentalCar(int carType){ this.carType = carType; if(carType == 1) this.rentalRate = 69.95; else if(carType == 2) this.rentalRate = 89.95; else this.rental = 99.99; } } class Luxury extends RentalCar{ double Surcharge; double getSurcharge(){return this.Surchage;} public Luxury(int carType){ RentalCar(carType); this.Surcharge = 40.00; } } class UseRentalCar { public static void main(String args[]){ System.out.println("Enter carType of car1:"); Scanner sc = New Scanner(System.in); int cartype = sc.nextInt(); RentalCar car1 = new RentalCar(cartype); System.out.println("Enter carType of car2:"); cartype = sc.nextInt(); RentalCar car2 = new RentalCar(2); System.out.println("Enter carType of car3:"); cartype = sc.nextInt(); RentalCar car3 = new RentalCar(3); System.out.println("Enter carType of car4:"); cartype = sc.nextInt(); Luxury car4 = new Luxury(2); System.out.println(car1.getCarType.toString()+" "+car1.getRentalrate().toString()); System.out.println(car2.getCarType.toString()+" "+car2.getRentalrate().toString()); System.out.println(car3.getCarType.toString()+" "+car3.getRentalrate().toString()); System.out.println(car4.getCarType.toString()+" "+car4.getRentalrate().toString())+" "+car4.getSurcharge().toString(); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.