OOPS Lab Programs
OOPS Lab Programs
3. Write a JAVA program to search for an element in a given list of elements using
binarysearch mechanism.
4. Create a class Fact with attribute n and a method factorial. Develop a JAVA code to
read n from user and find the factorial of a given number.
5. Write a program to display the employee details using Scanner class.
6. Develop a java application to implement currency converter (Dollar to INR, EURO
toINR, Yen to INR and vice versa), distance converter (meter to KM, miles to KM
and viceversa) , time converter (hours to minutes, seconds and vice versa) using
switch case.
7. Implement a Java Program that reads a line of integers, and then displays each integer,
and the sum of all the integers (use StringTokenizer class).
8. Implement a java program to print all tokens of a string on the bases of multiple
separators (use StringTokenizer class).
9. Develop a java application with Employee class with Emp_name, Emp_id,
Address,Mail_id, Mobile_no as members. Inherit the classes, Programmer, Assistant
Professor,Associate Professor and Professor from employee class. Add Basic Pay
(BP) as the memberof all the inherited classes with 97% of BP as DA, 10 % of BP as
HRA, 12% of BP as PF,0.1% of BP for staff club fund. Generate pay slips for the
employees with their gross and netsalary.
Unit – 2
Inheritance, Interface & Packages
1. Write a Java program to create a vehicle class hierarchy. The base class should be
Vehicle, with subclasses Truck, Car and Motorcycle. Each subclass should have
properties such as make, model, year, and fuel type. Implement methods for
calculating fuel efficiency, distance traveled, and maximum speed. Use the super
keyword to initialize the superclass attributes and invoke superclass methods where
appropriate. Apply the principles of inheritance and method overriding.
Constraints:
Fuel Efficiency:
Distance Traveled:
Maximum Speed:
Truck: 120 mph
Car: 150 mph
Motorcycle: 180 mph
2. Develop a JAVA program to create an abstract class Shape with abstract methods
calculateArea() and calculatePerimeter(). Create subclasses Circle and Triangle that
extend the Shape class and implement the respective methods to calculate the area and
perimeter of each shape.
Constraints:
Bank Class:
Should maintain a list of accounts.
Methods for adding accounts and viewing all accounts.
Account Interface:
SavingsAccount:
CurrentAccount:
5. You are tasked with designing a Java application to manage a library system. The
system involves different types of library items such as Books and DVDs, each with
specific attributes and behaviors. Implement inheritance and interface implementation
using the extends and implements keywords to model these entities.
Requirements
Book Class:
DVD Class:
Sample Input
6
Sample Output
I implemented: AdvancedArithmetic
12
7. Implement a Java program for the following
a) Creation of simple package.
b) Accessing a package.
Exception Handling
1. a). Implement a Java program to create a method that takes an integer as a parameter and
throws an exception if the number is odd.
b). Implement a Java program that reads a list of integers from the user and throws an
exception if any numbers are duplicates.
2. a.) Implement a Java program to create a method that takes a string as input and throws an
exception if the string does not contain vowels.
b). Create a class which accepts a number as choice and returns the Month of the year. If
the entered number is greater than 12 or less than 1 throw InvalidChoiceException. If the
entered option is not a number throw NotANumberException.
3. Create an Animal class which have four methods (eat, sleep, swim, walk). Extend a class
Fish that overrides walk method. Extend class Dolphin from Fish that overrides the walk
method. If a walk method is called form Dolphin class, generate an exception (as fish can’t
walk).
4. Implement a java program in which you have two Rides (Car, Boat) and two locations
(Land, Water). Ask from the user about the ride and the location. If user selects ride a car
and location water, then throw an exception. Similarly, if user selects ride a boat and
location water, then also throw an exception.
5. There are N number of bottles and M number of glasses that will be filled from these
bottles. Each bottle can fill up 5 glasses. You have to Implement JAVA code that will ask
user to input N and M, and check if the bottles are enough to fill M glasses. If bottles are
not enough throw an exception informing the user. (Total number of glasses that can be
made from bottles = 5*number of bottles).
6. Create a three-level hierarchy of exceptions. Now create a base-class A with a method that
throws an exception at the base of your hierarchy. Inherit B from A and override the
method so it throws an exception at level two of your hierarchy. Repeat by inheriting class
C from B. In main (), create a C and up cast it to A, then call the method.
7. Implement a java program using multiple catch blocks. Create a class CatchExercise
inside the try block declare an array a[] and initialize with value a[5] =30/5; . In each catch
block show Arithmetic exception and ArrayIndexOutOfBoundsException.
8. Implement a java program that count how many prime numbers between minimum and
maximum values provided by user. If minimum value is greater than or equal to maximum
value, the program should throw a InvalidRange exception and handle it to display a
message to the user on the following format: Invalid range: minimum is greater than or
equal to maximum. (For example, if the user provided 10 as maximum and 20 as
minimum, the message should be: Invalid range: 20 is greater than or equal to 10).
Module – 2
Multi-Threading
1. Develop a multi-function utility application in Java that consists of the following threads:
Compare and discuss the two approaches, highlighting the differences in implementation
and use-case scenarios.
5. Implement a Java application that uses multiple threads to increment a shared counter:
Implement the application without any synchronization to showcase the race condition and
potential incorrect final value of the counter.
Run the threads and observe their execution order and completion time.
Implement a Producer thread that generates data and puts it in the SharedResource.
Implement a Consumer thread that retrieves and processes the data from the
SharedResource.
8. Develop a Java application that simulates a simple data processing pipeline with:
DataBuffer Class: A buffer to hold a single piece of data shared between the producer and
consumer.
DataProducer Thread: A thread that reads data (simulates reading data from a file or
sensor) and places it into the shared buffer.
DataProcessor Thread: A thread that processes the data from the shared buffer (simulates
data processing such as transformation or analysis).
Ensure the program handles edge cases like empty lists and non-existent items gracefully.
3. Create a ticket reservation system for a concert using Java's Vector. Each ticket request
must store: Fan Name, Number of Tickets
Unit – 2
Java Swings
1. Create a Java program that handles various mouse events, including when the mouse
enters, exits, clicks, presses, releases, drags, and moves within the client area. Demonstrate
how each event can be used to perform specific actions, such as changing the background
color, displaying coordinates, or drawing shapes, to enhance user interaction in a simple
graphical application.
2. Implement a java program using swings to design a multiple choice question having three
options (use radio button), display the message using dialog box “Your answer is wrong”
if the user selects wrong option otherwise display, “Your answer is correct."
3. Develop a Java program that handles various key events, including when a key is pressed,
released, and typed within a client area. Demonstrate how each event can be used to
perform specific actions, such as updating a display with the pressed key to enhance user
interaction.
4. Design a Java application that uses Swing components to create a simple calculator with
buttons for digits (0-9), arithmetic operations (+, -, *, /), and an equals (=) button. Utilize
event listeners to handle button clicks and display the result of arithmetic operations in a
text field.
5. Develop a Java application that includes two JButton components labeled "Enable" and
"Disable". Implement functionality so that clicking the "Enable" button enables a third
JButton labeled "Action" and clicking the "Disable" button disables the "Action" button.
6. Implement a Java application for selecting a gender using JRadioButton (Male and
Female). Implement functionality so that selecting a radio button updates a JLabel to
display the selected gender.
7. Create a Java program that displays a list of cities (Delhi, Hyderabad, Kolkata. Mumbai)
using ‘JList’. Implement functionality so that selecting a city from the list updates a
‘JLabel’ with additional information about the city, such as population and country.
8. Develop a Java program that displays a ‘JComboBox’ with a list of colors (e.g., Red,
Green, Blue). Implement functionality to print the selected color to the console when a
user selects an item from the dropdown.