Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

MAJOR-2 : Object Oriented Programming Methodology

Practice Assignments
FY CS / IT Sem - 2

PRACTICE ASSIGNMENT - 5
Level: Basic

Q-1 Write a java program which has base class Student and a derived class named INTERNAL
which input marks of 3 subjects and calculate percentage and grade. Display Student result in
proper format.

Note: Input marks out of 70 (keep validations that user does not marks greater than 70 or
lesser than 0)

Grade is

1) Distinction if percentage is greater and equal to 70

2) First class if percentage is greater and equal to 60 and less than 70

3) Second class if percentage is greater and equal to 50 and less than 60

4) Pass class if percentage is greater and equal to 40 and less than 50

5) Fail if percentage less than 40


Prepared by: Prof. Lavleena Stephens
MAJOR-2 : Object Oriented Programming Methodology
Practice Assignments
FY CS / IT Sem - 2

Level: Moderate

Q-2 Write a java program which has base class Student , a derived class named MARKS AND
RESULT to input internal and external marks and calculate total marks , percentage and grade.
Display Student result in proper format.

//Note: Input marks out of 70 for Internal (keep validations that user does not marks greater
than 70 or lesser than 0)

Input marks out of 30 for External (keep validations that user does not marks greater than 30 or
lesser than 0)

Prepared by: Prof. Lavleena Stephens


MAJOR-2 : Object Oriented Programming Methodology
Practice Assignments
FY CS / IT Sem - 2
Grade is

1) Distinction if percentage is greater and equal to 70

2) First class if percentage is greater and equal to 60 and less than 70

3) Second class if percentage is greater and equal to 50 and less than 60

4) Pass class if percentage is greater and equal to 40 and less than 50

5) Fail if percentage less than 40

Level: Advanced

Q-3 Perform above Q-2 program for 10 students of the college in the proper format.

PRACTICE ASSIGNMENT - 4

Write a java program to implement following inheritance:


Create an interface Gross having data members TA with value 500 , DA with value 500 and
method gross_sal( ). Also create class Employee which has data members Employee Name and
basic_salary.
The Salary class having data member hra and method disp( ). The Salary class calculates
gross salary of an Employee and display the information.
Maintain the information for 3 employees
Formula: Gross Salary=Basic sal+TA+DA+Hra

Prepared by: Prof. Lavleena Stephens


MAJOR-2 : Object Oriented Programming Methodology
Practice Assignments
FY CS / IT Sem - 2

PRACTICE ASSIGNMENT - 3
To implement Inheritance by developing a java program with Employee class with Emp_name,
Emp_id, Address, Mail_id, Mobile_no as members.
Inherit the classes, Programmer, Assistant Professor, Associate Professor and Professor from
employee class.
Add Basic Pay (BP) as the member of all the inherited classes with 97% of BP as DA, 10 % of BP as
HRA, 12% of BP as PF, 0.1% of BP for staff club fund.
Generate pay slips for the employees with their gross and net salary.

PRACTICE ASSIGNMENT - 2
PART - 1
Create a class named Pizza which stores information about a single pizza. The class contains
following members:
1. Private variables :
size – it can store values like small, medium, large
num_of_cheese_topping – it can take an integer value

2. Constructor(s) that set all of the instance variables.


If no parameter is passed then by default set size =“small”, num_of_cheese_topping = 1
(Hint: so the class will have one parameterized and one default constructor)

3. Public methods to get and set the instance variables.


(Example: getsize(), setsize(), getnum_of_cheese_topping(), …..)

4. Public method calc_bill() which returns cost of pizza


Calculate cost as follows:
Small : 200 + 30rs per topping
Medium : 350 + 40rs per topping
Large : 500 + 50rs per topping
Example: size=medium, num_of_cheese_topping = 2,
Total_cost = 350 + 80(40*2) = 430
5. Public method named getdescription() which prints the size of pizza and the number of
Prepared by: Prof. Lavleena Stephens
MAJOR-2 : Object Oriented Programming Methodology
Practice Assignments
FY CS / IT Sem - 2
Toppings.

PART - 2
1. Within set_num_of_cheese_topping(), if user enters string value for this field then,
print – “Error : Format mismatch” . Note: use InputMismatchException in catch.
2. If user enters any value which is equal to or less than zero then throw user defined
exception and print “Invalid number”.
3. Finally, whether exception is there or not in set_num_of_cheese_topping(), at the end
of the method you should print “Set method called”.

PRACTICE ASSIGNMENT - 1
PART - 1
Aim : Understanding how to reuse the same class for creating multiple objects
and storing multiple values. Implementing getter and setter methods.

A. Create an application named “EmployeeManagement”.


[This should also create an EmployeeManagement class with a main method].
In this application, create a class named “Employee”.
Add following data members :
EmpName
Gender
DOB
Dept
Salary

Add following member functions :


SetEmpDetails(String EmpName, String Gender, String DOB, String Dept, int
Salary)
- This function will set(input) the details of the employee.
GetEmpDetails()
- This will fetch and print all the details of the employee.

Prepared by: Prof. Lavleena Stephens


MAJOR-2 : Object Oriented Programming Methodology
Practice Assignments
FY CS / IT Sem - 2
B. Implement the following :

EmployeeManagement contains the main() method.


Create at least 3 objects for the same class Employee.
Set the values of all data members for all the objects created by using
SetEmpDetails() method.
After assigning the values for all data members, at last call the method
GetEmpDetails() and display the details of the employees.

EXAMPLE :
Creating objects :
Employee em1 = new Employee();
Employee em2 = new Employee();

Assigning the values :


em1.SetEmpDetails(“Rick”,”Male”,”09/12/1987”,”Accounting”,35000);
em2.SetEmpDetails(“Mary”,”Female”,”25/10/1994”,”IT”,45000);

Displaying the details :


em1.GetEmpDetails();
em2.GetEmpDetails();

PART - 2
Aim : Understanding object construction and destruction in JAVA.
Implementing different types of constructors.

A. With reference to application “EmployeeManagement” created in Assignment-3, create


following constructors :

1) Employee()
Prepared by: Prof. Lavleena Stephens
MAJOR-2 : Object Oriented Programming Methodology
Practice Assignments
FY CS / IT Sem - 2
2) Employee(EmpName)
3) Employee(EmpName, Gender, DOB, Dept, Salary)

B. When the default constructor is called, the Dept must be set to “IT” and Salary must be
initialized to “30000”.
C. When the second constructor is called, it should accept only EmpName as a parameter.
D. The third constructor should accept all the data member values as parameters.
E. Finally, use the getter method created in Assignment-3 and display the Employee details.

PART - 3
Aim : Implementing data hiding. Working with array of objects.

A. Implement data hiding for the Employee class created earlier.


[make all attributes private.]
B. Create respective getter and setter methods for all attributes.
[make all getter-setter methods public so that attributes can be accessed outside the class.]
C. Using the Employee class as a template, create an array of objects of size 2.
Print the details of all students. [Use Static values]
D. Implement a dynamic array of objects of size n using user input values.
Print the details of all students.

SOLUTION
PART - 1

1. Create the main class “EmployeeManagement”.

Prepared by: Prof. Lavleena Stephens


MAJOR-2 : Object Oriented Programming Methodology
Practice Assignments
FY CS / IT Sem - 2

2. Within the same class file, create the class Employee and add its data and members.

3. Create objects and implement the getter & setter methods.

Prepared by: Prof. Lavleena Stephens


MAJOR-2 : Object Oriented Programming Methodology
Practice Assignments
FY CS / IT Sem - 2

SAMPLE OUTPUT :

PART - 2
Prepared by: Prof. Lavleena Stephens
MAJOR-2 : Object Oriented Programming Methodology
Practice Assignments
FY CS / IT Sem - 2
1. Create the mentioned constructors in the same Employee class.

2. Initialize objects and print the details in main() method.

Prepared by: Prof. Lavleena Stephens


MAJOR-2 : Object Oriented Programming Methodology
Practice Assignments
FY CS / IT Sem - 2

PART - 3

Prepared by: Prof. Lavleena Stephens

You might also like