DEV Community

rajeshwari rajeshwari
rajeshwari rajeshwari

Posted on

java projeact...

Sure! Here's the Java code for all 4 assignments written in a clean and human-readable style. I’ve included comments to explain each part to make it beginner-friendly.


Assignment 1: Employee – Manager

// Superclass
class Employee {
    protected String name;
    protected int empId;
    protected double basicSalary;

    public Employee(String name, int empId, double basicSalary) {
        this.name = name;
        this.empId = empId;
        this.basicSalary = basicSalary;
    }

    public double calculateSalary() {
        return basicSalary;
    }

    public void displayDetails() {
        System.out.println("Employee ID: " + empId);
        System.out.println("Name: " + name);
        System.out.println("Basic Salary: " + basicSalary);
    }
}

// Subclass
class Manager extends Employee {
    private double bonus;

    public Manager(String name, int empId, double basicSalary, double bonus) {
        super(name, empId, basicSalary);
        this.bonus = bonus;
    }

    @Override
    public double calculateSalary() {
        return basicSalary + bonus;
    }

    @Override
    public void displayDetails() {
        super.displayDetails();
        System.out.println("Bonus: " + bonus);
        System.out.println("Total Salary: " + calculateSalary());
    }
}
Enter fullscreen mode Exit fullscreen mode

Assignment 2: Vehicle – Electric Car

// Superclass
class Vehicle {
    protected String brand;
    protected int year;

    public Vehicle(String brand, int year) {
        this.brand = brand;
        this.year = year;
    }

    public void startEngine() {
        System.out.println("Starting the engine...");
    }

    public void showInfo() {
        System.out.println("Brand: " + brand);
        System.out.println("Year: " + year);
    }
}

// Subclass
class ElectricCar extends Vehicle {
    private int batteryCapacity;

    public ElectricCar(String brand, int year, int batteryCapacity) {
        super(brand, year);
        this.batteryCapacity = batteryCapacity;
    }

    @Override
    public void startEngine() {
        System.out.println("Silently starting electric engine...");
    }

    @Override
    public void showInfo() {
        super.showInfo();
        System.out.println("Battery Capacity: " + batteryCapacity + " kWh");
    }
}
Enter fullscreen mode Exit fullscreen mode

Assignment 3: Bank Account – Savings Account

// Superclass
class BankAccount {
    protected String accountNumber;
    protected String holderName;
    protected double balance;

    public BankAccount(String accountNumber, String holderName, double balance) {
        this.accountNumber = accountNumber;
        this.holderName = holderName;
        this.balance = balance;
    }

    public double calculateInterest() {
        return 0;
    }

    public void displayAccountInfo() {
        System.out.println("Account Number: " + accountNumber);
        System.out.println("Holder Name: " + holderName);
        System.out.println("Balance: " + balance);
    }
}

// Subclass
class SavingsAccount extends BankAccount {
    private double interestRate;

    public SavingsAccount(String accountNumber, String holderName, double balance, double interestRate) {
        super(accountNumber, holderName, balance);
        this.interestRate = interestRate;
    }

    @Override
    public double calculateInterest() {
        return balance * interestRate / 100;
    }

    @Override
    public void displayAccountInfo() {
        super.displayAccountInfo();
        System.out.println("Interest Rate: " + interestRate + "%");
        System.out.println("Calculated Interest: " + calculateInterest());
    }
}
Enter fullscreen mode Exit fullscreen mode

Assignment 5: Product – OnlineOrder

// Superclass
class Product {
    protected String productName;
    protected double price;

    public Product(String productName, double price) {
        this.productName = productName;
        this.price = price;
    }

    public void displayProductDetails() {
        System.out.println("Product Name: " + productName);
        System.out.println("Price: ₹" + price);
    }
}

// Subclass
class OnlineOrder extends Product {
    private int quantity;
    private double deliveryCharge;

    public OnlineOrder(String productName, double price, int quantity, double deliveryCharge) {
        super(productName, price);
        this.quantity = quantity;
        this.deliveryCharge = deliveryCharge;
    }

    public double calculateBillAmount() {
        return price * quantity + deliveryCharge;
    }

    @Override
    public void displayProductDetails() {
        super.displayProductDetails();
        System.out.println("Quantity: " + quantity);
        System.out.println("Delivery Charge: ₹" + deliveryCharge);
        System.out.println("Total Bill: ₹" + calculateBillAmount());
    }
}
Enter fullscreen mode Exit fullscreen mode

If you'd like, I can also give you a single Main class to test all these examples with sample values. Let me know!

Top comments (0)