OOPM Practice
OOPM Practice
Practice Assignments
FY CS / IT Sem ‐ 2
PRACTICE ASSIGNMENT ‐ 8
HYBRID INHERITANCE
Implement the following :
A. By implementing the method writeJavaCode(), the message “e_name is writing Java code.” should be
displayed. [display the employee’s name in place of e_name]
B. By implementing the method writePythonCode(), the message “e_name is writing Python code.” should
be displayed. [display the employee’s name in place of e_name]
C. For the attribute pr_type, accept values like Academic, E‐Commerce, Business, etc.
D. Maintain and display the details of at least 10 projects using an array of objects.
Sample output :
Source
PRACTICE ASSIGNMENT ‐ 7
MULTIPLE INHERITANCE
A. 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 EmployeeName and basic_salary.
B. The Salary class has data member HRA and method disp( ). The Salary class calculates the gross salary of
an Employee and displays the information. [Gross Salary = Basic sal+TA+DA+HRA]
C. Maintain the information for 3 employees.
PRACTICE ASSIGNMENT ‐ 6
HIERARCHICAL INHERITANCE
A. Create a class named “Shape” in your application.
Define a child class called “Circle” which inherits the parent class “Shape”.
Add following data members :
Radius
Add following member functions :
Circle(double radius)
‐ This constructor will initialize the value of the radius input by the user.
CirArea()
‐ This function will return the area of the circle.
Define another child class “Rectangle” which inherits the class “Shape”.
Add following data members :
Length
Width
2) Shape(Rectangle r)
‐ This constructor would accept the object of the class Rectangle as an input.
‐ Call the member function RectArea() inside this function.
Example :
rectangleArea(Rectangle r) {
r.rectArea(); }
PRACTICE ASSIGNMENT ‐ 5
MULTI‐LEVEL INHERITANCE
Suppose you are creating an application for Result Management.
Create a base class Student. Create a derived class named Marks which inherits Student class.
Create a class name Result which inherits Marks class.
Student class
Attributes :
Enroll_number
Name
YearOfAdmission
Marks class
Attributes :
Marks[] (int array of size 3 : consisting of 3 subjects out of 100)
TotalMarks
Result class
Attributes :
Perc
Grade
Methods :
calcGrade() : method which calculates Perc and Grade.
displayDetails() : method to display all details of student
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
PRACTICE ASSIGNMENT ‐ 4
SIMPLE INHERITANCE
A. Create an application named “Calc”. Within the application, create a class named Arithmetic with
following members:
Member functions :
add(int a, int b)
sub(int a, int b)
mul(int a, int b)
div(int a, int b)
C. Within the main class, create an object for child class only.
Initialize num1 and num2 using the parameterized constructor of Number class.
Write a menu driven program to perform all the arithmetic operations using functions defined in the
Arithmetic class and display the results.
PRACTICE ASSIGNMENT ‐ 3
EXCEPTION HANDLING
1. Write a program to demonstrate the NullPointerException. Try accessing the 2nd character of a null
String. Handle the exception by displaying the message “String is null !” to the user.
2. Write a program to accept the value of any String from the user. Try accessing a character at any index.
Throw the StringIndexOutOfBoundsException if the index number is illegal. Display the message “String
is null !” to the user when the exception is thrown.
[NOTE : index number can not be negative or greater than the String length.]
3. Write a program to accept any number from the user. Throw the NumberFormatException if the user
tries to enter invalid value.
4. Write a program to accept login credentials (username & password) from the user. Raise a user‐defined
exception called InvalidLogin if the login credentials do not match. Prompt the user to login again by
showing the message “Invalid login credentials, try again !”.
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
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 for cheese toppings, 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.
EXAMPLE :
Creating objects :
Employee em1 = new Employee();
Employee em2 = new Employee();
PART ‐ 2
Aim : Understanding object construction and destruction in JAVA.
Implementing different types of constructors.
1) Employee()
2) Employee(EmpName)
3) Employee(EmpName, Gender, DOB, Dept, Salary)
Prepared by: Prof. Lavleena Stephens
MAJOR‐2 : Object Oriented Programming Methodology
Practice Assignments
FY CS / IT Sem ‐ 2
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 earlier and display the Employee details.
PART ‐ 3
Aim : Implementing data hiding. Working with array of objects.
SOLUTION
PART ‐ 1
PART ‐ 2
1. Create the mentioned constructors in the same Employee class.
PART ‐ 3
FUNDAMENTALS
1. Create a class named Vehicle with following members. Create mentioned objects with suitable values.
Attributes: v_id, v_type, noOfWheels, price
Methods: start() , brake() , changeGear()
Objects: bike, car, bus