National University of Modern Languages
Department of Software Engineering
Object Oriented Programming
Assignment No.1
Student Name: _______________________
Roll No: ___________________________
Marks: 10
Submission Date: ____________
Q1: Title: Design and Implement a Careem Ride-Hailing Application using
Inheritance in Java
Scenario: You are required to design and develop a Java-based simulation of a Careem
ride-hailing system using inheritance and code reusability.
1.
Requirements:
1. Base Class – User
Attributes:
- name
- email
- contactNumber
- location
Methods:
- displayUserInfo()
- Parameterized constructor
Code: [Link]
public class User {
protected String name;
protected String email;
protected String contactNumber;
protected String location;
public User(String name, String email, String contactNumber, String location) {
[Link] = name;
[Link] = email;
[Link] = contactNumber;
[Link] = location;
}
public void displayUserInfo() {
[Link]("Name: " + name);
[Link]("Email: " + email);
[Link]("Contact: " + contactNumber);
[Link]("Location: " + location);
}
}
2. Derived Class – Customer (extends User)
Additional Attributes:
- customerId
- paymentMethod
Methods:
- bookRide()
- displayCustomerDetails()
Code: [Link]
public class Customer extends User {
private String customerId;
private String paymentMethod;
public Customer(String name, String email, String contactNumber, String location,
String customerId, String paymentMethod) {
super(name, email, contactNumber, location);
[Link] = customerId;
[Link] = paymentMethod;
}
public void bookRide() {
[Link](name + " (Customer) has booked a ride using " +
paymentMethod + ".");
}
public void displayCustomerDetails() {
displayUserInfo();
[Link]("Customer ID: " + customerId);
[Link]("Payment Method: " + paymentMethod);
}
}
3. Derived Class – Driver (extends User)
Additional Attributes:
- driverId
- vehicleAssigned
- rating
Methods:
- acceptRide()
- displayDriverDetails()
Code: [Link]
public class Driver extends User {
private String driverId;
private Vehicle vehicleAssigned;
private double rating;
public Driver(String name, String email, String contactNumber, String location,
String driverId, double rating) {
super(name, email, contactNumber, location);
[Link] = driverId;
[Link] = rating;
}
public void assignVehicle(Vehicle v) {
[Link] = v;
}
public void acceptRide() {
[Link]("Driver " + name + " has accepted the ride.");
}
public void displayDriverDetails() {
displayUserInfo();
[Link]("Driver ID: " + driverId);
[Link]("Rating: " + rating);
if (vehicleAssigned != null) {
[Link]("Vehicle Assigned: " + [Link]()
+ " (" + [Link]() + ")");
}
}
}
4. Vehicle Class
Attributes:
- vehicleId
- vehicleType
- registrationNumber
- capacity
Methods:
- displayVehicleInfo()
Code: [Link]
public class Vehicle {
private String vehicleId;
private String vehicleType;
private String registrationNumber;
private int capacity;
public Vehicle(String vehicleId, String vehicleType, String registrationNumber, int
capacity) {
[Link] = vehicleId;
[Link] = vehicleType;
[Link] = registrationNumber;
[Link] = capacity;
}
public String getVehicleType() { return vehicleType; }
public String getRegistrationNumber() { return registrationNumber; }
public void displayVehicleInfo() {
[Link]("Vehicle ID: " + vehicleId);
[Link]("Type: " + vehicleType);
[Link]("Registration: " + registrationNumber);
[Link]("Capacity: " + capacity);
}
}
5. Ride Class
Attributes:
- rideId
- pickupLocation
- dropoffLocation
- fare
- status
Methods:
- startRide()
- endRide()
- displayRideDetails()
Code: [Link]
public class Ride {
private String rideId;
private String pickupLocation;
private String dropoffLocation;
private double fare;
private String status; // Requested, Accepted, Completed
public Ride(String rideId, String pickupLocation, String dropoffLocation, double
fare) {
[Link] = rideId;
[Link] = pickupLocation;
[Link] = dropoffLocation;
[Link] = fare;
[Link] = "Requested";
}
public void startRide() {
status = "Started";
[Link]("Ride " + rideId + " has started.");
}
public void endRide() {
status = "Completed";
[Link]("Ride " + rideId + " has completed.");
}
public void accept() {
status = "Accepted";
[Link]("Ride " + rideId + " is accepted by the driver.");
}
public void displayRideDetails() {
[Link]("Ride ID: " + rideId);
[Link]("Pickup: " + pickupLocation);
[Link]("Dropoff: " + dropoffLocation);
[Link]("Fare: " + fare);
[Link]("Status: " + status);
}
}
6. Demonstration in main() method
Create objects, assign vehicle, create ride and simulate workflow.
Code: [Link] (Main)
public class CareemApp {
public static void main(String[] args) {
Customer c = new Customer("Ayesha", "ayesha@[Link]",
"03001234567", "G-10 Islamabad", "CUST001", "Card");
Driver d = new Driver("Bilal", "bilal@[Link]", "03007654321", "G-11
Islamabad", "DR001", 4.8);
Vehicle v = new Vehicle("V001", "Car", "ABC-123", 4);
[Link](v);
Ride r = new Ride("R001", [Link], "Blue Area", 350.0);
// Workflow
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]("\n--- DETAILS ---\n");
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
7. Use of super keyword
The derived classes Customer and Driver call the parent (User) constructor using
super(...). This initializes inherited attributes.
8. References
1. Deitel & Deitel, "How to Program", Java examples adapted.
2. Lecture notes of OOP (NUML)
3. Official Java documentation ([Link])
Output Screenshot (Simulated)