OOP7
OOP7
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.
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.
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.]
PRACTICAL ASSIGNMENT - 6
Date : 26/02/2024
Aim : Implementing Static Polymorphism using Method Overloading
Overload the calcCGPA() method which calculates the CGPA based on different input values and
different formulas.
Create a main class called CalcCGPA and calculate the CGPA through all the methods.
PRACTICAL ASSIGNMENT - 5
Date : 15/02/2024
PART 1
Aim : Implementing Multilevel 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]
PART 2
Aim : Implementing Hierarchical Inheritance
Create following classes and relationships with their respective data members.
PRACTICAL ASSIGNMENT - 4
Date : 05/02/2024
Aim : Implementing Simple Inheritance
SIMPLE
Implement OOP in java using the following information.
Create the class called “Customer” as follows with given accessibility for its members:
Data member:
+ Name
+ Age
+ Contact
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.)
↑
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 )
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.
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.
SELF REFERENCE :
Errors and Exceptions :
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.
PRACTICAL ASSIGNMENT - 1
Date : 13/01/2024
Aim : Working with classes, objects and constructors
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
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.
type
colour
brand
price
Create at least 2 different objects of Pen class, setting different attributes for them and
print their details.
num[]
Create at least 2 different objects of the ArraySum class and print the sum of both.