0% found this document useful (0 votes)
32 views15 pages

OOPM Practice

The document outlines practice assignments for a course on Object Oriented Programming Methodology, covering various inheritance types including hybrid, multiple, hierarchical, multi-level, and simple inheritance. Each assignment includes specific tasks such as implementing methods, creating classes, handling exceptions, and managing employee or project data. The assignments are designed to reinforce concepts through practical implementation in Java programming.

Uploaded by

pandyaj036
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)
32 views15 pages

OOPM Practice

The document outlines practice assignments for a course on Object Oriented Programming Methodology, covering various inheritance types including hybrid, multiple, hierarchical, multi-level, and simple inheritance. Each assignment includes specific tasks such as implementing methods, creating classes, handling exceptions, and managing employee or project data. The assignments are designed to reinforce concepts through practical implementation in Java programming.

Uploaded by

pandyaj036
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/ 15

MAJOR‐2 : Object Oriented Programming Methodology

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 :

Prepared by: Prof. Lavleena Stephens


MAJOR‐2 : Object Oriented Programming Methodology
Practice Assignments
FY CS / IT Sem ‐ 2
Additional examples to practice :

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.

Prepared by: Prof. Lavleena Stephens


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

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

Add following member functions :


Rectangle(double length, double width)
‐ This constructor will initialize the value of the length and width input by the user.
RecArea()
‐ This function will return the area of the rectangle.

B. In the “Shape” class, define following constructors:


1) Shape(Circle c)
‐ This constructor would accept the object of the class Circle as an input.
‐ Call the member function CirArea() inside this function.
Example :
circleArea(Circle c) {
c.CirArea(); }

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(); }

Prepared by: Prof. Lavleena Stephens


MAJOR‐2 : Object Oriented Programming Methodology
Practice Assignments
FY CS / IT Sem ‐ 2
C. Create objects for both classes Circle and Rectangle. Set the values of their respective attributes
using their respective constructors. Lastly, call the constructors defined in the Shape:
Shape(c);
Shape(r);

Prepared by: Prof. Lavleena Stephens


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

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

Create an object of Result class only.


Input the values for student’s EnrollNumber, Name, YearOfAdmission, Marks of 3 subjects.
Input marks out of 100 for each subject. (Check that user does not enter marks greater than 100 or less than 0)
Calculate total marks, percentage and grade; and display Student results.
Maintain and display records for 10 students in proper format.

Prepared by: Prof. Lavleena Stephens


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

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)

B. Create another class Number which inherits the class Arithmetic.


Add following members to this class :
Data members : num1, num2
Create a common getter method and a common setter method. Also create a parameterized
constructor which would initialize values of num1 and num2.

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.

Prepared by: Prof. Lavleena Stephens


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

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

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 getdescription() which prints the size of pizza and the number of
Toppings.
Prepared by: Prof. Lavleena Stephens
MAJOR‐2 : Object Oriented Programming Methodology
Practice Assignments
FY CS / IT Sem ‐ 2

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.

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 earlier, create following


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.

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 employees. [Use Static values]
D. Implement a dynamic array of objects of size n using user input values.
Print the details of all employees.

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
1. Create the mentioned constructors in the same Employee class.

Prepared by: Prof. Lavleena Stephens


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

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


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

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

2. Create a class named Car with following members :


Attributes: ModelName, Manufacturer
Methods: run(), brake()
In the default constructor, the manufacturer should be set to “Toyota”.
In the parameterized constructor, initialize all instance variables.
Create getter and setter methods for all attributes.
Create at least 3 distinct Car objects.

3. Create a class named Child with following members:


Attributes: Name, Age, Class
Methods: study() , eat() , play() , sleep()
In the default constructor, the Class should be set to “2”.
In the parameterized constructor, initialize all attributes.
Create getter and setter methods for all attributes, and common getter and setter methods with all
attributes.
Accept the values of 3 children using an array of objects, and display their details.

Prepared by: Prof. Lavleena Stephens

You might also like