Lab Assignment
Lab Assignment
Programming
LAB TASK
public Vehicle() {
this.make = "NULL";
this.model = "NULL";
this.year = 0;
}
1
public double fuelEfficiency(double avg) {
return avg;
}
package vehiclemanagmentsystem;
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;
}
2
// Source code is decompiled from a .class file using FernFlower decompiler.
package vehiclemanagmentsystem;
public Car(String mak, String mou, int y, double ed, double wei) {
super(mak, mou, y);
this.engineDisplacement = ed;
this.weight = wei;
}
package vehiclemanagmentsystem;
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;
}
package vehiclemanagmentsystem;
4
OUTPUT
Task 2:
package estore;
/**
*
* @author sp22-bai-038
*/
public class Product {
private int productID;
private String name;
private double price;
5
}
@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;
}
6
}
}
package estore;
/**
*
* @author sp22-bai-038
*/
public class Food extends Product {
@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 {
@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 {
@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));
9
OUTPUT:
Task 3:
// Constructor
public Appliance(String brand) {
this.brand = brand;
}
10
this.brand = brand;
}
@Override
public boolean isEnergyEfficient() {
return energyRating > 4;
}
}
// Constructor
public WashingMachine(String brand, double waterConsumption) {
super(brand); // Call superclass constructor
this.waterConsumption = waterConsumption;
}
11
// Setter for waterConsumption
public void setWaterConsumption(double waterConsumption) {
this.waterConsumption = waterConsumption;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
OUTPUT:
12
Task 4:
13
public class Executive extends Employee {
private double stockOptions;
@Override
public double calculateBonus() {
return salary * 0.15 + stockOptions * 50;
}
@Override
public double calculateBonus() {
return salary * 0.10 + teamSize * 100;
}
/**
* @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);
OUTPUT:
15
16