0% found this document useful (0 votes)
35 views4 pages

OOP Assignment 1

Uploaded by

abdullahsami675
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)
35 views4 pages

OOP Assignment 1

Uploaded by

abdullahsami675
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/ 4

Object Oriented Programming

Assignment:1

Topic: Classes, Objects and Constructors

Task#1:
Create a program to initialize employee details with different parameters
using constructor overloading, and test it using a separate class.

Task Details:
1. Define a class Employee with the following attributes:
• String name
• int id
• double salary
• String department
2. Constructor Overloading:
• Default Constructor: Initializes name to "Unknown", id to 0, salary to 0.0,
and department to "Not Assigned".
• Parameterized Constructor (1): Takes only name and id as
parameters and initializes salary and department with default values.
• Parameterized Constructor (2): Takes name, id, and salary as parameters
and sets department to "Not Assigned".
• Full Parameter Constructor: Takes all four attributes as parameters and
initializes each attribute.
3. Method: displayInfo()

• Add a method displayInfo () to print all attributes of the Employee object. 4.


Testing the Program:
• Create a separate class named EmployeeTest in a new file
(EmployeeTest.java) to test the Employee class.
• In EmployeeTest, create at least three different Employee objects using
different constructors, and call displayInfo() to display each employee's
details.
Task#2:
Create a BankAccount class that manages a user's account details with a focus
on initializing account information using overloaded constructors. Test the class
with a separate test class.

Task Details:
1. Define a class BankAccount with the following attributes:
• String accountHolder
• String accountNumber
• double balance
• String accountType (such as "Savings" or "Current")
2. Constructor Overloading:
• Default Constructor: Initializes accountHolder to "Unknown",
accountNumber to "0000", balance to 0.0, and accountType to
“Savings".
• Parameterized Constructor (1): Accepts only accountHolder and
accountNumber and initializes balance to 0.0 and accountType to
“Savings".
• Full Parameter Constructor: Takes all four attributes as parameters and
initializes each one.
3. Method: deposit (double amount)

• Adds the amount to balance.


• Ensure that the amount is positive, otherwise print an error message.
4. Method: withdraw (double amount)

• Deducts the amount from balance if the balance is sufficient.


• Ensure that the amount is positive and less than or equal to balance,
otherwise print an error message.
5. Method: displayAccountInfo ()

• Prints all details of the account, including the balance.


6. Testing the Program:
• Create a separate class named BankAccountTest in a new file
(BankAccountTest.java) to test the BankAccount class.
• In BankAccountTest, create two different BankAccount objects using
different constructors. Perform deposit and withdrawal operations on both
accounts and display their information after each operation.
Lab Task 3:
Product Inventory Management with Constructor, Control
Flow, and Looping

Objective: Create a Product class for managing inventory, including an update method.
Test the functionality with a test class.

Tasks:

1. Class Definition: Define a Product class with attributes productName, productId,


quantity, and price.

2. Constructors:
• Implement a default constructor that sets attributes to default values.
• Implement a parameterized constructor that accepts initial values for all
attributes.

Method:
• Write an update_Quantity () method that adjusts the quantity based on an
action ("add" or "remove"). Ensure that quantity does not go below zero.

Test Class:
 Create a separate test file, ProductTest.java.
 Use a loop to add or remove products: prompt the user to enter product
details and specify an action.
 After each update, display the updated inventory.
 Exit the loop if the user chooses not to manage more products.

Lab Task 4: Calculate the performance_Score and bonus of an


employee

1. Class Definition: Define an Employee class with attributes name, id, salary,
and performanceScore.
2. Constructors:
• Write a default constructor that initializes each attribute to default
values. • Write a parameterized constructor to set all attributes.
3. Method:
• Implement a calculateBonus() method that returns a bonus percentage of
salary based on performanceScore:
• 90 and above: 20%
• 70–89: 10%
• Below 70: 5%
4. Test Class:
• Create a separate test file, EmployeeTest.java.
• Use a loop to process multiple employees: prompt the user for name, id,
salary, and performanceScore.
• Calculate and display each employee’s bonus and total salary (salary + bonus).
Lab Task 5:

Student Grading System with Constructor, Control, and Loop Structures


Objective: Create a Student class with attributes, constructors, a grading method, and
implement a test class to validate the grading functionality.
Tasks:
1. Class Definition: Create a Student class with attributes name, id, and
score. 2. Constructors:
• Implement a default constructor that initializes attributes to default values. •
Implement a parameterized constructor that allows initializing all attributes
when a Student object is created.
3. Method:
• Write a getGrade() method in Student that determines the grade based on
score using the following scale:
• 90 and above: "A"
• 80–89: "B"
• 70–79: "C"
• 60–69: "D"
• Below 60: "F"
4. Test Class:
• Create a separate test file, StudentTest.java, to test the Student class. •
Use a loop to create multiple Student objects and display their grades. •
Prompt the user to enter name, id, and score for each student, and display
the calculated grade.

You might also like