Object Oriented
Programming
LAB TASK
Omer Shahzad (SP22-BAI-038) 11/21/24 OOP
Graded Tasks:
Task 1:
package vehiclemanagmentsystem;
public class Vehicle {
protected String make;
protected String model;
protected int year;
public Vehicle() {
this.make = "NULL";
this.model = "NULL";
this.year = 0;
}
public Vehicle(String m, String mo, int y) {
this.make = m;
this.model = mo;
this.year = y;
}
public String getModel() {
return this.model;
}
public void setModel(String m) {
this.model = m;
}
public String getMake() {
return this.make;
}
public void setMake(String m) {
this.make = m;
}
public int getYear() {
return this.year;
}
public void setYear(int y) {
this.year = y;
}
1
public double fuelEfficiency(double avg) {
return avg;
}
public double calculateRentalFee(int days) {
return (double)(50 * days);
}
public String toString() {
return "Make: " + this.make + "\nModel: " + this.model + "\nYear: " +
this.year;
}
}
package vehiclemanagmentsystem;
public class Bike extends Vehicle {
private double engineDisplacement;
private double weight;
public Bike() {
this.engineDisplacement = 0.0;
this.weight = 0.0;
}
public Bike(String ma, String mo, int y, double ed, double wei) {
super(ma, mo, y);
this.engineDisplacement = ed;
this.weight = wei;
}
public String toString() {
return super.toString() + "\nEngine Displacement: " +
this.engineDisplacement + "\nWeight: " + this.weight;
}
public double calculateRentalFee(int days) {
return (double)(40 * days);
}
}
2
// Source code is decompiled from a .class file using FernFlower decompiler.
package vehiclemanagmentsystem;
public class Car extends Vehicle {
private double engineDisplacement;
private double weight;
public Car(String mak, String mou, int y, double ed, double wei) {
super(mak, mou, y);
this.engineDisplacement = ed;
this.weight = wei;
}
public double getEngineDisplacement() {
return this.engineDisplacement;
}
public void setEngineDisplacement(double ed) {
this.engineDisplacement = ed;
}
public double getWeight() {
return this.weight;
}
public void setWeight(double wei) {
this.weight = wei;
}
public double calculateRentalFee(int days) {
return (double)(50 * days);
}
public String toString() {
return super.toString() + "\nEngine Displacement: " +
this.engineDisplacement + "\nWeight: " + this.weight;
}
}
package vehiclemanagmentsystem;
public class Truck extends Vehicle {
3
private double engineDisplacement;
private double weight;
public Truck() {
this.engineDisplacement = 0.0;
this.weight = 0.0;
}
public Truck(String ma, String mo, int y, double ed, double wei) {
super(ma, mo, y);
this.engineDisplacement = ed;
this.weight = wei;
}
public String toString() {
return super.toString() + "\nEngine Displacement: " +
this.engineDisplacement + "\nWeight: " + this.weight;
}
public double calculateRentalFee(int days) {
return (double)(75 * days);
}
}
package vehiclemanagmentsystem;
public class VehicleManagmentSystem {
public VehicleManagmentSystem() {
}
public static void main(String[] args) {
Vehicle car = new Car("Hyundai", "Sonata", 2024, 2.5, 400.0);
Vehicle bike = new Bike("Yamaha", "YBR", 2020, 125.0, 200.0);
Vehicle truck = new Truck("Mercedes", "Actros", 1993, 7.5, 800.0);
System.out.println(bike + "\nRent for 15 days: " +
bike.calculateRentalFee(15));
System.out.println(car + "\nRent for 15 days: " +
car.calculateRentalFee(15));
System.out.println(truck + "\nRent for 15 days: " +
truck.calculateRentalFee(15));
}
}
4
OUTPUT
Task 2:
package estore;
/**
*
* @author sp22-bai-038
*/
public class Product {
private int productID;
private String name;
private double price;
public Product(int productID, String name, double price) {
this.productID = productID;
this.name = name;
this.price = price;
5
}
public int getProductID() {
return productID;
}
public void setProductID(int productID) {
this.productID = productID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
// TypeCast
Product product = (Product) obj;
// Compare the productID of the current object with the passed object
return productID == product.productID;
}
@Override
public String toString() {
return "Product ID: " + productID + "\nName: " + name + "\nPrice: " +
price;
}
public double calculateDiscount(double discountPercentage) {
return price - (price * discountPercentage / 100);
6
}
}
package estore;
/**
*
* @author sp22-bai-038
*/
public class Food extends Product {
public Food(int productID, String name, double price) {
super(productID, name, price);
}
@Override
public double calculateDiscount(double discountPercentage) {
if (discountPercentage == 0) {
discountPercentage = 5;
}
return super.calculateDiscount(discountPercentage);
}
}
package estore;
/**
*
* @author sp22-bai-038
*/
public class Electronics extends Product {
public Electronics(int productID, String name, double price) {
super(productID, name, price);
}
@Override
public double calculateDiscount(double discountPercentage) {
if (discountPercentage == 0) {
discountPercentage = 15;
}
return super.calculateDiscount(discountPercentage);
}
7
}
package estore;
/**
*
* @author sp22-bai-038
*/
public class Clothing extends Product {
public Clothing(int productID, String name, double price) {
super(productID, name, price);
}
@Override
public double calculateDiscount(double discountPercentage) {
if (discountPercentage == 0) {
discountPercentage = 20;
}
return super.calculateDiscount(discountPercentage);
}
}
package estore;
/**
*
* @author sp22-bai-038
*/
public class Estore {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Product product1 = new Product(101, "Generic Product", 100.0);
Electronics electronics1 = new Electronics(102, "Laptop", 1200.0);
Clothing clothing1 = new Clothing(103, "Jacket", 80.0);
Food food1 = new Food(104, "Pizza", 15.0);
System.out.println(product1);
System.out.println("-------------------------------");
System.out.println(electronics1);
8
System.out.println("-------------------------------");
System.out.println(clothing1);
System.out.println("-------------------------------");
System.out.println(food1);
System.out.println("\nDiscounted prices:");
System.out.println("Product1 discounted price (10%): " +
product1.calculateDiscount(10));
System.out.println("Electronics discounted price (default 15%): " +
electronics1.calculateDiscount(0));
System.out.println("Clothing discounted price (default 20%): " +
clothing1.calculateDiscount(0));
System.out.println("Food discounted price (default 5%): " +
food1.calculateDiscount(0));
Product product2 = new Product(101, "Another Product", 100.0);
System.out.println("\nAre product1 and product2 equal? " +
product1.equals(product2));
}
9
OUTPUT:
Task 3:
public abstract class Appliance {
private String brand; // Data member for brand
// Constructor
public Appliance(String brand) {
this.brand = brand;
}
// Getter for brand
public String getBrand() {
return brand;
}
// Setter for brand
public void setBrand(String brand) {
10
this.brand = brand;
}
// Abstract method to be implemented by subclasses
public abstract boolean isEnergyEfficient();
}
public class Refrigerator extends Appliance {
private int energyRating;
public Refrigerator(String brand, int energyRating) {
super(brand);
this.energyRating = energyRating;
}
public int getEnergyRating() {
return energyRating;
}
public void setEnergyRating(int energyRating) {
this.energyRating = energyRating;
}
@Override
public boolean isEnergyEfficient() {
return energyRating > 4;
}
}
public class WashingMachine extends Appliance {
private double waterConsumption; // Data member for water consumption
// Constructor
public WashingMachine(String brand, double waterConsumption) {
super(brand); // Call superclass constructor
this.waterConsumption = waterConsumption;
}
// Getter for waterConsumption
public double getWaterConsumption() {
return waterConsumption;
}
11
// Setter for waterConsumption
public void setWaterConsumption(double waterConsumption) {
this.waterConsumption = waterConsumption;
}
// Override the isEnergyEfficient method
@Override
public boolean isEnergyEfficient() {
// Return true if waterConsumption is less than 50
return waterConsumption < 50;
}
}
public class ElectronicsSystem {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Create objects of WashingMachine and Refrigerator
WashingMachine wm = new WashingMachine("Samsung", 45); // Water
consumption is 45
Refrigerator fridge = new Refrigerator("LG", 5); // Energy rating is 5
// Display information and check energy efficiency
System.out.println("Washing Machine Brand: " + wm.getBrand());
System.out.println("Water Consumption: " + wm.getWaterConsumption());
System.out.println("Is Energy Efficient: " + wm.isEnergyEfficient());
System.out.println("\nRefrigerator Brand: " + fridge.getBrand());
System.out.println("Energy Rating: " + fridge.getEnergyRating());
System.out.println("Is Energy Efficient: " + fridge.isEnergyEfficient());
}
}
OUTPUT:
12
Task 4:
public abstract class Employee {
protected String name;
protected double salary;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public abstract double calculateBonus();
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
public void printDetails() {
System.out.println("Name: " + name + ", Salary: " + salary);
}
}
13
public class Executive extends Employee {
private double stockOptions;
public Executive(String name, double salary, double stockOptions) {
super(name, salary);
this.stockOptions = stockOptions;
}
@Override
public double calculateBonus() {
return salary * 0.15 + stockOptions * 50;
}
public void leadCompany() {
System.out.println(name + " is leading the company with " + stockOptions
+ " stock options.");
}
public double getStockOptions() {
return stockOptions;
}
}
public class Manager extends Employee {
private int teamSize;
public Manager(String name, double salary, int teamSize) {
super(name, salary);
this.teamSize = teamSize;
}
@Override
public double calculateBonus() {
return salary * 0.10 + teamSize * 100;
}
public void manageTeam() {
System.out.println(name + " is managing a team of " + teamSize + "
members.");
}
public int getTeamSize() {
return teamSize;
14
}
}
public class CompanySystem {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Employee emp1 = new Manager("Ali", 80000, 10);
Employee emp2 = new Executive("Babar", 120000, 500);
Employee[] employees = {emp1, emp2};
for (Employee emp : employees) {
System.out.println("\nEmployee: " + emp.getName());
System.out.println("Salary: " + emp.getSalary());
System.out.println("Bonus: " + emp.calculateBonus());
if (emp instanceof Manager) {
Manager manager = (Manager) emp;
manager.manageTeam();
} else if (emp instanceof Executive) {
Executive executive = (Executive) emp;
executive.leadCompany();
}
}
}
OUTPUT:
15
16