You are on page 1of 12

MAJOR-2 : Object Oriented Programming Methodology

Practical Assignments
FY CS / IT Sem - 2

PRACTICAL ASSIGNMENT - 7
Date : 29/02/2024
Aim : Implementing Dynamic Polymorphism using Method Overriding

A. Create a class named SingleTennisGame that holds data about a single tennis game.

The class has mentioned fields: player1_name, player2_name, player1_score,


player2_score, Result (values can be- “team1 win”, “team2 win”).

Include get methods for each of the fields. Also include setNames() method that accepts
two players’ names from the user, and another setScores() method that accepts the two
integer score values.

The score for a player is the number of points the player won during the game; this value
should be in the range of 0 through 4. If either of the setScores() method parameters is not
in the range of 0 through 4, assign 0 to both scores and “error” to the Result.

If both players’ score parameters are 4, assign 0 to both scores and “tie” to the Result. The
Result value(“team1 win”, “team2win”) is also set by the setScores() method based on the
scores of both players.

B. Create a derived class named DoublesTennisGame.

Override the parent class setNames() method to accept the names of two team names
instead of two individual player names. Include get methods for the names.

Write logic to accept user input and display the details of one SingleTennisGame and one
DoubleTennisGame.

Note: Ask the user whether he wants to enter details for single tennis game or double
tennis game and implement runtime polymorphism accordingly.

[HINT : Define the object of the parent class, but instantiate it according to the user's
choice.]

Prepared by: Prof. Lavleena Stephens


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

PRACTICAL ASSIGNMENT - 6
Date : 26/02/2024
Aim : Implementing Static Polymorphism using Method Overloading

Consider you are developing an application for calculating CGPA of students.


Create a Student class which accepts basic details of a Student like enr_no, s_name, dept.
[Marks obtained should be between 0 and 500.]

Overload the calcCGPA() method which calculates the CGPA based on different input values and
different formulas.

calcCGPA() with Percentage as a parameter -


CGPA= Percentage / 9.5

calcCGPA() with Total Grade Point Obtained as a parameter -


CGPA= Total Grade Point Obtained / No. of Subjects
[Grade Points must be numbers between 0 and 50, Total Subjects = 5]

calcCGPA() with SGPA of both sem1 & sem2 as parameters-


CGPA = SGPA of sem1 + SGPA of sem2 / Number of semesters
[SGPA must be between 1 and 10]

Create a main class called CalcCGPA and calculate the CGPA through all the methods.

Prepared by: Prof. Lavleena Stephens


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

PRACTICAL ASSIGNMENT - 5
Date : 15/02/2024
PART 1
Aim : Implementing Multilevel Inheritance

1. Implement the following inheritance :

2. Using an array of objects, add and display data of at least 3 Under Graduate Students.
Input the values of student’s name, phone number, email address, enrollment number,
course and total marks scored.
[NOTE : CGPA should not be input, but must be calculated using calcCGPA]
[CGPA = TotalMarks x 10 / 500 , TotalMarks should be from 0 to 500 only]

Prepared by: Prof. Lavleena Stephens


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

PART 2
Aim : Implementing Hierarchical Inheritance

Create following classes and relationships with their respective data members.

1. Create objects of Savings A/c and Current A/c.


2. Deposit Rs 5000 in Savings A/c.
3. Deposit Rs 10000 in Current A/c.
4. Find Monthly interest received based on Interest rate 10% on Savings A/c.
5. Find Monthly interest received based on Interest rate 15% on Current A/c.
6. Withdraw from Savings A/c (the amount taken from the user). Do not allow withdrawal if
account balance is less than Rs. 1000.
7. Withdraw from Current A/c (the amount taken from the user). Do not allow withdrawal if
account balance is less than Rs. 10000.

Prepared by: Prof. Lavleena Stephens


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

PRACTICAL ASSIGNMENT - 4
Date : 05/02/2024
Aim : Implementing Simple Inheritance

NOTE: Create MENU DRIVEN application

SIMPLE
Implement OOP in java using the following information.

Create the class called “Customer” as follows with given accessibility for its members:

(NOTE: + indicates Protected, * indicates Public, ++ indicates private)


Class: Customer

Data member:
+ Name
+ Age
+ Contact

Note: Create a constructor to initialize the data members


Member functions:
* getCustomers()
* showCustomers()

MODERATE
Extend the above program to add the following functionalities:

1) Create a class account “Account ” which will inherit the customer class.
(Use the keyword extends for inheritance.)

Prepared by: Prof. Lavleena Stephens


MAJOR-2 : Object Oriented Programming Methodology
Practical Assignments
FY CS / IT Sem - 2
Class: Customer

Data member: pro


+Name
+ Age
+contact

Member functions: pub


*getCustomers()
*showCustomers()


Class: Account

Data members:
+Accno
+bal

Member functions:
*deposit()
*withdraw()
*transfer()

2) Input and display user details like name, age, contact, accno, and bal for n
customers. Implement an array of objects.
[HINT: Create object of child class only.]

Expected Input:
Welcome! Enter following details:
Name: Peter Andrews
Age: 30
Contact Number: 4478962586
AccountNo: 111
Balance: 20000
(Repeat taking input for details of n customers)

Expected Output:
Name Age Contact Number Account Number Balance
_________________________________________________________________
Peter Andrews 30 4478962586 111 20000
Emma Stone 30 3456345675 222 30000
(Show details of n customers )

Prepared by: Prof. Lavleena Stephens


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

ADVANCED
1) Allow the user to deposit some amount in the account. Display the reflected balance in the
account after the transaction.
Expected Input:
Enter AccountNo: 111
Enter the amount you wish to deposit: 10000
Expected Output:
Transaction Successful!
AccountNo: 111
Final balance: 30000

2) Allow the user to withdraw some amount from the account, only if the account balance is
greater than 0 and if the withdrawal amount is less than the account balance. Display the
final balance in the account after the transaction.
Expected Input:
Enter AccountNo: 111
Enter the amount you wish to withdraw: 30000
Expected Output:
Sorry! You don’t have a sufficient balance in your account.

Expected Input:
Enter AccountNo: 111
Enter the amount you wish to withdraw: 10000
Expected Output:
Transaction Successful!
AccountNo: 111
Final balance: 10000

3) Allow the user to transfer some amount from his account to another account, only if his
account balance is greater than 0 and if the transfer amount is less than the account balance.
Display the final balance of both accounts after the transaction.
Expected Input:
Enter your AccountNo: 111
Enter AccountNo you wish to transfer: 222
Enter the amount you wish to transfer: 30000
Expected Output:
Sorry! You don’t have a sufficient balance in your account.

Prepared by: Prof. Lavleena Stephens


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

Expected Input:
Enter your AccountNo: 111
Enter AccountNo you wish to transfer: 222
Enter the amount you wish to transfer: 10000
Expected Output:
Transaction Successful!
AccountNo: 111 Balance: 10000
AccountNo: 222 Balance: 40000

PRACTICAL ASSIGNMENT - 3
Date : 25/01/2024
Aim : Implementing Exception Handling

PART 1
1. Write a program in Java to accept two numbers from the user. Raise the
ArithmeticException when the user tries to divide num1 by num2, and num2 is zero.
2. Write a java program which accepts the elements of an integer array of size n, and displays
the same. Catch the ArrayIndexOutOfBoundsException whenever the user tries to access
the array outside of its bound (range).

PART 2
Create the following exceptions, with reference to the Student class created in previous
assignments:
1. If the user tries to input a String value for the s_id, throw the InputMismatchException
and print the message “Invalid Input !” in the catch block.
2. If the user enters total_marks less than zero or greater than 500, throw a user-defined
exception named “InvalidMarksException”.
3. Finally, print the message “Student details entered!”, regardless of if any exception is
raised or not.

Prepared by: Prof. Lavleena Stephens


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

SELF REFERENCE :
Errors and Exceptions :

Process of Exception Handling :

Creating User-defined Exceptions :


1) Create your Exception class, which inherits the base class “Exception”.
2) Add constructors in your custom exception class.
3) Throw the exception using the throw keyword.
4) Handle the exception using its suitable catch block.

Prepared by: Prof. Lavleena Stephens


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

PRACTICAL ASSIGNMENT - 2
Date : 23/01/2024
Aim : Implementing data hiding & getter and setter methods.
Working with array of objects.

PART 1
1. Implement data hiding for the Student class earlier created in Assignment-1.
[make all attributes private.]
2. Create respective getter and setter methods for all attributes.
[make all getter-setter methods public so that attributes can be accessed outside the class.]
3. Now, initialize the instance variables of objects using the common setter method.
4. Print the student attributes using their respective getter methods. [in tabular format]

PART 2
1. Using the Student class as a template, create an array of objects of size 2.
Print the details of all students. [Use Static values]
2. Implement a dynamic array of objects of size n using user input values.
Print the details of all students.

Prepared by: Prof. Lavleena Stephens


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

PRACTICAL ASSIGNMENT - 1
Date : 13/01/2024
Aim : Working with classes, objects and constructors

1. Implement the following structure :

Student

s_id
s_name
dob
dept
total_marks (out of 500)
perc

Student() : a constructor
Student() : a constructor to initialize all instance variables
calcPerc() : calculate the perc of a student
isEligibleToEnroll() : print the eligibility message
getStudentDetails() : print all details of a student

If the default constructor is used, then the value must be set to “IT”.
The calcPerc() method is used to calculate the percentage, based on total_marks input by
the user.
The isEligibleToEnroll() method will print “Eligible” if the percentage is above 65, else it
will print “Not Eligible”.

Enter values for at least 2 students and print the details in a tabular form.
[Static input will not be considered.]
Expected Output :
Id Name DOB Dept Perc Eligibility
1 Charles 17/03/2004 IT 72 Eligible
2 Mark 22/08/2003 MB 55 Not Eligible

Prepared by: Prof. Lavleena Stephens


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

PRACTICAL ASSIGNMENT - 0
Date : 08/01/2024
Aim : Introduction to OOP & revisiting Java Fundamentals

1. Create a user defined class named Area. It should contain the following methods:
circle() : to print area of a circle
square() : to print area of a square
triangle() : to print area of a triangle
[NOTE : define PI as a final variable in Area class]
Create an object, initialize necessary parameters within the concerned methods, and print
the area of all shapes in output.

2. Implement the following class structure in the program :


Pen

type
colour
brand
price

setPenDetails() : set all details of a pen


printPenDetails() : print all details of a pen

Create at least 2 different objects of Pen class, setting different attributes for them and
print their details.

3. Implement the following class structure in the program :


ArraySum

num[]

setElements(int[] array) : initialize array elements


printSum() : prints the sum of array elements

Create at least 2 different objects of the ArraySum class and print the sum of both.

Prepared by: Prof. Lavleena Stephens

You might also like