You are on page 1of 8

Lab Session 4: Classes and Objects

Student Name: Maryam Raza

Depart: BSCS Section A

Exercises
1. Write a class Employee having data members to store the following
information: Name, Father’s

Name, CNIC, Department, Salary and Designation. Write a method void printInfo()
that prints

all information of the employee. Also write a class to test all methods of
Employee class.
2. Write a class Box having data members to store the following information:
Width, Height and Depth

and method to store the volume of a box using formula (width*height*depth).

 Create two objects myBox1 and myBox2 for the above class.
 Print values of width, depth and height for each object.
 Calculate volume for both objects.
3. Write a class Student having attributes to store the following data: Name,
Fathers’ Name,

Department, Degree, Enrolment, Seat number, and Marks of three subjects. Add
the following

methods in the above class.

 int getTotal() : This methods returns the total of marks


 void printTotal() Prints the total marks of Student
 float getPercentage() this method calculates the percentage of a student
 void printPercentatge() this method prints the percentage of student
 char getGrade() this method calculates and returns the grade.
 void printGrade() this method prints the grade of a student.
 void printInfo() This method prints all information of the student.

Also write a class to test all methods of Student class.


4. Create class SavingsAccount. Use a static variable annualInterestRate (type
double) to store the

annual interest rate for all account holders. The class also contains an instance
variable

savingsBalance indicating the amount the saver currently has on deposit. Provide
method

calculateMonthlyInterest() to calculate the monthly interest by multiplying the


savingsBalance by

annualInterestRate divided by 12, this interest should be added to


savingsBalance. Provide a static

method modifyInterestRate() that sets the annualInterestRate to a new value.

Write another class TestSavingsAccount that instantiates two savingsAccount


objects, saver1 and

saver2, with balances of Rs. 20,000.00/- and Rs. 300,000.00/- respectively. Set
annualInterestRate

to 4%, then calculate the monthly interest and print the new balances for both
savers. Then set the

annualInterestRate to 5%, calculate the next month’s interest and print the new
balances for both

savers.

Add more methods in SavingsAccount class if needed.

You might also like