III
Object Oriented Programming Using Java
Roll No:
CSE (III Sem)
CERTIFICATE
Certified that this is a Bonafide record of the work done
in the Object Oriented Programming Using
Java
2024-2025
during the academic year 2025-2026
Name:
Roll No:
Branch:
Artificial
Computer
Intelligence
Science and
Engineering
Machine Lg
Date: Signature of the Faculty
External Examiner
1. A program to illustrate the concept of class with constructors, methods and overloading.
(this keyoword should also be inccluded)
A. class Demo {
int x, y;
Demo() {
this.x = 0;
this.y = 0;
}
Demo(int a) {
this.x = a;
this.y = a;
}
Demo(int a, int b) {
this.x = a;
this.y = b;
}
void show() {
[Link](this.x + " " + this.y);
}
void set(int a) {
this.x = a;
this.y = a;
}
void set(int a, int b) {
this.x = a;
this.y = b;
}
public static void main(String[] args) {
Demo d1 = new Demo();
Demo d2 = new Demo(5);
Demo d3 = new Demo(3, 7);
[Link]();
[Link]();
[Link]();
[Link](10);
[Link]();
[Link](2, 9);
[Link]();
}
}
Output:
00
55
37
10 10
29
2.A program to illustrate the concept of Inheritance (all types of inheritance) and Dynamic
polymorphism. (super should also be included).
A.
class A {
int x = 10;
void fun() {
[Link]("A fun");
}
}
class B extends A { // single
int x = 20;
void fun() { // overriding
[Link]("B fun, super x = " + super.x);
}
}
class C extends B { // multilevel
void fun() {
[Link]("C fun");
}
}
class D extends A { // hierarchical
void fun() {
[Link]("D fun");
}
}
public class Main {
public static void main(String[] args) {
A obj;
obj = new B(); // dynamic polymorphism
[Link]();
obj = new C();
[Link]();
obj = new D();
[Link]();
}
}
Output:
B fun, super x = 10
C fun
D fun
3. A program to show the concept of packages. (using all access modifiers)
A.
pack/[Link]:
package pack1;
public class One {
public int a = 10;
protected int b = 20;
int c = 30; // default
private int d = 40;
public void show() {
[Link](a + " " + b + " " + c + " " + d);
}
}
Pack2/[Link]:
package pack2;
import [Link];
public class Two extends One {
public void disp() {
[Link](a); // public
[Link](b); // protected
}
}
Pack2/[Link]
package pack2;
import [Link];
public class Main {
public static void main(String[] args) {
One o = new One();
[Link]();
Two t = new Two();
[Link]();
}
}
Output:
10 20 30 40
10
20
4. A program to illustrate the usage of interfaces (class implementing and interface extends)
and Abstract class.
A.
interface A {
void funA();
}
interface B extends A { // interface extends
void funB();
}
abstract class C { // abstract class
abstract void funC();
void hi() {
[Link]("hi from C");
}
}
class D extends C implements B { // class implements + abstract class
public void funA() {
[Link]("funA");
}
public void funB() {
[Link]("funB");
}
void funC() {
[Link]("funC");
}
}
public class Main {
public static void main(String[] args) {
D obj = new D();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
Output:
funA
funB
funC
hi from C
7. Demonstrate inner class and anonymous inner classes.
A. class Outer {
int x = 10;
class Inner { // normal inner class
void show() {
[Link]("inner x = " + x);
}
}
void test() {
Runnable r = new Runnable() { // anonymous inner class
public void run() {
[Link]("hello from anon class");
}
};
[Link]();
}
}
public class Main {
public static void main(String[] args) {
Outer o = new Outer();
[Link] in = [Link] Inner();
[Link]();
[Link]();
}
}
Output:
inner x = 10
hello from anon class
5. A program to illustrate exception handling keywords.
A. class Main {
public static void main(String[] args) {
try {
int a = 10, b = 0;
int c = a / b; // exception
[Link](c);
}
catch (ArithmeticException e) {
[Link]("cant divide");
}
catch (Exception e) {
[Link]("some error");
}
finally {
[Link]("finally run");
}
try {
int arr[] = {1,2,3};
[Link](arr[5]);
}
catch (ArrayIndexOutOfBoundsException e) {
[Link]("array wrong");
}
}
}
Output:
cant divide
finally run
array wrong
6. A program to illustrate user define exception for evaluating postfix expression using stack.
A. // user defined exception + postfix using stack
class MyExp extends Exception {
MyExp(String s) {
super(s);
}
}
class Stk {
int top = -1;
int arr[] = new int[20];
void push(int x) {
arr[++top] = x;
}
int pop() throws MyExp {
if (top < 0)
throw new MyExp("stack empty");
return arr[top--];
}
}
public class Main {
public static int evalPost(String p) throws MyExp {
Stk s = new Stk();
for (int i = 0; i < [Link](); i++) {
char ch = [Link](i);
if ([Link](ch)) {
[Link](ch - '0');
} else {
int b = [Link]();
int a = [Link]();
int r = 0;
if (ch == '+') r = a + b;
else if (ch == '-') r = a - b;
else if (ch == '*') r = a * b;
else if (ch == '/') r = a / b;
[Link](r);
}
}
return [Link]();
}
public static void main(String[] args) {
try {
[Link](evalPost("23+5*"));
}
catch (MyExp e) {
[Link]([Link]());
}
}
}
Output: 25
8. A program to illustrate to handle string in java using String and String Buffer. (different
methods )
A. class Main {
public static void main(String[] args) {
String s = "hello";
[Link]([Link]());
[Link]([Link]());
[Link]([Link](1));
[Link]([Link](" world"));
[Link]([Link]('l','x'));
StringBuffer sb = new StringBuffer("abc");
[Link]("123");
[Link](sb);
[Link](1, "Q");
[Link](sb);
[Link](2, 4);
[Link](sb);
[Link]();
[Link](sb);
}
}
Output:
5
HELLO
e
hello world
hexxo
abc123
aQbc123
aQ23
32Qa
9. A program to illustrate manipulating array in java
A. class Main {
public static void main(String[] args) {
int a[] = {5, 2, 9, 1, 7};
[Link]("len = " + [Link]);
for (int i = 0; i < [Link]; i++)
[Link](a[i] + " ");
[Link]();
// find max
int max = a[0];
for (int i = 1; i < [Link]; i++)
if (a[i] > max) max = a[i];
[Link]("max = " + max);
for (int i = 0, j = [Link] - 1; i < j; i++, j--) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
for (int i = 0; i < [Link]; i++)
[Link](a[i] + " ");
}
}
Output:
len = 5
52917
max = 9
71925
10. A program to illustrate Multithreading. (two approaches)
A. // approach 1: extending Thread
class A extends Thread {
public void run() {
for (int i = 0; i < 5; i++)
[Link]("A " + i);
}
}
class B implements Runnable {
public void run() {
for (int i = 0; i < 5; i++)
[Link]("B " + i);
}
}
public class Main {
public static void main(String[] args) {
A t1 = new A();
[Link]();
Thread t2 = new Thread(new B());
[Link]();
}
}
Output:
A0
A1
A2
A3
A4
B0
B1
B2
B3
B4
11. A program to illustrate Thread synchronization. (block level and method level)
A. class Table {
void printTab(int n) { // method-level sync
synchronized(this) {
for (int i = 1; i <= 5; i++)
[Link](n * i);
}
}
void blockTab(int n) {
for (int i = 1; i <= 5; i++) {
synchronized(this) {
[Link](n + i);
}
}
}
}
class A extends Thread {
Table t;
A(Table t) { this.t = t; }
public void run() { [Link](2); }
}
class B extends Thread {
Table t;
B(Table t) { this.t = t; }
public void run() { [Link](3); }
}
public class Main {
public static void main(String[] args) {
Table t = new Table();
new A(t).start();
new B(t).start();
}
}
Output:
2
4
6
8
10
4
5
6
7
8
12.A program to illustrate inter thread communication (wait, notify() and notifyall()) with
different use cases 1 producer and consumer, 1 producer and 2 consumers, 2 producers and 1
consumer
A. approach a:
class Box {
int data;
boolean full = false;
synchronized void put(int x) {
try {
while(full) wait();
data = x;
full = true;
[Link]("put : " + x);
notify();
} catch(Exception e){}
}
synchronized int get() {
int v = 0;
try {
while(!full) wait();
v = data;
full = false;
[Link]("get : " + v);
notify();
} catch(Exception e){}
return v;
}
}
class P1 extends Thread {
Box b;
P1(Box b){ this.b = b; }
public void run() {
for(int i=1;i<=5;i++) [Link](i);
}
}
class C1 extends Thread {
Box b;
C1(Box b){ this.b = b; }
public void run() {
for(int i=1;i<=5;i++) [Link]();
}
}
public class Main1 {
public static void main(String[] a) {
Box b = new Box();
new P1(b).start();
new C1(b).start();
}
}
Output:
put : 1
get : 1
put : 2
get : 2
put : 3
get : 3
put : 4
get : 4
put : 5
get : 5
Approach b:
class Box2 {
int data;
boolean full = false;
synchronized void put(int x) {
try {
while(full) wait();
data = x;
full = true;
[Link]("put : " + x);
notifyAll();
} catch(Exception e){}
}
synchronized int get() {
int v = 0;
try {
while(!full) wait();
v = data;
full = false;
[Link]([Link]().getName()+" got "+v);
notifyAll();
} catch(Exception e){}
return v;
}
}
class P2 extends Thread {
Box2 b;
P2(Box2 b){ this.b = b; }
public void run() {
for(int i=1;i<=5;i++) [Link](i);
}
}
class C2 extends Thread {
Box2 b;
C2(Box2 b){ this.b = b; }
public void run() {
for(int i=1;i<=3;i++) [Link]();
}
}
public class Main2 {
public static void main(String[] a) {
Box2 b = new Box2();
new P2(b).start();
new C2(b,"C1").start();
new C2(b,"C2").start();
}
}
Output:
put : 1
C1 got 1
put : 2
C2 got 2
put : 3
C1 got 3
put : 4
C2 got 4
put : 5
C1 got 5
Approach C:
class Box3 {
int data;
boolean full = false;
synchronized void put(int x) {
try {
while(full) wait();
data = x;
full = true;
[Link]([Link]().getName()+" put "+x);
notifyAll();
} catch(Exception e){}
}
synchronized int get() {
int v = 0;
try {
while(!full) wait();
v = data;
full = false;
[Link]("consumer got " + v);
notifyAll();
} catch(Exception e){}
return v;
}
}
class P3 extends Thread {
Box3 b;
P3(Box3 b){ this.b = b; }
public void run() {
for(int i=1;i<=3;i++) [Link](i);
}
}
class C3 extends Thread {
Box3 b;
C3(Box3 b){ this.b = b; }
public void run() {
for(int i=1;i<=6;i++) [Link]();
}
}
public class Main3 {
public static void main(String[] a) {
Box3 b = new Box3();
new P3(b,"P1").start();
new P3(b,"P2").start();
new C3(b).start();
}
}
Output:
put : 1
C1 got 1
put : 2
C2 got 2
put : 3
C1 got 3
put : 4
C2 got 4
put : 5
C1 got 5
13.A program using String tokenizer.
A. import [Link];
class Main {
public static void main(String[] args) {
String s = "hello how are you";
StringTokenizer st = new StringTokenizer(s, " ");
while([Link]()) {
[Link]([Link]());
}
String s2 = "10,20,30,40";
StringTokenizer st2 = new StringTokenizer(s2, ",");
int sum = 0;
while([Link]()) {
sum += [Link]([Link]());
}
[Link]("sum = " + sum);
}
}
Output:
hello
how
are
you
sum = 100
14. A program using Linked list and list iterator classes
A. import [Link].*;
class Main {
public static void main(String[] args) {
LinkedList<String> lst = new LinkedList<>();
[Link]("a");
[Link]("b");
[Link]("c");
[Link]("list : " + lst);
ListIterator<String> it = [Link]();
[Link]("forward:");
while([Link]()) {
[Link]([Link]());
}
[Link]("backward:");
while([Link]()) {
[Link]([Link]());
}
it = [Link](1);
[Link]("x"); // add using iterator
[Link]("after add: " + lst);
}
}
Output:
list : [a, b, c]
forward:
a
b
c
backward:
c
b
a
after add: [a, x, b, c]
15. A program using Tree set class.
A. import [Link].*;
class Main {
public static void main(String[] args) {
TreeSet<Integer> t = new TreeSet<>();
[Link](5);
[Link](1);
[Link](9);
[Link](3);
[Link](1); // duplicate ignored
[Link]("set = " + t);
[Link]("first = " + [Link]());
[Link]("last = " + [Link]());
[Link]("higher(3) = " + [Link](3));
[Link]("lower(5) = " + [Link](5));
for(int x : t)
[Link](x);
}
}
Output:
set = [1, 3, 5, 9]
first = 1
last = 9
higher(3) = 5
lower(5) = 3
1
3
5
9
16. A program using Hash set and Iterator classes.
A. import [Link].*;
class Main {
public static void main(String[] args) {
HashSet<String> hs = new HashSet<>();
[Link]("apple");
[Link]("ball");
[Link]("cat");
[Link]("apple"); // duplicate ignored
[Link]("set = " + hs);
Iterator<String> it = [Link]();
while([Link]()) {
[Link]([Link]());
}
}
}
Output:
set = [cat, ball, apple]
cat
ball
apple
17. A program using Map classes.
A. import [Link].*;
class Main {
public static void main(String[] args) {
HashMap<Integer,String> m = new HashMap<>();
[Link](1, "a");
[Link](2, "b");
[Link](3, "c");
[Link]("map = " + m);
[Link]("get 2 = " + [Link](2));
for(Integer k : [Link]())
[Link]("key : " + k);
for(String v : [Link]())
[Link]("val : " + v);
for([Link]<Integer,String> e : [Link]())
[Link]([Link]() + " -> " + [Link]());
TreeMap<Integer,String> tm = new TreeMap<>();
[Link](3,"x");
[Link](1,"y");
[Link](2,"z");
[Link]("treemap = " + tm);
}
}
Output:
map = {1=a, 2=b, 3=c}
get 2 = b
key : 1
key : 2
key : 3
val : a
val : b
val : c
1 -> a
2 -> b
3 -> c
treemap = {1=y, 2=z, 3=x}
18. A program using Enumeration and Comparator interfaces.
A. import [Link].*;
class MyComp implements Comparator<Integer> {
public int compare(Integer a, Integer b) {
return a - b; // ascending
}
}
class Main {
public static void main(String[] args) {
Vector<String> v = new Vector<>();
[Link]("dog");
[Link]("cat");
[Link]("bat");
Enumeration<String> en = [Link]();
while([Link]()) {
[Link]([Link]());
}
ArrayList<Integer> ar = new ArrayList<>();
[Link](9);
[Link](2);
[Link](5);
[Link](ar, new MyComp());
[Link]("sorted : " + ar);
}
}
Output:
dog
cat
bat
sorted : [2, 5, 9]
19. A program to illustrate Buffered I/O streams and Buffered reader. (with no buffer and
different buffer sizes)
A. import [Link].*;
class Main {
public static void main(String[] args) {
try {
// -------- NO BUFFER --------
FileInputStream f1 = new FileInputStream("[Link]");
FileOutputStream f2 = new FileOutputStream("[Link]");
int ch;
while((ch = [Link]()) != -1) {
[Link](ch);
}
[Link]();
[Link]();
// -------- BUFFERED STREAM (default buffer) --------
BufferedInputStream b1 = new BufferedInputStream(new FileInputStream("[Link]"));
BufferedOutputStream b2 = new BufferedOutputStream(new
FileOutputStream("[Link]"));
while((ch = [Link]()) != -1) {
[Link](ch);
}
[Link]();
[Link]();
// -------- BUFFERED STREAM (custom buffer size) --------
BufferedInputStream b3 = new BufferedInputStream(new FileInputStream("[Link]"),
4096);
BufferedOutputStream b4 = new BufferedOutputStream(new
FileOutputStream("[Link]"), 4096);
while((ch = [Link]()) != -1) {
[Link](ch);
}
[Link]();
[Link]();
// -------- BufferedReader --------
BufferedReader br = new BufferedReader(new FileReader("[Link]"));
String line;
while((line = [Link]()) != null) {
[Link](line);
}
[Link]();
} catch(Exception e) {
[Link](e);
}
}
}
Output:
hello
world
20. Write a Java program to read text from file from a specify index or skipping byte using
file Input
A. import [Link].*;
class Main {
public static void main(String[] args) {
try {
FileInputStream f = new FileInputStream("[Link]");
[Link](5); // skip first 5 bytes
int x;
while((x = [Link]()) != -1) {
[Link]((char)x);
}
[Link]();
}
catch(Exception e) {
[Link](e);
}
}
}
Output:
World
21. Write a Java program to determine number of byte return to file using data output
stream.
A. import [Link].*;
class Main {
public static void main(String[] args) {
try {
FileOutputStream f = new FileOutputStream("[Link]");
DataOutputStream d = new DataOutputStream(f);
[Link](100);
[Link](55.5);
[Link]('A');
[Link]("hello");
int n = [Link](); // number of bytes written
[Link]("bytes written = " + n);
[Link]();
[Link]();
}
catch(Exception e) {
[Link](e);
}
}
}
Output:
bytes written = 17
22. A program to illustrate Byte Array I/O Streams.
A. import [Link].*;
class Main {
public static void main(String[] args) {
try {
byte arr[] = "hello world".getBytes();
// write to byte array output stream
ByteArrayOutputStream out = new ByteArrayOutputStream();
[Link](arr);
byte b[] = [Link]();
[Link]("written bytes: " + new String(b));
// read using byte array input stream
ByteArrayInputStream in = new ByteArrayInputStream(b);
int x;
while((x = [Link]()) != -1) {
[Link]((char)x);
}
[Link]();
[Link]();
}
catch(Exception e) {
[Link](e);
}
}
}
Output:
written bytes: hello world
hello world
23. A program to illustrate the usage of Serialization.
A. import [Link].*;
// simple class for serialization
class Stu implements Serializable {
int id;
String name;
Stu(int i, String n) {
id = i;
name = n;
}
}
class Main {
public static void main(String[] args) {
try {
Stu s1 = new Stu(1, "ram");
// write object
ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("[Link]"));
[Link](s1);
[Link]();
// read object
ObjectInputStream i = new ObjectInputStream(new FileInputStream("[Link]"));
Stu s2 = (Stu)[Link]();
[Link]();
[Link]([Link] + " " + [Link]);
} catch(Exception e) {
[Link](e);
}
}
}
Output:
1 ram
24. An application involving GUI with different controls, menus and event handling.
A. import [Link].*;
import [Link].*;
import [Link].*;
class MyApp extends JFrame implements ActionListener {
JTextField t;
JButton b1, b2;
JTextArea a;
JMenuItem mi1, mi2;
MyApp() {
setTitle("My GUI");
setSize(400,300);
setLayout(new FlowLayout());
t = new JTextField(15);
b1 = new JButton("show");
b2 = new JButton("clear");
a = new JTextArea(5,20);
[Link](this);
[Link](this);
add(t);
add(b1);
add(b2);
add(new JScrollPane(a));
JMenuBar mb = new JMenuBar();
JMenu m = new JMenu("Menu");
mi1 = new JMenuItem("Upper");
mi2 = new JMenuItem("Exit");
[Link](this);
[Link](this);
[Link](mi1);
[Link](mi2);
[Link](m);
setJMenuBar(mb);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if([Link]()==b1) {
[Link]([Link]() + "\n");
}
if([Link]()==b2) {
[Link]("");
}
if([Link]()==mi1) {
[Link]([Link]().toUpperCase());
}
if([Link]()==mi2) {
[Link](0);
}
}
public static void main(String[] args) {
new MyApp();
}
}
25. A program to implement asimple calculator using grid layout manager.
A. import [Link].*;
import [Link].*;
import [Link].*;
class Calc extends JFrame implements ActionListener {
JTextField t;
JButton nums[] = new JButton[10];
JButton add, sub, mul, div, eq, clr;
int a = 0, b = 0;
char op;
Calc() {
setTitle("calc");
setSize(300,400);
setLayout(new BorderLayout());
t = new JTextField();
add(t, [Link]);
JPanel p = new JPanel();
[Link](new GridLayout(4,4));
for(int i=0;i<10;i++) {
nums[i] = new JButton(""+i);
nums[i].addActionListener(this);
}
add = new JButton("+");
sub = new JButton("-");
mul = new JButton("*");
div = new JButton("/");
eq = new JButton("=");
clr = new JButton("C");
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](nums[7]); [Link](nums[8]); [Link](nums[9]); [Link](add);
[Link](nums[4]); [Link](nums[5]); [Link](nums[6]); [Link](sub);
[Link](nums[1]); [Link](nums[2]); [Link](nums[3]); [Link](mul);
[Link](nums[0]); [Link](clr); [Link](eq); [Link](div);
add(p, [Link]);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
Object o = [Link]();
for(int i=0;i<10;i++) {
if(o == nums[i]) {
[Link]([Link]() + i);
return;
}
}
if(o == clr) {
[Link]("");
}
else if(o == add || o == sub || o == mul || o == div) {
a = [Link]([Link]());
op = ((JButton)o).getText().charAt(0);
[Link]("");
}
else if(o == eq) {
b = [Link]([Link]());
int r = 0;
switch(op) {
case '+': r = a+b; break;
case '-': r = a-b; break;
case '*': r = a*b; break;
case '/': r = a/b; break;
}
[Link](""+r);
}
}
public static void main(String[] args) {
new Calc();
}
}
26. Write a program to count the vowels and digits, which prompts the user for a String,
counts the number of vowels (a, e, i, o, u, A, E, I, O, U) and digits (0-9) contained in the
string, and prints the counts and the percentages (rounded to 2 decimal places).
A. import [Link].*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("enter string : ");
String s = [Link]();
int v = 0, d = 0;
for(int i=0;i<[Link]();i++) {
char c = [Link](i);
if("aeiouAEIOU".indexOf(c) != -1)
v++;
if(c >= '0' && c <= '9')
d++;
}
int n = [Link]();
double vp = (v * 100.0) / n;
double dp = (d * 100.0) / n;
[Link]("vowels = %d (%.2f%%)\n", v, vp);
[Link]("digits = %d (%.2f%%)\n", d, dp);
}
}
Output:
vowels = 2 (25.00%)
digits = 3 (37.50%)
27. Write a program CaesarCode to cipher the Caesar's code. The program shall prompt user
for a plaintext string consisting of mix-case letters only; compute the ciphertext; and print
the ciphertext in uppercase.
A. import [Link].*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("enter text : ");
String s = [Link]();
String res = "";
for(int i = 0; i < [Link](); i++) {
char c = [Link](i);
if([Link](c)) {
c = [Link](c);
c = (char)( (c - 'A' + 3) % 26 + 'A' ); // shift by 3
res += c;
}
}
[Link]("cipher : " + res);
}
}
Output:
cipher : DEFABC
28. Demonstrate garbage collection.
A. class Demo {
int id;
Demo(int x) { id = x; }
protected void finalize() {
[Link]("object " + id + " destroyed");
}
}
class Main {
public static void main(String[] args) {
Demo d1 = new Demo(1);
Demo d2 = new Demo(2);
d1 = null;
d2 = null;
[Link](); // request garbage collection
[Link]("end main");
}
}
Output:
end main
object 1 destroyed
object 2 destroyed