Java Question-01

Sureshkumar kajanthan
2 min readApr 2, 2023

--

Question-01
abstract class Vehicle {
private String make;
private String model;
private int year;

private double dailyRentalRate;

public Vehicle(String make, String model, int year, double dailyRentalRate) {
this.make = make;
this.model = model;
this.year = year;
this.dailyRentalRate = dailyRentalRate;
}

public String getMake() {
return make;
}

public void setMake(String make) {
this.make = make;
}

public String getModel() {
return model;
}

public void setModel(String model) {
this.model = model;
}

public int getYear() {
return year;
}

public void setYear(int year) {
this.year = year;
}

public double getDailyRentalRate() {
return dailyRentalRate;
}

public void setDailyRentalRate(double dailyRentalRate) {
this.dailyRentalRate = dailyRentalRate;
}

public abstract double rent(int days);
}
class Car extends Vehicle{

private int numPassengers;

public Car(String make, String model, int year, double dailyRentalRate,int numPassengers)
{
super(make,model,year,dailyRentalRate);
this.numPassengers=numPassengers;
}
public int getNumPassengers() {
return numPassengers;
}

public void setNumPassengers(int numPassengers) {
this.numPassengers = numPassengers;
}

@Overide
public double rent(int days)
{
return days*getDailyRentalRate();
}
}
public class Truck extends Vehicle {
private double cargoCapacity;

public Truck(String make, String model, int year, double dailyRentalRate,double cargoCapacity)
{
super(make,model,year,dailyRentalRate);
this.cargoCapacity=cargoCapacity;
}

public double getCargoCapacity() {
return cargoCapacity;
}

public void setCargoCapacity(double cargoCapacity) {
this.cargoCapacity = cargoCapacity;
}

@Override
public double rent(int days) {
return days*getDailyRentalRate();
}
}
public class RentalCompany {

private Vehicle[] vehicles;
public RentalCompany(Vehicle[] vehicles)
{
this.vehicles=vehicles;
}

public double calculateTotalRentalCost(int[] rentaldays)
{
double totalCost=0;
for(int i=0;i<rentaldays.length;i++)
{
totalCost+=vehicles[i].rent(rentaldays[i]);
}
return totalCost;
}
}
public class Main {
public static void main(String[] args)
{
Vehicle[] vehicles={
new Car("Toyota","camry",2022,50.0,5),
new Truck("Ford","f-150",2023,100.0,200.0)
};
RentalCompany rentalCompany=new RentalCompany(vehicles);

int[] rentalDays={3,5};

double totalCost=rentalCompany.calculateTotalRentalCost(rentalDays);
System.out.println("Total rental cost:$"+totalCost);


}
}

--

--

No responses yet