OOPs through Java Lab Programs
LAB 1
1. Write a code to implement switch case with the basic arithmetic operators. The numbers
and options must be entered by the user.
2. Program to find the greatest of three numbers.
LAB 2
3. Write a program to reverse elements of an array.
4. Write a program to count each duplicate elements in an array and delete them.
5. Write a program to reverse right half of an array.
6. Write a program to sort the elements in an array according to even indexes and odd
indexes.
LAB 3
7. Write a program to demonstrate the use of call by value and call by reference.
8. Suppose you are developing a program for a small grocery store. Your program will have
a GroceryStore class that represents the store and a Product class that represents a
product sold in the store.
❖ The Product class should have the following instance variables:
➢ name (a string)
➢ price (a double)
➢ quantity (an integer)
❖ The GroceryStore class should have the following static variables:
➢ numProducts (an integer)
➢ totalRevenue (a double)
❖ The GroceryStore class should also have the following methods:
➢ addProduct(Product p): adds a Product object to the store's inventory.
➢ sellProduct(Product p, int quantity): removes a specified quantity of a
product from the store's inventory and adds the revenue from the sale to
the store's total revenue.
➢ displayStats(): displays the number of products in the store's inventory
and the store's total revenue.
❖ Write a Java program that creates a GroceryStore object and adds several
Product objects to the store's inventory. The program should use
method/constructor overloading to create different Product objects with different
sets of instance variables. The program should also use static
variables/methods/blocks to keep track of the number of products in the store's
inventory and the store's total revenue.
LAB 4
9. Write a program in Java where the user has to input a string. Capitalize the first alphabet
of each word in the string, reverse the word, and print the complete string. The program
must:
a. Write the code without using any predefined string methods
b. Write the code using predefined string methods
10.Write a Java program to validate an email ID based on the following rules:
❖ Must contain exactly one @
❖ Must contain at least one .(dot)
❖ @ should not be the first or last character
❖ .(dot) should appear after @
a. Write the code without using any predefined string methods
b. Write the code using predefined string methods
11.Write a Program in Java where the user has to input a string. Enter a substring and
check whether it is present in the given string or not.
a. Write the code without using any predefined string methods
b. Write the code using predefined string methods
LAB 5
12. Write a java program to implement a transport system that manages different vehicle
details.
a. Create a class Vehicle with registrationNumber and fuelType.
b. Create a class Car that inherits from Vehicle and adds seatingCapacity.
c. Create a class ElectricCar that inherits from Car and adds batteryCapacity.
You can add other necessary methods, instance variables, constructors, getter, and
setter methods as per the requirements.
13. Write a java program to implement a banking application where a bank provides different
account types.
a. Create a class Account with accountNumber and balance.
b. Create a class SavingsAccount that inherits from Account.
c. Override a method to calculate interest at a higher rate.
d. Use a Tester class for demonstrating the workings of the application
You can add other necessary methods, instance variables, constructors, getter, and
setter methods as per the requirements.
Lab 6
14. An online payment system supports both Credit Card and UPI payment modes. Each
payment mode requires validation and processing of payment details.
Write a Java program to:
● Define two interfaces CardPayment and UPIPayment, each declaring the methods
validate() and processPayment().
● Create a class UnifiedPayment that implements both interfaces and provides a common
implementation for the methods.
● In the main method, create objects using interface references and demonstrate multiple
inheritance using interfaces.
15. A university examination system evaluates students using different assessment
[Link] assessment scheme must calculate the final score, but the evaluation rules differ
for each [Link] a Java program to perform the following tasks:
Create an abstract class Assessment that contains:
● A final instance variable MAX_MARKS initialized to 100
● An abstract method calculateScore(int internalMarks, int externalMarks)
● A final method displayResult(int score) that displays whether the student has passed or
failed (pass if score ≥ 40)
Create two concrete classes:
● TheoryAssessment that extends Assessment
● PracticalAssessment that extends Assessment
Each class must provide its own implementation of the calculateScore() method using different
logic.
Write a main method to:
● Create objects of both assessment types using abstract class references
● Calculate and display the final result for a student
Lab 7
16. Write a program to read the file and display all the attributes of the file. (attributes means
Details of a File)
17. Write a program to accept the directory name and write it's contents into an Array.
18. Write a program that accepts the file name using Command line Argument. Then display
[Link] Characters, words and Lines in the file.
19. Write a Code Print your own Code.
Lab 8
20. Write a java code to simulate a digital shopping cart.
● The Problem: The user might try to add "0" items or even a negative number of items to
their cart. Additionally, the store only has a limited stock (e.g., 5 laptops).
● The Task: Create logic to catch an InvalidQuantityException for negative inputs
and an OutOfStockException if the user requests more than what is available in the
database.
21. Write a Java program to perform a bank withdrawal operation using a user-defined
exception named InsufficientBalanceException.
Conditions:
● Accept account balance and withdrawal amount.
● If withdrawal amount is greater than the available balance, throw the customized
exception.
● Otherwise, deduct the amount and display the remaining balance.