19IT419 3IT03 LabManual

Download as pdf or txt
Download as pdf or txt
You are on page 1of 111

Lab Manual

of
Object Oriented Programming with Java

Subject Code: 3IT03

Academic Year 2021-22

Name of Student : Aryan Bhatt


Id.No. : 19IT419
Batch : B- 10
Faculty Name : Dr. Vatsal Shah

BACHELOR OF ENGINEERING
In
INFORMATION TECHNOLOGY

Birla Vishwakarma Mahavidhyalaya Engineering College


An Autonomous Institute
Vallabh Vidhyanagar-388120
Birla VishvakarmaMahavidyalaya Engineering College
An Autonomous Institute
Information Technology Department
2021-2022

CERTIFICATE

This is to certify that Mr. / Mrs. Aryan Bhatt of Class/Sem. 5th

Id. No. 19IT419 has satisfactorily completed his/her term work in

Month of NOVEMBER for the term ending in

2020/2021, No. of Practical certified 29 out of 29 in the subject of

3IT03: Object Oriented Programming with JAVA.

Date: 02 / 11 / 2021

Signature of Teacher
3IT03: Object Oriented Programming with JAVA

INDEX
Sr. Date Practical Pg. Sign
No. No.
1. Write a programme to make a simple calculator.
2. Write a programme to check a number is palindrome or
not.
3. Write a programme to check a number is prime or not
between given range.
4. Write a programme to implement matrix multiplication
5. Write a programme to implement sum of digits of a
number
6. Write a programme to implement a number is Armstrong
or not.
7. Write a programme to implement dynamic stack.
8. Write a programme to demonstrate constructor
overloading and method overloading.
9. Write a programme to set up an array of 10 variables
each containing an arbitrary string of form month date
year for example 30/10/19 and output as 30th October
1999.
10. Write a programme to define a mcm length to represent
a length measured in meters and millimeters each stored
as integers. Include method to add and subtract object to
multiply and dived an object by an integer value to
calculate area resulting from the product of two objects
and two compare objects.
Include constructors that accept
I. Three arguments meters, cm, mm
II. One integer argument with length set to
zero.
Create the class by creating some objects and
testing the class operations.

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

11. Write a programme to implement factorial of a number


using recursion.
12. Write a programme to implement G.C.D of numbers
using recursion.
13. Write a programme to check a matrix is a magic square
matrix or not
14. Write a programme which shows inheritance.
15. Write a programme which shows use of final and abstract
keyword.
16.
Write a programme which implement addition and
subtraction for complex number.
17.
Write a programme to implement singly linked list.
18. Write a programme to implement Circular singly linked
list.
19. Write a programme to implement Doubly linked list.
20. Write a programme to implement Circular Doubly linked
list.
21. Write a programme to implement access specification
using package.
22. Write a programme to implement user (Custom)
exception subclass.
23. Write a programme to implement threads by
implementing Runnable class and by extending Thread
class.
24. Write a programme to implement producer and
consumer problem.
25. Write a programme to create file using ByteStream class.
26. Write a programme to copy one file to another.
27. Write an applet programme to change the color of
rectangle using to change the value of red, green and blue.
28. Write an applet programme to implement moving
banner.
29. Write a programme to handle mouse and Keyboard
events in Frame.

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Practical-1: Write a program to make a simple calculator.

class Calc

public static void main(String arg [])

int a=15,b=35;

System.out.println("Sum is "+(a+b));

System.out.println("Sub is "+(a-b));

System.out.println("Mul is "+(a*b));

System.out.println("Div is "+(a/b));

Output:

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Practical-2: Write a program to check a number is palindrome or not.

import java.util.Scanner;

class Palindrome

public static void main(String[] arg)

Scanner sc = new Scanner(System.in);

int n,temp,sum=0;

System.out.print("Enter Number : ");

n = sc.nextInt();

temp=n;

while(n>0)

sum=(10*sum)+(n%10);

n=n/10;

if(temp==sum)

System.out.println(temp+" is a Palindrome");

else

System.out.println(temp+" is not a Palindrome");

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Output:

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Practical-3: Write a program to check a number is prime or not between


given range.

import java.util.Scanner;

class Prime

public static void main(String[] arg)

int a,b,n,flag=0;

Scanner sc = new Scanner(System.in);

System.out.print("Enter Number to Check Prime or not : ");

n = sc.nextInt();

System.out.print("Enter Starting Range Number : ");

a = sc.nextInt();

System.out.print("Enter Ending Range Number : ");

b = sc.nextInt();

if(a>b)

a=a+b;

b=a-b;

a=a-b;

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

if(n<=b&&n>=a)

for(int i=2;i<n;i++)

if(n%i==0)

break;

if(i==n-1)

flag=1;

if(flag==1||n==2)

System.out.println(n+" is prime number between "+a+" and "+b);

else

System.out.println(n+" is not a prime number between "+a+" and "+b);

else

System.out.println(n+" is not between "+a+" and "+b);

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Output:

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Practical-4: Write a program to implement matrix multiplication

import java.util.Scanner;

class MatMul

public static void main(String arg[])

int a[][] = new int[3][3];

int b[][] = new int[3][3];

System.out.println("Enter matrix A : ");

for(int i=0;i<3;i++)

for(int j=0;j<3;j++)

System.out.print("Enter A["+i+"]["+j+"] = ");

Scanner sc = new Scanner(System.in);

a[i][j]=sc.nextInt();

System.out.println("Enter matrix B : ");

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

for(int i=0;i<3;i++)

for(int j=0;j<3;j++)

System.out.print("Enter B["+i+"]["+j+"] = ");

Scanner sc = new Scanner(System.in);

b[i][j]=sc.nextInt();

System.out.println("Addition of Marix is :");

for(int i=0;i<3;i++)

for(int j=0;j<3;j++)

System.out.print((a[i][j]+b[i][j])+"\t");

System.out.println();

int c[][] = new int[3][3];

System.out.println("Multiplication of Matrix is : ");

for(int i=0;i<3;i++)

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

for(int j=0;j<3;j++)

c[i][j]=0;

for(int k=0;k<3;k++)

c[i][j]+=a[i][k]*b[k][j];

System.out.print(c[i][j]+"\t");

System.out.println();

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Output:

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Practical-5: Write a program to implement sum of digits of a number

import java.util.Scanner;

class SumofDigit

public static void main(String[] arg)

Scanner sc = new Scanner(System.in);

int n,sum=0,temp;

System.out.print("Enter number : ");

n = sc.nextInt();

temp=n;

while(n>0)

sum=sum+n%10;

n/=10;

System.out.println("Sum of digit for number "+temp+" is "+sum);

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Output:

Practical-6: Write a program to implement a number is Armstrong or not.

import java.util.Scanner;

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

class Armstrong

public static void main(String[] arg)

Scanner sc = new Scanner(System.in);

int n,sum=0,temp;

System.out.print("Enter Number : ");

n = sc.nextInt();

temp=n;

while(n>0)

int r = n%10;

sum=sum+(r*r*r);

n/=10;

if(temp==sum)

System.out.println(temp+" is a Armstrong Number ");

else

System.out.println(temp+" is not a Armstrong Number");

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Output:

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Practical-7: Write a program to implement dynamic stack.

import java.util.Scanner;

class Stack

int size,top;

int[] stack;

Stack(int n)

size = n;

stack = new int[size];

top = -1;

void push(int value)

if(top == stack.length - 1)

int newStack[] = new int[2*stack.length];

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

for(int i=0;i<stack.length;i++)

newStack[i] = stack[i];

stack = newStack;

stack[++top] = value;

void pop()

if(top == -1)

System.out.println("Stack UnderFlow\n");

else

top--;

void display()

System.out.print("Stack : ");

for(int i=0;i<=top;i++)

System.out.print(stack[i]+"\t");

System.out.println("");

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

class DynamicStack

public static void main(String[] arg)

int size,choice=0;

Scanner sc = new Scanner(System.in);

System.out.print("Enter Size of Stack : ");

size = sc.nextInt();

Stack obj = new Stack(size);

while(choice != 4)

System.out.println("");

System.out.println("1 Push");

System.out.println("2 Pop");

System.out.println("3 Display");

System.out.println("4 Exit");

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

System.out.print("Enter your Choice : ");

choice = sc.nextInt();

if(choice == 1)

int value;

System.out.print("Enter Value : ");

value = sc.nextInt();

obj.push(value);

else if(choice == 2)

obj.pop();

else if(choice == 3)

obj.display();

else if(choice == 4)

break;

else

System.out.println("Invalid Input");

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Output:

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Practical-8: Write a program to demonstrate constructor overloading and


method overloading.

import java.util.Scanner;

class A

int a,b,c;

A(int x,int y)

a=x;

b=y;

c=0;

System.out.println("Construxter 1\nA = "+a+"\tB = "+b+"\tC = "+c);

A(int x,int y,int z)

a=x;

b=y;

c=z;

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

System.out.println("Construxter 2\nA = "+a+"\tB = "+b+"\tC = "+c);

void display(int x)

System.out.println("Method 1\nX = "+x);

void display(int x,int y)

System.out.println("Method 2\nX = "+x+"\tY = "+y);

class MethodOverloading

public static void main(String[] arg)

//Scanner sc = new Scanner(System.in);

A ob1 = new A(10,20);

A ob2 = new A(11,22,33);

ob1.display(5);

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

ob1.display(5,10);

Output:

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Practical-9: Write a program to set up an array of 10 variables each


containing an arbitrary string of form month date year for example 30/10/19
and output as 30th October 1999.

import java.util.Random;

class IntToString

static String end(int n)

if(n==1)

return "st";

else if(n==2)

return "nd";

else if(n==3)

return "rd";

else

return "th";

static String monthString(int n)

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

String[] month = {"January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"};

return month[n-1];

static void getValue()

Random r = new Random();

int month,day,year;

String dayAns,monthAns;

month = r.nextInt(12);

month++;

monthAns=monthString(month);

if(month==4||month==6||month==9||month==11)

day=r.nextInt(30);

else if(month==2)

day=r.nextInt(28);

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

else

day=r.nextInt(31);

day++;

dayAns=Integer.toString(day)+end(day);

year = r.nextInt(100);

String s,ans;

s = Integer.toString(day)+'/'+Integer.toString(month)+'/'+Integer.toString(year);

year=year+2000;

ans = dayAns +' '+ monthAns+' '+Integer.toString(year);

System.out.println(s+"\n"+ans+"\n\n");

public static void main(String[] arg)

String[] date = new String[10];

for(int i=0;i<10;i++)

getValue();

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Output:

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Practical-10: Write a programme to define a mcm length to represent a length


measured in meters and millimeters each stored as integers. Include method
to add and subtract object to multiply and dived an object by an integer value
to calculate area resulting from the product of two objects and two compare
objects.

Include constructors that accept


III. Three arguments meters, cm, mm
IV. One integer argument with length set to zero.

Create the class by creating some objects and testing the class operations.

class Mcm{

int m,cm,mm;

Mcm(int d_m ,int d_cm , int d_mm)

m=d_m;

cm=d_cm;

mm=d_mm;

int getMeter(){ return m;

int getCentiMeter(){ return cm;

int getMiliMeter(){ return mm;

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

void addSub(int x,int op)

if(op==1)

m+=x;

cm+=x;

mm+=x;

if(op==2){ m-=x;

cm-=x;

mm-=x;

void mulDiv(int x,int op)

if(op==1)

m*=x; cm*=x; mm*=x;

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

if(op==2){ m/=x; cm/=x; mm/=x;

int area(Mcmobj){ return

(this.m+this.cm/100+this.mm/1000)*(obj.m+obj.cm/100+obj.mm/1000);

void compare(Mcm obj1, Mcm obj2){

String s="Object 1 == Object 2";

String s1="Object 1 > Object 2";

String s2="Object 2 > Object 1";

if(obj1.m>obj2.m){ System.out.println(s1);

else if(obj2.m>obj2.m){ System.out.println(s2);

else{

if(obj1.cm>obj2.cm){

System.out.println(s1);

else if(obj2.cm>obj2.cm)

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

System.out.println(s2);

if(obj1.mm>obj2.mm){

System.out.println(s1);

else if(obj2.mm>obj2.mm){ System.out.println(s2);

else{ System.out.println(s);

void displayAll(){

System.out.println("Meter : "+m+"\nCentimeter : "+cm+"\nMilimeter : "+mm);

public class ClassOperation{

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

public static void main(String[] args) { Mcm m1 = new Mcm(1,2,3);

Mcm m2 = new Mcm(3,4,0);

m1.addSub(10, 1);

m2.addSub(2, 2);

m1.compare(m1,m2); System.out.println("Object 1 :");

m1.displayAll();

System.out.println("Object 2 :"); m2.displayAll();

Output:

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Practical-11: Write a program to implement factorial of a number using


recursion.

import java.util.*;

class Factorial

static int fac(int n)

if(n<1)

return 1;

return n*fac(n-1);

public static void main(String[] arg)

int n;

Scanner sc = new Scanner(System.in);

System.out.print("Enter Number : ");

n=sc.nextInt();

System.out.println(fac(n));

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Output:

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Practical-12: Write a program to implement G.C.D of numbers using


recursion.

import java.util.Scanner;

class GDC

static int gcd(int a,int b)

if(a%b==0)

return b;

return gcd(b,a%b);

public static void main(String[] arg)

Scanner sc = new Scanner(System.in);

int a,b;

System.out.print("Enter Number One : ");

a=sc.nextInt();

System.out.print("Enter Number Two : ");

b=sc.nextInt();

if(b<a)

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

a=a+b;

b=a-b;

a=a-b;

System.out.print("Ans = "+gcd(a,b));

Output:

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Practical-13: Write a program to check a matrix is a magic square matrix or


not

import java.util.Scanner;

class MagicMatrix

public static void main(String[] arg)

int n;

Scanner sc = new Scanner(System.in);

System.out.print("Enter Number of Row or column : ");

n = sc.nextInt();

int a[][] = new int[n][n];

int column[],row[],flag=0,dia1=0,dia2=0,sum=n*((n*n)+1)/2;

column = row = new int[n];

//row = new int[n];

for(int i=0;i<n;i++)

for(int j=0;j<n;j++)

System.out.print("Enter Value of row "+(i+1)+" and column "+(j+1)+" = ");

a[i][j] = sc.nextInt();

if(flag==0)

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

for(int q=0;q<i;q++)

for(int r=0;r<j;r++)

if(a[q][r]==a[i][j])

flag=1;

break;

row[i] += a[i][j];

column[j] += a[i][j];

if(i==j)

dia1+=a[i][j];

if((i+j)==(n-1))

dia2+=a[i][j];

System.out.println("Matrix : ");

for(int i=0;i<n;i++)

for(int j=0;j<n;j++)

System.out.print(a[i][j]+"\t");

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

System.out.println("");

if(dia2==dia1&&dia1==sum)

for(int i=0;i<n;i++)

if(sum!=column[i]||sum!=row[i])

flag=1;

break;

else

flag=1;

if(flag==0)

System.out.println("\nIt's Magic Square with sum = "+sum);

else

System.out.println("\nIt's Not Magic square");

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Output:

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Practical-14: Write a program which shows inheritance.

class Base

int n=2;

Base()

System.out.println("It's Base Constructer");

class Child extends Base

Child()

System.out.println("It's Child Constructer");

void display()

System.out.println("Value of n = "+n);

class Inheritance

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

public static void main(String[] arg)

Child ob = new Child();

ob.display();

Output:

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Practical-15: Write a programme which shows use of final and abstract


keyword.

Abstract class A

final int n = 1;

A()

System.out.println(“It’s A Constructer”);

void display()

System.out.println(“It’s Display from A n = “+n);

final class B extends A

B()

System.out.println(“It’s B Constructer”);

void display()

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

super.display();

//n is final can’t change value

//n=2;

System.out.println(“It’s Display from B N = “+n);

/*

B is final class it’s can’t Inherit

class C extends B

C()

System.out.println(“It’s C Constructer”);

}*/

class FinalAbs

public static void main(String[] arg)

//C ob1 = new C();

A ob2;

ob2 = new B();

ob2.display();

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Output:

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Practical-16: Write a program which implement addition and subtraction for


complex number.

class Complex

public static void main(String args[])

int x1=3;

int y1=6;

int x2=-3;

int y2=2;

System.out.println("First Vector : "+x1+"+"+y1+"i");

System.out.println("First Vector : "+x2+"+"+y2+"i");

System.out.println("Addition : "+ (x1+x2) + "+" + (y1+y2) +"i");

System.out.println("Substraction : "+ (x1-x2) + "+" + (y1-y2) +"i");

int real = x1*x2 - y1*y2;

int img = x1*y2 + y1*x2;

System.out.println("Multiplication : "+ real + "+" + img +"i");

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Output:

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Practical-17: Write a program to implement singly linked list.

import java.util.Scanner;

class Node

int data;

Node next;

Node()

data = 0;

next = null;

Node(int d_data)

data = d_data;

next = null;

void append(Node obj)

Node temp = this;

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

while(temp.next!=null)

temp=temp.next;

temp.next = obj;

void display()

Node temp = this;

System.out.print("Value : ");

while(temp.next!=null)

System.out.print(temp.data+", ");

temp=temp.next;

System.out.println(temp.data);

int search(int d_data)

Node temp = this;

int k=1;

while(temp.data!=d_data)

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

if(temp.next==null)

return -1;

k++;

temp = temp.next;

return k;

class SLL

public static void main(String[] arg)

int choice=0,i=0;

Node ob[] = new Node[100];

Scanner sc = new Scanner(System.in);

while(choice!=6)

System.out.println("1 Append");

System.out.println("2 Insert");

System.out.println("3 Delete");

System.out.println("4 Display");

System.out.println("5 Search");

System.out.println("6 Exit");

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

System.out.print("Enter Your Choice : ");

choice = sc.nextInt();

if(choice == 1)

System.out.print("Enter Value : ");

ob[i++] = new Node(sc.nextInt());

if(i>1)

ob[0].append(ob[i-1]);

else if(choice == 2)

System.out.print("Enter Value : ");

int value = sc.nextInt();

System.out.print("Enter Value position Node : ");

int pos = sc.nextInt();

if(pos>i||pos<=0)

System.out.println("Invalid Position");

else

Node temp = new Node(value);

for(int j=i++;j>=pos;j--)

ob[j]=ob[j-1];

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

ob[pos-1] = temp;

ob[pos-1].next = ob[pos];

if(pos>1)

ob[pos-2].next = ob[pos-1];

else if(choice == 3)

if(i==0||i==1)

System.out.println("Deletion Can't Be Perfrom");

else

System.out.print("Enter Position of Node : ");

int pos = sc.nextInt();

i--;

for(int j=pos-1;j<i;j++)

ob[j] = ob[j+1];

ob[i-1].next = null;

if(pos>1&&pos!=(i+1))

ob[pos-2].next = ob[pos-1];

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

else if(choice == 4)

ob[0].display();

else if(choice == 5)

System.out.print("Enter Searching Value : ");

int value = sc.nextInt();

int pos = ob[0].search(value);

if(pos!=-1)

System.out.println(value+" is available at "+pos);

else

System.out.println(value+" is not available");

else if(choice == 6)

break;

else

System.out.println("Invalid Input");

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Output:

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Practical-18: Write a program to implement Circular singly linked list.

import java.util.Scanner;

class Node

int data;

Node next;

Node()

data = 0;

next = this;

Node(int d_data)

data = d_data;

next = this;

void append(Node obj)

Node temp = this;

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

while(temp.next!=this)

temp=temp.next;

temp.next = obj;

obj.next = this;

void display()

Node temp = this;

System.out.print("Value : ");

while(temp.next!=this)

System.out.print(temp.data+", ");

temp=temp.next;

System.out.println(temp.data);

int search(int d_data)

Node temp = this;

int k=1;

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

while(temp.data!=d_data)

if(temp.next==this)

return -1;

k++;

temp = temp.next;

return k;

class CSLL

public static void main(String[] arg)

int choice=0,i=0;

Node ob[] = new Node[100];

Scanner sc = new Scanner(System.in);

while(choice!=6)

System.out.println("\n1 Append");

System.out.println("2 Insert");

System.out.println("3 Delete");

System.out.println("4 Display");

System.out.println("5 Search");

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

System.out.println("6 Exit");

System.out.print("Enter Your Choice : ");

choice = sc.nextInt();

if(choice == 1)

System.out.print("Enter Value : ");

ob[i++] = new Node(sc.nextInt());

if(i>1)

ob[0].append(ob[i-1]);

else if(choice == 2)

System.out.print("Enter Value : ");

int value = sc.nextInt();

System.out.print("Enter Value position Node : ");

int pos = sc.nextInt();

if(pos>i||pos<=0)

System.out.println("Invalid Position");

else

Node temp = new Node(value);

for(int j=i++;j>=pos;j--)

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

ob[j]=ob[j-1];

ob[pos-1] = temp;

ob[pos-1].next = ob[pos];

if(pos == 1)

ob[i-1].next = ob[0];

else

ob[pos-2].next = ob[pos-1];

else if(choice == 3)

if(i==0||i==1)

System.out.println("Deletion Can't Be Perfrom");

else

System.out.print("Enter Position of Node : ");

int pos = sc.nextInt();

System.out.println("I = "+i);

i--;

for(int j=pos-1;j<i;j++)

ob[j] = ob[j+1];

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

ob[i] = null;

ob[i-1].next = ob[0];

if(pos>1&&pos!=(i+1))

ob[pos-2].next = ob[pos-1];

else if(choice == 4)

ob[0].display();

else if(choice == 5)

System.out.print("Enter Searching Value : ");

int value = sc.nextInt();

int pos = ob[0].search(value);

if(pos!=-1)

System.out.println(value+" is available at "+pos);

else

System.out.println(value+" is not available");

else if(choice == 6)

break;

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

else

System.out.println("Invalid Input");

Output:

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Practical-19: Write a program to implement Doubly linked list.

import java.util.Scanner;

class Node

int data;

Node next,prev;

Node()

data = 0;

next = null;

prev = null;

Node(int d_data)

data = d_data;

next = null;

prev = null;

void append(Node obj)

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Node temp = this;

while(temp.next!=null)

temp=temp.next;

obj.prev = temp;

temp.next = obj;

void display()

Node temp = this;

System.out.print("Value : ");

while(temp.next!=null)

System.out.print(temp.data+", ");

temp=temp.next;

System.out.println(temp.data);

int search(int d_data)

Node temp = this;

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

int k=1;

while(temp.data!=d_data)

if(temp.next==null)

return -1;

k++;

temp = temp.next;

return k;

class DLL

public static void main(String[] arg)

int choice=0,i=0;

Node ob[] = new Node[100];

Scanner sc = new Scanner(System.in);

while(choice!=6)

System.out.println("1 Append");

System.out.println("2 Insert");

System.out.println("3 Delete");

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

System.out.println("4 Display");

System.out.println("5 Search");

System.out.println("6 Exit");

System.out.print("Enter Your Choice : ");

choice = sc.nextInt();

if(choice == 1)

System.out.print("Enter Value : ");

ob[i++] = new Node(sc.nextInt());

if(i>1)

ob[0].append(ob[i-1]);

else if(choice == 2)

System.out.print("Enter Value : ");

int value = sc.nextInt();

System.out.print("Enter Value position Node : ");

int pos = sc.nextInt();

if(pos>i||pos<=0)

System.out.println("Invalid Position");

else

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Node temp = new Node(value);

for(int j=i++;j>=pos;j--)

ob[j]=ob[j-1];

ob[pos-1] = temp;

ob[pos-1].next = ob[pos];

ob[pos].prev = ob[pos-1];

if(pos>1)

ob[pos-2].next = ob[pos-1];

ob[pos-1].prev = ob[pos-2];

else if(choice == 3)

if(i==0||i==1)

System.out.println("Deletion Can't Be Perfrom");

else

System.out.print("Enter Position of Node : ");

int pos = sc.nextInt();

i--;

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

for(int j=pos-1;j<i;j++)

ob[j] = ob[j+1];

ob[i] = null;

ob[i-1].next = null;

ob[0].prev = null;

if(pos>1&&pos!=(i+1))

ob[pos-2].next = ob[pos-1];

ob[pos-1].prev = ob[pos-2];

else if(choice == 4)

ob[0].display();

else if(choice == 5)

System.out.print("Enter Searching Value : ");

int value = sc.nextInt();

int pos = ob[0].search(value);

if(pos!=-1)

System.out.println(value+" is available at "+pos);

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

else

System.out.println(value+" is not available");

else if(choice == 6)

break;

else

System.out.println("Invalid Input");

Output:

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Practical-20: Write a program to implement Circular Doubly linked list.

import java.util.Scanner;

class Node

int data;

Node next,prev;

Node()

data = 0;

next = this;

prev = this;

Node(int d_data)

data = d_data;

next = this;

prev = this;

void append(Node obj)

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Node temp = this;

while(temp.next!=this)

temp=temp.next;

obj.prev = temp;

temp.next = obj;

obj.next = this;

this.prev = obj;

void display()

Node temp = this;

System.out.print("Value : ");

while(temp.next!=this)

System.out.print(temp.data+", ");

temp=temp.next;

System.out.println(temp.data);

int search(int d_data)

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Node temp = this;

int k=1;

while(temp.data!=d_data)

if(temp.next==this)

return -1;

k++;

temp = temp.next;

return k;

class CDLL

public static void main(String[] arg)

int choice=0,i=0;

Node ob[] = new Node[100];

Scanner sc = new Scanner(System.in);

while(choice!=6)

System.out.println("1 Append");

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

System.out.println("2 Insert");

System.out.println("3 Delete");

System.out.println("4 Display");

System.out.println("5 Search");

System.out.println("6 Exit");

System.out.print("Enter Your Choice : ");

choice = sc.nextInt();

if(choice == 1)

System.out.print("Enter Value : ");

ob[i++] = new Node(sc.nextInt());

if(i>1)

ob[0].append(ob[i-1]);

else if(choice == 2)

System.out.print("Enter Value : ");

int value = sc.nextInt();

System.out.print("Enter Value position Node : ");

int pos = sc.nextInt();

if(pos>i||pos<=0)

System.out.println("Invalid Position");

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

else

Node temp = new Node(value);

for(int j=i++;j>=pos;j--)

ob[j]=ob[j-1];

ob[pos-1] = temp;

ob[pos-1].next = ob[pos];

ob[pos].prev = ob[pos-1];

if(pos == 1)

ob[i-1].next = ob[0];

ob[0].prev = ob[i-1];

else

ob[pos-2].next = ob[pos-1];

ob[pos-1].prev = ob[pos-2];

else if(choice == 3)

if(i==0||i==1)

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

System.out.println("Deletion Can't Be Perfrom");

else

System.out.print("Enter Position of Node : ");

int pos = sc.nextInt();

i--;

for(int j=pos-1;j<i;j++)

ob[j] = ob[j+1];

ob[i] = null;

ob[i-1].next = ob[0];

ob[0].prev = ob[i-1];

if(pos>1&&pos!=(i+1))

ob[pos-2].next = ob[pos-1];

ob[pos-1].prev = ob[pos-2];

else if(choice == 4)

ob[0].display();

else if(choice == 5)

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

System.out.print("Enter Searching Value : ");

int value = sc.nextInt();

int pos = ob[0].search(value);

if(pos!=-1)

System.out.println(value+" is available at "+pos);

else

System.out.println(value+" is not available");

else if(choice == 6)

break;

else

System.out.println("Invalid Input");

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Output:

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Practical-21: Write a program to implement access specification using


package.

Pack > p1 > Pac1.java package p1;

public class Pac1{

int noVar=0; public

int publicVar=1; private

int privateVar=2; protected

int protectedVar=3;

public static void main(String[] args) { B ob = new B();

System.out.println("Same Package different class"); System.out.println("No access Modifier :


"+ob.noVar); System.out.println("Public : "+ob.publicVar);

class B extends Pac1{ B(){

System.out.println("Same Package Child class"); System.out.println("Protected :


"+protectedVar); System.out.println("No access Modifier : "+noVar); System.out.println("Public
: "+publicVar);

Pack > p2 >Pac2.java package p2;

// import p1.A;

class C extends p1.Pac1{ C(){

System.out.println("Public : "+publicVar); System.out.println("Protected : "+protectedVar);

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

class Pac2{

public static void main(String[] args) { C ob = new C();

Output:

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Practical-22: Write a program to implement user (Custom) exception


subclass.

import java.util.*;

class Exp extends Exception

public String toString()

return "Exception !!";

public String getMessage()

return "Error..!!";

class College

public static void main(String args[])

int age;

double cpi;

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

String name;

String dept;

Scanner sc=new Scanner(System.in);

System.out.println("Enter Name:");

name = sc.nextLine();

System.out.println("Enter Department:");

dept = sc.nextLine();

System.out.println("Enter Age:");

age = sc.nextInt();

System.out.println("Enter CPI:");

cpi = sc.nextDouble();

if(dept.equals("IT") && age>=23 && cpi>=4.5 && cpi<=6.5)

try

throw new MyException();

catch(Exception e)

System.out.println(e.getMessage());

else

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

System.out.println("Finish");

Output:

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Practical-23: Write a program to implement threads by implementing


Runnable class and by extending Thread class.

import java.util.Scanner;

class PaliThreadEx extends Thread

int a,b;

PaliThreadEx(int a,int b)

super("Palindrome");

this.a = a;

this.b = b;

System.out.println("Child Extended Thread : "+this);

start();

public void run()

try

for(int i=a;i<=b;i++)

int temp=i,sum=0;

while(temp>0)

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

sum=sum*10+temp%10;

temp/=10;

if(sum==i)

System.out.println("Palindrome from Extends : "+i);

Thread.sleep(500);

catch(InterruptedException e)

System.out.println("Error : "+e);

class PaliThreadIn implements Runnable

int a,b;

Thread ob;

PaliThreadIn(int a,int b)

ob = new Thread(this,"Palindrome");

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

System.out.println("Child Runnable Thread : "+this);

this.a = a;

this.b = b;

ob.start();

public void run()

try

for(int i=a;i<=b;i++)

int temp=i,sum=0;

while(temp>0)

sum=sum*10+temp%10;

temp/=10;

if(sum==i)

System.out.println("Palindrome from Runnable : "+i);

Thread.sleep(500);

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

catch(InterruptedException e)

System.out.println("Error : "+e);

class ThreadByTwoMethod

public static void main(String[] args)

int a,b;

Scanner sc = new Scanner(System.in);

System.out.print("Enter Range A = ");

a = sc.nextInt();

System.out.print("Enter Range B = ");

b = sc.nextInt();

//PaliThreadEx obj = new PaliThreadEx(a,b);

PaliThreadIn obj = new PaliThreadIn(a,b);

try

for(int i=a;i<=b;i++)

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

if(i%5==0)

System.out.println("Divisible : "+i);

Thread.sleep(500);

catch(InterruptedException e)

System.out.println("Error : "+e);

Output:

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Practical-24: Write a program to implement producer and consumer


problem.

class Work

int n;

boolean flag = false;

synchronized void put(int n)

if(flag)

try

wait();

catch(InterruptedException Ex)

System.out.println("Error : "+Ex);

this.n = n;

flag = true;

System.out.println("Put : "+n);

notify();

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

synchronized void get()

if(!flag)

try

wait();

catch(InterruptedException Ex)

System.out.println("Error : "+Ex);

flag = false;

System.out.println("Get : "+n);

notify();

class Producer implements Runnable

Work ob;

Producer(Work tmp)

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

ob = tmp;

Thread t = new Thread(this,"Producer");

t.start();

public void run()

int i=0;

while(true)

ob.put(++i);

class Consumer implements Runnable

Work ob;

Consumer(Work tmp)

ob = tmp;

Thread t = new Thread(this,"Consumer");

t.start();

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

public void run()

while(true)

ob.get();

class ProdCon

public static void main(String[] args)

Work ob = new Work();

Producer p = new Producer(ob);

Consumer c = new Consumer(ob);

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Output:

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Practical-25: Write a program to create file using ByteStream class.

import java.io.*;

import java.util.Scanner;

class ByteFile

public static void main(String[] args) throws IOException

try

Scanner sc = new Scanner(System.in);

String s;

System.out.print("Enter File Name : ");

s=sc.nextLine();

FileOutputStream fo = new FileOutputStream(s,true);

while(true)

String m;

m = sc.nextLine()+"\n";

if(m.equals("stop\n"))

break;

byte[] b = m.getBytes();

fo.write(b);

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

fo.close();

System.out.println("File Saved");

catch(Exception Ex)

System.out.println("Error:"+Ex);

Output:

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Practical-26: Write a program to copy one file to another.

import java.util.Scanner;

import java.io.*;

class CopyFile

public static void main(String[] args) throws IOException

try

Scanner sc = new Scanner(System.in);

System.out.print("Enter File Name : ");

String old,fresh;

old = sc.nextLine();

System.out.print("Enter New File Name : ");

fresh = sc.nextLine();

FileInputStream fi = new FileInputStream(old);

FileOutputStream fo = new FileOutputStream(fresh);

int n;

while((n=fi.read())!=-1)

fo.write(n);

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

fo.close();

fi.close();

System.out.println("File Copyed");

catch(Exception Ex)

System.out.println("Error : "+Ex);

Output:

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Practical-27: Write an applet program to change the color of rectangle using


to change the value of red, green and blue.

import java.awt.*;

import java.applet.*;

/*

<applet code="banner.class" width=300 height=200>

</applet>

*/

public class banner extends Applet implements Runnable

String msg="My name is Aryan bhatt ";

Thread t=null;

int state;

boolean flag;

public void init()

setBackground(Color.yellow);

setForeground(Color.red);

public void start()

t=new Thread(this);

flag=false;

t.start();

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

public void run()

char ch;

for( ; ; )

try

repaint();

Thread.sleep(1000);

ch = msg.charAt(0);

msg = msg.substring(1, msg.length());

msg += ch;

if(flag)

break;

} catch(InterruptedException e) {}

public void stop()

flag=true;

t=null;

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Output:

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Practical-28: Write an applet program to implement moving banner.

import java.awt.*; import java.applet.*;

/*

<applet code="BannerMov" width=300 height=50>

</applet>

*/

public class BannerMov extends Applet implements Runnable

String msg="Hello Friends "; Thread t;

public void init()

setBackground(Color.black); setForeground(Color.white);

public void start()

t = new Thread(this); t.start();

public void run()

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

char ch; while(true)

try

repaint(); Thread.sleep(250); ch = msg.charAt(0);

msg = msg.substring(1,msg.length()); msg+=ch;

catch(InterruptedException Ex){}

public void paint(Graphics g)

g.drawString(msg,100,30);

Output:

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Practical-29: Write a program to handle mouse and Keyboard events in


Frame.

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*<applet code="lab11_1" width=300 height=200></applet>*/

class SampleFrame extends Frame implements MouseListener,

MouseMotionListener,KeyListener{

String msg = "";

int mouseX=10, mouseY=40;

SampleFrame(String title) {

super(title);

addMouseListener(this);

addMouseMotionListener(this);

addKeyListener(this);

requestFocus();

MyWindowAdapter adapter = new MyWindowAdapter(this);

addWindowListener(adapter);

public void mouseClicked(MouseEvent me) {

msg = "Clicked";

repaint();

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

public void mouseEntered(MouseEvent evtObj) {

msg = "Mouse entered";

repaint();

public void mouseExited(MouseEvent evtObj) {

msg = "Mouse exited";

repaint();

public void mousePressed(MouseEvent me) {

mouseX = me.getX();

mouseY = me.getY();

msg = "Pressed";

repaint();

public void mouseReleased(MouseEvent me) {

mouseX = me.getX();

mouseY = me.getY();

msg = "Released";

repaint();

public void mouseDragged(MouseEvent me) {

mouseX = me.getX();

mouseY = me.getY();

msg = "Dragging";

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

repaint();

public void mouseMoved(MouseEvent me) {

repaint(0, 0, 100, 60);

public void keyPressed(KeyEvent ke) {

msg="Key Pressed";

repaint();

public void keyReleased(KeyEvent ke) {

msg="Key Released";

repaint();

public void keyTyped(KeyEvent ke) {

msg += ke.getKeyChar();

repaint();

public void paint(Graphics g) {

g.drawString(msg, mouseX, mouseY);

class MyWindowAdapter extends WindowAdapter{

SampleFrame sampleFrame;

public MyWindowAdapter(SampleFrame sampleFrame) {

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

this.sampleFrame = sampleFrame;

public void windowClosing(WindowEvent we) {

sampleFrame.setVisible(false);

public class lab11_1 extends Applet implements

MouseListener,MouseMotionListener,KeyListener{

SampleFrame f;

String msg = "";

int mouseX=10, mouseY=10;

public void init() {

f = new SampleFrame("Handle Mouse & Key Events");

f.setSize(500, 300);

f.setVisible(true);

addMouseListener(this);

addMouseMotionListener(this);

addKeyListener(this);

requestFocus();

public void stop() {

f.setVisible(false);

public void start() {

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

f.setVisible(true);

public void mouseClicked(MouseEvent me) {

msg="Clicked";

repaint();

public void mouseEntered(MouseEvent me) {

msg = "Mouse entered.";

repaint();

public void mouseExited(MouseEvent me) {

msg = "Mouse exited.";

repaint();

public void mousePressed(MouseEvent me) {

mouseX = me.getX();

mouseY = me.getY();

msg = "Pressed";

repaint();

public void mouseReleased(MouseEvent me) {

mouseX = me.getX();

mouseY = me.getY();

msg = "Released";

repaint();

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

public void mouseDragged(MouseEvent me) {

mouseX = me.getX();

mouseY = me.getY();

msg = "Dragging";

repaint();

public void mouseMoved(MouseEvent me) {

repaint(0, 0, 100, 20);

public void keyPressed(KeyEvent ke) {

msg="Key Pressed.";

repaint();

public void keyReleased(KeyEvent ke) {

msg="Key Released";

repaint();

public void keyTyped(K yEvent ke) {

msg += ke.getKeyChar();

repaint();

public void paint(Graphics g) {

g.drawString(msg, mouseX, mouseY);

Id. No: 19IT419


3IT03: Object Oriented Programming with JAVA

Output:

Id. No: 19IT419

You might also like