Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual
DEPARTMENT
OF
COMPUTER SCIENCE & ENGINEERING
LAB MANUAL
DETAILS OF THE COURSE
COURSE
COURSE TITLE OBJECT ORIENTED PROGRAMMING LAB PCC CS 593
CODE
SEMESTER 5TH
YEAR 3RD
Course Contents
2|Page
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
To impart quality technical education amongst the individuals, emphasizing on the needs of
industry and to contribute to the society.
M1: To prepare Computer Science & Engineering students with competence in basic science &
engineering.
M2: To make the students have a good command on software and hardware development so that
they can succeed in industry or pursue higher studies or become an entrepreneur.
M3: To inculcate the concept of corporate social responsibility among the student community
3|Page
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
CREDITS 2
Pre-requisite
2. Familiarity with control structures (loops, conditionals), data types, and basic syntax.
4 Familiarity with using an integrated development environment (IDE) or a text editor for
writing code.
4|Page
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
5|Page
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
6|Page
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
PSO3 greatness and develop Graduates as computing professionals who can conduct
research and lead, design, develop, or maintain projects in various areas of Computer
science and engineering.
7|Page
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
8|Page
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Mapping of Course Outcomes with Program Outcomes and Program specific Outcomes
PCC-CS593.1 2 2 1 1 1 1 2 2 1 1 1 3 3
PCC-CS593.2 1 1 2 1 1 1 1 3 2 1 2 2 3
PCC-CS593.3 3 2 2 2 1 1 1 2 2 2 1 1 3 2
PCC-CS593.4 3 1 2 2 2 1 1 2 2 2 1 2 3 3
PCC-CS593.5 2 2 3 2 3 1 2 1 2 3 3 1 3
PCC-CS593.6 1 2 1 3 1 2 3 1 2 2 3 1 1
Average 2.00 1.67 2.00 2.00 1.67 1.00 1.20 1.50 2.00 1.83 1.75 1.50 1.80 2.50 2.50 1.00
9|Page
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
DO’S
DONT’S
10 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Assignment List
11 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
12 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
13 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Hardware Requirements:
Software Requirements
1. Java Development Kit (JDK): You’ll need JDK installed on your system. The
recommended version is Java 8 (JDK 1.8).
3. Text Editor: If you prefer not to use an IDE, you can write Java code in a
simple text editor (e.g., Notepad++) and compile it using the command line.
4. Compiler: Java code needs to be compiled into bytecode. The JDK includes the
necessary compiler (javac).
14 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
PRACTICAL ASSESSMENT
Attendance 10 Marks
15 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
ANNEXURE
16 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Assignment-01
import java.util.Scanner;
OUTPUT:
17 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Assignment – 02
import java.util.Scanner;
if (isPrime) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is not a prime number.");
}
scanner.close();
}
18 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
OUTPUT:
Enter a positive integer: 17
17 is a prime number.
19 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Assignment – 03
Write a program in Java of creation of an object and using that object to display the
values.
// Constructor
public Student(String name, int age) {
this.name = name;
this.age = age;
}
// Display method
public void displayInfo() {
System.out.println("Student Name: " + name);
System.out.println("Student Age: " + age);
}
In this program:
We define a Student class with instance variables (name and age).
The constructor initializes the object with the provided name and age.
The displayInfo() method prints the student’s name and age.
In the main method, we create an object (student1) and call the displayInfo() method to
show the values.
20 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
OUTPUT:
21 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Assignment – 04
Write a program in Java of basic method overloading using different objects and display
the values for every object accordingly.
In this program:
We have three overloaded add methods with different parameter types.
The program demonstrates how to use each method based on the argument types.
22 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
OUTPUT:
Sum of integers: 8
Sum of doubles: 6.2
Concatenated string: Hello, world!
23 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Assignment – 05
Write a program in Java of basic method overloading using different objects and display
the values for every object accordingly.
import java.util.Scanner;
class BankAccount {
private String accountHolder;
private double balance;
scanner.close();
}
}
In this program:
We define a BankAccount class with private instance variables
(accountHolder and balance).
The constructor initializes the account details.
The deposit method allows adding funds to the account.
25 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
OUTPUT:
Enter account holder's name: Alice Johnson
Enter initial balance: $500
Deposit successful. New balance: $600
Withdrawal successful. New balance: $550
Account Holder: Alice Johnson
Current Balance: $550
26 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Assignment – 06
Write a program in Java to show constructor overloading with the help of four different
object creations.
class Employee {
private int empId;
private String empName;
private double salary;
salary = 0.0;
}
In this program:
We define an Employee class with overloaded constructors.
Each constructor initializes different combinations of employee details (ID, name, and
salary).
In the main method, we create four different employee objects using different
constructors and display their information.
OUTPUT:
Employee ID: 0
Employee Name: Unknown
Salary: $0.0
Employee ID: 101
Employee Name: Guest
Salary: $0.0
Assignment – 07
Write a program in Java where constructor overloading and method overloading will
perform with the help of four different objects.
class MathOperations {
// Constructor with no parameters (default constructor)
public MathOperations() {
// No initialization needed for default constructor
}
return a + b;
}
In this program:
We define a MathOperations class with overloaded constructors and
overloaded add methods.
Each constructor initializes different combinations of parameters (integers, doubles, and
strings).
The add methods perform addition or string concatenation based on the argument types.
In the main method, we create four different MathOperations objects and demonstrate
their usage.
OUTPUT:
30 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Assignment – 08
In this program:
We define a PolymorphismDemo class with two overloaded methods: one for string
concatenation and another for float addition.
The performOperation method is overloaded based on the argument types.
In the main method, we demonstrate both operations using the same method name.
OUTPUT:
Concatenated string: Hello, world!
Sum of float numbers: 5.64
31 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Assignment – 09
Write a program in Java of polymorphism to calculate area of different shapes like Square,
Rectangle, Triangle and Circle with the help of four different object creations.
// Square class
class Square implements Shape {
private double side;
@Override
public double calculateArea() {
return side * side;
}
}
// Rectangle class
class Rectangle implements Shape {
private double length;
private double width;
this.width = width;
}
@Override
public double calculateArea() {
return length * width;
}
}
// Triangle class
32 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
@Override
public double calculateArea() {
return 0.5 * base * height;
}
}
// Circle class
class Circle implements Shape {
private double radius;
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
}
In this program:
We define an interface called Shape with a single method calculateArea().
Each shape class (Square, Rectangle, Triangle, and Circle) implements
the Shape interface and provides its own implementation of the calculateArea() method.
In the main method, we create different shape objects and calculate their respective areas.
OUTPUT:
Area of square: 25.0
Area of rectangle: 24.0
Area of triangle: 6.0
Area of circle: 19.634954084936208
34 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Assignment – 10
Write a program in Java to show constructor overloading and method overloading with the
help of Inheritance by creating three different subclasses.
// Subclass Car
class Car extends Vehicle {
String brand;
// Subclass Bike
class Bike extends Vehicle {
int wheels;
35 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
// Subclass Truck
class Truck extends Vehicle {
double loadCapacity;
In this program:
We have a base class Vehicle with a constructor and a method.
The subclasses Car, Bike, and Truck inherit from Vehicle.
Each subclass has its own constructors (overloaded) and overrides
the displayInfo() method.
OUTPUT:
Car type: Sedan
Car brand: Toyota
Bike type: Mountain Bike
Number of wheels: 2
Truck type: Heavy Duty
Load capacity (in tons): 10.5
37 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Assignment – 11
Write a program in Java to show method overloading with the help of Inheritance
by creating three different subclasses.
// Subclass Dog
class Dog extends Animal {
String breed;
System.out.println("Dog barks!");
}
}
// Subclass Cat
class Cat extends Animal {
boolean isLazy;
38 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
// Subclass Bird
class Bird extends Animal {
String color;
In this program:
We have a base class Animal with a constructor and a method.
The subclasses Dog, Cat, and Bird inherit from Animal.
Each subclass has its own constructors (overloaded) and overrides
the makeSound() method
OUTPUT:
Dog barks!
Cat meows!
Bird chirps!
40 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Assignment – 12
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
}
@Override
public double calculateArea() {
return length * width;
}
}
In this program:
We define the Shape interface with a single method calculateArea().
The Circle class implements the Shape interface and provides its own implementation
of calculateArea().
The Rectangle class also implements the Shape interface with its own implementation
of calculateArea().
OUTPUT:
42 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Assignment – 13
@Override
public void draw() {
System.out.println("Drawing a rectangle with width " + width + " and height " + height);
}
@Override
public void resize(double factor) {
width *= factor;
height *= factor;
System.out.println("Resized rectangle: new width = " + width + ", new height = " + height);
}
}
43 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
In this program:
We define two interfaces: Drawable and Resizable.
The Rectangle class implements both interfaces.
The draw() method from the Drawable interface is used to draw the rectangle.
The resize(double factor) method from the Resizable interface is used to resize the
rectangle.
OUTPUT:
Drawing a rectangle with width 5.0 and height 3.0
Resized rectangle: new width = 7.5, new height = 4.5
44 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Assignment – 14
@Override
public void draw() {
System.out.println("Drawing a rectangle with width " + width + " and height " + height);
}
@Override
public void resize(double factor) {
width *= factor;
height *= factor;
System.out.println("Resized rectangle: new width = " + width + ", new height = " + height);
}
}
In this program:
We define two interfaces: Drawable and Resizable.
The Rectangle class implements both interfaces.
The draw() method from the Drawable interface is used to draw the rectangle.
The resize(double factor) method from the Resizable interface is used to resize the
rectangle.
OUTPUT:
46 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Assignment-15
47 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
In this program:
Animal is the base class with an eat() method.
Mammal inherits from Animal and adds a run() method.
Dog inherits from Mammal and adds a bark() method.
OUTPUT:
Animal is eating.
Mammal is running.
Dog is barking.
48 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Assignment – 16
Write a program in Java of basic two number additions through user input using
command line arguments using wrapper class.
try {
// Parse command line arguments as doubles
double num1 = Double.parseDouble(args[0]);
double num2 = Double.parseDouble(args[1]);
// Perform addition
double sum = num1 + num2;
OUTPUT:
49 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Assignment – 17
Write a program in Java that demonstrates how command line arguments are
received as strings in Java.
Replace arg1, arg2, and arg3 with any values you’d like. The program will display the
provided command line arguments.
OUTPUT:
50 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Assignment – 18
Write a program in Java to test system define exceptions through different category
user inputs.
import java.util.Scanner;
try {
// Example 1: Handling NumberFormatException
System.out.print("Enter an integer: ");
String userInput = scanner.nextLine();
int number = Integer.parseInt(userInput);
System.out.println("You entered: " + number);
51 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
In this program:
52 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Assignment – 19
import java.util.Scanner;
try {
System.out.print("Enter a dividend: ");
int dividend = scanner.nextInt();
if (divisor == 0) {
throw new DivisionByZeroException("Division by zero is not allowed.");
}
53 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
In this program:
OUTPUT:
Enter a dividend: 10
Enter a divisor: 2
Result of division: 5
54 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Assignment – 20
Write a program in Java for several types of system defined exceptions.
try {
// Example 2: ArrayIndexOutOfBoundsException
int[] arr = new int[5];
int value = arr[10];
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBoundsException caught: " + e.getMessage());
}
try {
// Example 3: NullPointerException
String str = null;
int length = str.length();
} catch (NullPointerException e) {
System.out.println("NullPointerException caught: " + e.getMessage());
}
55 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
try {
// Example 4: NumberFormatException
String invalidNumber = "abc";
int parsedNumber = Integer.parseInt(invalidNumber);
} catch (NumberFormatException e) {
System.out.println("NumberFormatException caught: " + e.getMessage());
}
}
}
In this program:
We intentionally cause exceptions to occur and handle them using try-catch blocks.
The output shows the exception messages associated with each type of exception.
OUTPUT:
ArithmeticException caught: / by zero
ArrayIndexOutOfBoundsException caught: Index 10 out of bounds for length 5
NullPointerException caught: null
NumberFormatException caught: For input string: "abc"
56 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Assignment – 21
Write a program in Java for different types of user defined exceptions.
class BankAccount {
private double balance;
}
balance -= amount;
}
}
Explanation:
We’ve defined two custom exception
classes: InvalidAgeException and InsufficientBalanceException.
The BankAccount class has a withdraw method that throws
an InsufficientBalanceException if the withdrawal amount exceeds the account balance.
In the main method, we create a bank account with an initial balance of $1000 and
attempt to withdraw $1500.
Since the balance is insufficient, an InsufficientBalanceException is thrown and caught.
58 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
OUTPUT:
Error: Insufficient balance to withdraw $1500.0
Explanation:
In the main method, we create a BankAccount object with an initial balance of $1000.
We attempt to withdraw an amount of $1500 using the withdraw method.
Since the account balance is insufficient for this withdrawal,
an InsufficientBalanceException is thrown.
The catch block catches the exception and prints the error message: “Insufficient balance
to withdraw $1500.0”.
59 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Assignment – 22
Write a program in Java to show exceptions of user’s choice depending on user
input logic.
import java.util.Scanner;
try {
switch (choice) {
case 1:
// Example 1: NullPointerException
String str = null;
int length = str.length();
break;
case 2:
// Example 2: ArrayIndexOutOfBoundsException
int[] arr = new int[5];
60 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
scanner.close();
}
}
61 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
OUTPUT:
Choose an exception type:
1. NullPointerException
2. ArrayIndexOutOfBoundsException
3. CustomException (InvalidInputException)
2
ArrayIndexOutOfBoundsException caught: Index 10 out of bounds for length 5
Explanation:
The user selects option 2, which corresponds to the ArrayIndexOutOfBoundsException.
The program attempts to access an array element that is out of bounds (index 10 in an
array of length 5).
The exception is caught, and the error message is displayed.
62 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Assignment – 23
Create an abstract class to calculate employee details, leveraging inheritance
properties. Then, create two objects for different child classes.
@Override
public double calculateSalary() {
return monthlySalary;
}
}
@Override
public double calculateSalary() {
// Assume a fixed monthly payment for contract employees
return 2000 * contractDuration;
}
}
64 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
65 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Assignment – 24
Write a program in Java for Banking transactions where deposit, withdrawal,
display and search of an account have to show according to the choice of the user.
import java.util.ArrayList;
import java.util.Scanner;
class BankAccount {
private int accountNumber;
private String accountHolder;
private double balance;
while (true) {
System.out.println("\nBanking Transactions Menu:");
System.out.println("1. Deposit");
System.out.println("2. Withdraw");
System.out.println("3. Display Account Details");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
67 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
OUTPUT:
Banking Transactions Menu:
1. Deposit
2. Withdraw
3. Display Account Details
4. Exit
Enter your choice: 1
Enter account number: 1001
Enter deposit amount: $1000
Deposit successful. New balance: $6000.0
69 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Assignment – 25
Write a program in Java that demonstrates thread functionality for getting the
thread name and priority. Use try-catch logic in your implementation.
OUTPUT:
Thread name: MyCustomThread
Thread priority: 10
Custom thread is running.
Explanation:
We create a custom thread using a lambda expression that simulates some work (sleeps
for 2 seconds).
We set the thread name to “MyCustomThread” and the priority to the maximum value
(10).
The thread starts executing, and we display its name and priority.
The custom thread completes its work and prints “Custom thread is running.”
72 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Assignment – 26
Write a program in Java of your own thread which will follow the thread life cycle.
OUTPUT:
Thread state before starting: NEW
Custom thread is running (State: NEW)
Thread state after starting: RUNNABLE
Custom thread completed its work (State: TERMINATED)
Thread state after completion: TERMINATED
Explanation:
We create a custom thread using a lambda expression.
Initially, the thread is in the NEW state.
After starting the thread, it transitions to the RUNNABLE state.
The custom thread simulates some work (sleeps for 2 seconds) and then completes its
task.
Finally, the thread enters the TERMINATED state.
74 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Assignment – 27
Write a program in Java that extends thread using package which contains thread
class.
OUTPUT:
Thread 11: 1
Thread 12: 1
Thread 11: 2
Thread 12: 2
Thread 11: 3
Thread 12: 3
Thread 11: 4
Thread 12: 4
Thread 11: 5
Thread 12: 5
Explanation:
We create a custom thread class MyCustomThread by extending the Thread class.
The run method is overridden to define the behavior of the custom thread.
In the main method, we create two instances of MyCustomThread.
Both threads start executing concurrently and print numbers from 1 to 5.
76 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Assignment - 28
Write a program in Java to control main thread by changing its name.
77 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
OUTPUT:
Main thread name: MyMain
Working in MyMain: Step 1
Working in MyMain: Step 2
Working in MyMain: Step 3
Working in MyMain: Step 4
Working in MyMain: Step 5
Explanation:
We retrieve the reference to the main thread using Thread.currentThread().
We set a custom name for the main thread using setName("MyMain").
The program then simulates some work (printing steps) in the main thread.
Assignment – 29
Write a program in Java to create threads using two processes which will run
simultaneously using sleep timer.
79 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
OUTPUT:
Thread 1: 1
Thread 2: 1
Thread 2: 2
Thread 1: 2
Thread 2: 3
Thread 1: 3
Thread 2: 4
Thread 1: 4
Thread 2: 5
Thread 1: 5
Explanation:
We create two threads (thread1 and thread2) using lambda expressions.
Each thread prints numbers from 1 to 5 with a specified delay using Thread.sleep.
The threads run simultaneously, and their output may be interleaved.
80 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Assignment – 30
Write a program in Java to create threads by setting child thread and using all
thread with time interval.
OUTPUT:
The output will be a mix of numbers and letters printed by the three threads. The order of
execution may vary due to thread scheduling.
82 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Assignment – 31
Write a program in Java to create threads by making child thread and use all
threads by their name and their priorities.
84 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
OUTPUT:
Auxiliary Thread: A
Main Thread: 1
Child Thread: 1
Child Thread: 2
Auxiliary Thread: B
Main Thread: 2
Child Thread: 3
Auxiliary Thread: C
Child Thread: 4
Main Thread: 3
Child Thread: 5
Auxiliary Thread: D
Auxiliary Thread: E
In this program:
The child thread is named “ChildThread” and has the minimum priority.
The auxiliary thread is named “AuxiliaryThread” and has the normal priority.
The main thread continues execution alongside the child and auxiliary threads.
85 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
Assignment – 32
Create a basic Java applet that demonstrates interactivity and dynamic behavior
within a web page.
import java.applet.Applet;
import java.awt.Graphics;
86 | P a g e
BUDGE BUDGE INSTITUTE OF TECHNOLOGY
Affiliated to MAKAUT (formerly WBUT) & Approved by AICTE, NAAC Accredited
Kolkata - 700 137, West Bengal, India
Phone : 033 2482 0676 / 0670
http://www.bbit.edu.in
<html>
<head>
<title>Hello Applet</title>
</head>
<body>
</body>
</html>
o You should see the message “Hello, Applet World!” displayed on the screen.
Remember that the class name must be public because its object is created by the Java Plugin
software that resides in the browser. The HTML file (myapplet.html) specifies the applet’s
dimensions and references the compiled class file.
You can also run the applet using the appletviewer tool:
appletviewer HelloWorld.java
87 | P a g e