Programming Assignment Unit 6
Programming Assignment Unit 6
import java.util.Scanner;
interface Vehicle {
String getMake();
String getModel();
int getYearOfManufacture();
int getNumberOfDoors();
String getFuelType();
int getNumberOfWheels();
String getMotorcycleType();
double getCargoCapacity();
String getTransmissionType();
this.make = make;
this.model = model;
this.yearOfManufacture = yearOfManufacture;
@Override
return make;
@Override
return model;
}
@Override
return yearOfManufacture;
@Override
this.numberOfDoors = numberOfDoors;
@Override
return numberOfDoors;
}
@Override
this.fuelType = fuelType;
@Override
return fuelType;
@Override
return "Car{" +
'}';
this.make = make;
this.model = model;
this.yearOfManufacture = yearOfManufacture;
}
@Override
return make;
@Override
return model;
@Override
return yearOfManufacture;
@Override
public void setNumberOfWheels(int numberOfWheels) {
this.numberOfWheels = numberOfWheels;
@Override
return numberOfWheels;
@Override
this.motorcycleType = motorcycleType;
@Override
return motorcycleType;
}
@Override
return "Motorcycle{" +
'}';
this.make = make;
this.model = model;
this.yearOfManufacture = yearOfManufacture;
@Override
return make;
@Override
@Override
return yearOfManufacture;
@Override
this.cargoCapacity = cargoCapacity;
@Override
return cargoCapacity;
}
@Override
this.transmissionType = transmissionType;
@Override
return transmissionType;
@Override
return "Truck{" +
'}';
while (true) {
System.out.println("1. Car");
System.out.println("2. Motorcycle");
System.out.println("3. Truck");
System.out.println("4. Exit");
if (choice == 1) {
car.setNumberOfDoors(scanner.nextInt());
scanner.nextLine(); // Consume the newline character
car.setFuelType(scanner.nextLine());
System.out.println("Car details:");
System.out.println(car);
} else if (choice == 2) {
motorcycle.setNumberOfWheels(scanner.nextInt());
motorcycle.setMotorcycleType(scanner.nextLine());
System.out.println("Motorcycle details:");
System.out.println(motorcycle);
} else if (choice == 3) {
truck.setCargoCapacity(scanner.nextDouble());
truck.setTransmissionType(scanner.nextLine());
System.out.println("Truck details:");
System.out.println(truck);
} else if (choice == 4) {
break;
} else {
}
}
Output:
1. Car
2. Motorcycle
3. Truck
4. Exit
Toyoto
Corolla
2022
petrol
Car details:
fuelType='petrol'}
1. Car
2. Motorcycle
3. Truck
4. Exit
Harley-Davidson
Street Bob
sport
Motorcycle details:
numberOfWheels=2, motorcycleType='sport'}
1. Car
2. Motorcycle
3. Truck
4. Exit
Ford
2010
1.5
automatic
Truck details:
transmissionType='automatic'}
1. Car
2. Motorcycle
3. Truck
4. Exit
Explanation:
This Java program utilizes interfaces and classes to model a car rental agency system. The core
of the program is the `Vehicle` interface, which establishes a contract for all vehicle types. This
(`TruckVehicle`) extend `Vehicle` and add methods relevant to their unique characteristics.
`CarVehicle` handles attributes like number of doors and fuel type, while `MotorVehicle`
manages wheels and motorcycle type, and `TruckVehicle` accommodates cargo capacity and
transmission type.
The program then implements these interfaces through classes: `Car`, `Motorcycle`, and `Truck`.
Each class stores data specific to its respective vehicle type and implements the appropriate
interface methods.
Finally, the `CarRental` class acts as the main program. It presents a menu to the user, allowing
them to select a vehicle type. The user is then prompted to provide relevant information such as
make, model, year, and any additional details related to the chosen vehicle type. Based on the
user's input, an object of the corresponding vehicle class is created. The program then displays
the details of the newly created vehicle object, showcasing the values entered by the user. This
structure ensures a flexible and well-organized system for managing different vehicle types