0% found this document useful (0 votes)
4 views

Lab#2 Journal Waleed Khalid [OOP]

The document outlines lab tasks for an Object Oriented Programming course at NUTECH University, detailing the creation of three classes: Rectangle, Employee, and Account. Each class includes attributes, constructors, and methods to perform specific functionalities, such as calculating area and perimeter, managing employee salary based on hours worked, and handling account deposits and withdrawals. Sample Java code is provided for each class to demonstrate their implementation and functionality.

Uploaded by

apex.wali3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab#2 Journal Waleed Khalid [OOP]

The document outlines lab tasks for an Object Oriented Programming course at NUTECH University, detailing the creation of three classes: Rectangle, Employee, and Account. Each class includes attributes, constructors, and methods to perform specific functionalities, such as calculating area and perimeter, managing employee salary based on hours worked, and handling account deposits and withdrawals. Sample Java code is provided for each class to demonstrate their implementation and functionality.

Uploaded by

apex.wali3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Student Name | Waleed Khalid | F24610019

Object Oriented Programming Lab


Lecturer Muntaha Noor

NUTECH UNIVERSITY ISLAMABAD


LAB#2 - Journal

March 2, 2025
LAB TASK # 1: Create a Rectangle Class with
• Attributes – length and width
• Constructor – to initialize the attributes
• Methods – calArea(length, width), calPerimeter(length, width)
• calArea(length, width) – Calculate the area of the rectangle and
display it
• calPerimeter(length, width) – Calculate the perimeter of the rectangle
and
display it
• Write main method to check the functionality of the class

Program:
public class Rectangle {
private double length;
private double width;

public Rectangle(double length, double width) {


this.length = length;
this.width = width;
}

public void calArea() {


double area = length * width;
System.out.println("Area of Rectangle: " + area);
}

public void calPerimeter() {


double perimeter = 2 * (length + width);
System.out.println("Perimeter of Rectangle: " + perimeter);
}

public static void main(String[] args) {


Rectangle rect = new Rectangle(5, 3);
rect.calArea();
rect.calPerimeter();
}
}

Post Lab Task # 1: Create a Employee Class with


• Attributes – name, salary, hours of work
• Constructor – to initialize the attributes
• Methods – showInfo(), addToSalary(amount),
deductFromSalary(amount)
• showInfo() – display whole information of Employee
• addToSalary(amount) – increase salary by 20% if hours of work is
greater
than 8
• deductFromSalary(amount) – deduct 5% if hours of work is less than 5
• Write main method to check the functionality of the class

Program:
public class Employee {
private String name;
private double salary;
private int hoursWorked;

public Employee(String name, double salary, int hoursWorked) {


this.name = name;
this.salary = salary;
this.hoursWorked = hoursWorked;
}

public void showInfo() {


System.out.println("Name: " + name);
System.out.println("Salary: " + salary);
System.out.println("Hours Worked: " + hoursWorked);
}

public void addToSalary() {


if (hoursWorked > 8) {
salary *= 1.2; // Increase salary by 20%
}
}

public void deductFromSalary() {


if (hoursWorked < 5) {
salary *= 0.95; // Deduct 5%
}
}

public static void main(String[] args) {


Employee emp = new Employee("John Doe", 50000, 9);
emp.showInfo();
emp.addToSalary();
emp.showInfo();
}
}

Post Lab Task # 2:Create an Account Class with


• Attributes – name and balance
• Constructor – to initialize the attributes
• Methods – displayInfo(), deposit(amount), withdraw(amount)
• displayInfo() – Display the account holder&#39;s name and current
balance
• deposit(amount) – Add the specified amount to the account balance
and
display the updated balance
• withdraw(amount) – Check if the account has sufficient funds to
withdraw
the specified amount. If there are enough funds, withdraw the amount
and
display the remaining balance. If there are insufficient funds, display a
message indicating so

• Write main method to check the functionality of the class

Program:
public class Account {
private String name;
private double balance;

public Account(String name, double balance) {


this.name = name;
this.balance = balance;
}

public void displayInfo() {


System.out.println("Account Holder: " + name);
System.out.println("Current Balance: " + balance);
}

public void deposit(double amount) {


balance += amount;
System.out.println("Updated Balance after deposit: " + balance);
}
public void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
System.out.println("Remaining Balance after withdrawal: " + balance);
} else {
System.out.println("Insufficient funds for withdrawal.");
}
}
public static void main(String[] args) {
Account acc = new Account("Alice", 1000);
acc.displayInfo();
acc.deposit(500);
acc.withdraw(300);
acc.withdraw(1500);
}
}

You might also like