19IT419 3IT03 LabManual
19IT419 3IT03 LabManual
19IT419 3IT03 LabManual
of
Object Oriented Programming with Java
BACHELOR OF ENGINEERING
In
INFORMATION TECHNOLOGY
CERTIFICATE
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.
class Calc
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:
import java.util.Scanner;
class Palindrome
int n,temp,sum=0;
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
Output:
import java.util.Scanner;
class Prime
int a,b,n,flag=0;
n = sc.nextInt();
a = sc.nextInt();
b = sc.nextInt();
if(a>b)
a=a+b;
b=a-b;
a=a-b;
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)
else
else
Output:
import java.util.Scanner;
class MatMul
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
a[i][j]=sc.nextInt();
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
b[i][j]=sc.nextInt();
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();
for(int i=0;i<3;i++)
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();
Output:
import java.util.Scanner;
class SumofDigit
int n,sum=0,temp;
n = sc.nextInt();
temp=n;
while(n>0)
sum=sum+n%10;
n/=10;
Output:
import java.util.Scanner;
class Armstrong
int n,sum=0,temp;
n = sc.nextInt();
temp=n;
while(n>0)
int r = n%10;
sum=sum+(r*r*r);
n/=10;
if(temp==sum)
else
Output:
import java.util.Scanner;
class Stack
int size,top;
int[] stack;
Stack(int n)
size = n;
top = -1;
if(top == stack.length - 1)
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("");
class DynamicStack
int size,choice=0;
size = sc.nextInt();
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");
choice = sc.nextInt();
if(choice == 1)
int 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");
Output:
import java.util.Scanner;
class A
int a,b,c;
A(int x,int y)
a=x;
b=y;
c=0;
a=x;
b=y;
c=z;
void display(int x)
class MethodOverloading
ob1.display(5);
ob1.display(5,10);
Output:
import java.util.Random;
class IntToString
if(n==1)
return "st";
else if(n==2)
return "nd";
else if(n==3)
return "rd";
else
return "th";
String[] month = {"January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"};
return month[n-1];
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);
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;
System.out.println(s+"\n"+ans+"\n\n");
for(int i=0;i<10;i++)
getValue();
Output:
Create the class by creating some objects and testing the class operations.
class Mcm{
int m,cm,mm;
m=d_m;
cm=d_cm;
mm=d_mm;
if(op==1)
m+=x;
cm+=x;
mm+=x;
if(op==2){ m-=x;
cm-=x;
mm-=x;
if(op==1)
(this.m+this.cm/100+this.mm/1000)*(obj.m+obj.cm/100+obj.mm/1000);
if(obj1.m>obj2.m){ System.out.println(s1);
else{
if(obj1.cm>obj2.cm){
System.out.println(s1);
else if(obj2.cm>obj2.cm)
System.out.println(s2);
if(obj1.mm>obj2.mm){
System.out.println(s1);
else{ System.out.println(s);
void displayAll(){
m1.addSub(10, 1);
m2.addSub(2, 2);
m1.displayAll();
Output:
import java.util.*;
class Factorial
if(n<1)
return 1;
return n*fac(n-1);
int n;
n=sc.nextInt();
System.out.println(fac(n));
Output:
import java.util.Scanner;
class GDC
if(a%b==0)
return b;
return gcd(b,a%b);
int a,b;
a=sc.nextInt();
b=sc.nextInt();
if(b<a)
a=a+b;
b=a-b;
a=a-b;
System.out.print("Ans = "+gcd(a,b));
Output:
import java.util.Scanner;
class MagicMatrix
int n;
n = sc.nextInt();
int column[],row[],flag=0,dia1=0,dia2=0,sum=n*((n*n)+1)/2;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
a[i][j] = sc.nextInt();
if(flag==0)
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");
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)
else
Output:
class Base
int n=2;
Base()
Child()
void display()
System.out.println("Value of n = "+n);
class Inheritance
ob.display();
Output:
Abstract class A
final int n = 1;
A()
System.out.println(“It’s A Constructer”);
void display()
B()
System.out.println(“It’s B Constructer”);
void display()
super.display();
//n=2;
/*
class C extends B
C()
System.out.println(“It’s C Constructer”);
}*/
class FinalAbs
A ob2;
ob2.display();
Output:
class Complex
int x1=3;
int y1=6;
int x2=-3;
int y2=2;
Output:
import java.util.Scanner;
class Node
int data;
Node next;
Node()
data = 0;
next = null;
Node(int d_data)
data = d_data;
next = null;
while(temp.next!=null)
temp=temp.next;
temp.next = obj;
void display()
System.out.print("Value : ");
while(temp.next!=null)
System.out.print(temp.data+", ");
temp=temp.next;
System.out.println(temp.data);
int k=1;
while(temp.data!=d_data)
if(temp.next==null)
return -1;
k++;
temp = temp.next;
return k;
class SLL
int choice=0,i=0;
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");
choice = sc.nextInt();
if(choice == 1)
if(i>1)
ob[0].append(ob[i-1]);
else if(choice == 2)
if(pos>i||pos<=0)
System.out.println("Invalid Position");
else
for(int j=i++;j>=pos;j--)
ob[j]=ob[j-1];
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)
else
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];
else if(choice == 4)
ob[0].display();
else if(choice == 5)
if(pos!=-1)
else
else if(choice == 6)
break;
else
System.out.println("Invalid Input");
Output:
import java.util.Scanner;
class Node
int data;
Node next;
Node()
data = 0;
next = this;
Node(int d_data)
data = d_data;
next = this;
while(temp.next!=this)
temp=temp.next;
temp.next = obj;
obj.next = this;
void display()
System.out.print("Value : ");
while(temp.next!=this)
System.out.print(temp.data+", ");
temp=temp.next;
System.out.println(temp.data);
int k=1;
while(temp.data!=d_data)
if(temp.next==this)
return -1;
k++;
temp = temp.next;
return k;
class CSLL
int choice=0,i=0;
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");
System.out.println("6 Exit");
choice = sc.nextInt();
if(choice == 1)
if(i>1)
ob[0].append(ob[i-1]);
else if(choice == 2)
if(pos>i||pos<=0)
System.out.println("Invalid Position");
else
for(int j=i++;j>=pos;j--)
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)
else
System.out.println("I = "+i);
i--;
for(int j=pos-1;j<i;j++)
ob[j] = ob[j+1];
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)
if(pos!=-1)
else
else if(choice == 6)
break;
else
System.out.println("Invalid Input");
Output:
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;
while(temp.next!=null)
temp=temp.next;
obj.prev = temp;
temp.next = obj;
void display()
System.out.print("Value : ");
while(temp.next!=null)
System.out.print(temp.data+", ");
temp=temp.next;
System.out.println(temp.data);
int k=1;
while(temp.data!=d_data)
if(temp.next==null)
return -1;
k++;
temp = temp.next;
return k;
class DLL
int choice=0,i=0;
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");
choice = sc.nextInt();
if(choice == 1)
if(i>1)
ob[0].append(ob[i-1]);
else if(choice == 2)
if(pos>i||pos<=0)
System.out.println("Invalid Position");
else
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)
else
i--;
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)
if(pos!=-1)
else
else if(choice == 6)
break;
else
System.out.println("Invalid Input");
Output:
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;
while(temp.next!=this)
temp=temp.next;
obj.prev = temp;
temp.next = obj;
obj.next = this;
this.prev = obj;
void display()
System.out.print("Value : ");
while(temp.next!=this)
System.out.print(temp.data+", ");
temp=temp.next;
System.out.println(temp.data);
int k=1;
while(temp.data!=d_data)
if(temp.next==this)
return -1;
k++;
temp = temp.next;
return k;
class CDLL
int choice=0,i=0;
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");
choice = sc.nextInt();
if(choice == 1)
if(i>1)
ob[0].append(ob[i-1]);
else if(choice == 2)
if(pos>i||pos<=0)
System.out.println("Invalid Position");
else
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)
else
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)
if(pos!=-1)
else
else if(choice == 6)
break;
else
System.out.println("Invalid Input");
Output:
int protectedVar=3;
// import p1.A;
class Pac2{
Output:
import java.util.*;
return "Error..!!";
class College
int age;
double cpi;
String name;
String dept;
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();
try
catch(Exception e)
System.out.println(e.getMessage());
else
System.out.println("Finish");
Output:
import java.util.Scanner;
int a,b;
PaliThreadEx(int a,int b)
super("Palindrome");
this.a = a;
this.b = b;
start();
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)
Thread.sleep(500);
catch(InterruptedException e)
System.out.println("Error : "+e);
int a,b;
Thread ob;
PaliThreadIn(int a,int b)
ob = new Thread(this,"Palindrome");
this.a = a;
this.b = b;
ob.start();
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)
Thread.sleep(500);
catch(InterruptedException e)
System.out.println("Error : "+e);
class ThreadByTwoMethod
int a,b;
a = sc.nextInt();
b = sc.nextInt();
try
for(int i=a;i<=b;i++)
if(i%5==0)
System.out.println("Divisible : "+i);
Thread.sleep(500);
catch(InterruptedException e)
System.out.println("Error : "+e);
Output:
class Work
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();
if(!flag)
try
wait();
catch(InterruptedException Ex)
System.out.println("Error : "+Ex);
flag = false;
System.out.println("Get : "+n);
notify();
Work ob;
Producer(Work tmp)
ob = tmp;
t.start();
int i=0;
while(true)
ob.put(++i);
Work ob;
Consumer(Work tmp)
ob = tmp;
t.start();
while(true)
ob.get();
class ProdCon
Output:
import java.io.*;
import java.util.Scanner;
class ByteFile
try
String s;
s=sc.nextLine();
while(true)
String m;
m = sc.nextLine()+"\n";
if(m.equals("stop\n"))
break;
byte[] b = m.getBytes();
fo.write(b);
fo.close();
System.out.println("File Saved");
catch(Exception Ex)
System.out.println("Error:"+Ex);
Output:
import java.util.Scanner;
import java.io.*;
class CopyFile
try
String old,fresh;
old = sc.nextLine();
fresh = sc.nextLine();
int n;
while((n=fi.read())!=-1)
fo.write(n);
fo.close();
fi.close();
System.out.println("File Copyed");
catch(Exception Ex)
System.out.println("Error : "+Ex);
Output:
import java.awt.*;
import java.applet.*;
/*
</applet>
*/
Thread t=null;
int state;
boolean flag;
setBackground(Color.yellow);
setForeground(Color.red);
t=new Thread(this);
flag=false;
t.start();
char ch;
for( ; ; )
try
repaint();
Thread.sleep(1000);
ch = msg.charAt(0);
msg += ch;
if(flag)
break;
} catch(InterruptedException e) {}
flag=true;
t=null;
Output:
/*
</applet>
*/
setBackground(Color.black); setForeground(Color.white);
try
catch(InterruptedException Ex){}
g.drawString(msg,100,30);
Output:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
MouseMotionListener,KeyListener{
SampleFrame(String title) {
super(title);
addMouseListener(this);
addMouseMotionListener(this);
addKeyListener(this);
requestFocus();
addWindowListener(adapter);
msg = "Clicked";
repaint();
repaint();
repaint();
mouseX = me.getX();
mouseY = me.getY();
msg = "Pressed";
repaint();
mouseX = me.getX();
mouseY = me.getY();
msg = "Released";
repaint();
mouseX = me.getX();
mouseY = me.getY();
msg = "Dragging";
repaint();
msg="Key Pressed";
repaint();
msg="Key Released";
repaint();
msg += ke.getKeyChar();
repaint();
SampleFrame sampleFrame;
this.sampleFrame = sampleFrame;
sampleFrame.setVisible(false);
MouseListener,MouseMotionListener,KeyListener{
SampleFrame f;
f.setSize(500, 300);
f.setVisible(true);
addMouseListener(this);
addMouseMotionListener(this);
addKeyListener(this);
requestFocus();
f.setVisible(false);
f.setVisible(true);
msg="Clicked";
repaint();
repaint();
repaint();
mouseX = me.getX();
mouseY = me.getY();
msg = "Pressed";
repaint();
mouseX = me.getX();
mouseY = me.getY();
msg = "Released";
repaint();
mouseX = me.getX();
mouseY = me.getY();
msg = "Dragging";
repaint();
msg="Key Pressed.";
repaint();
msg="Key Released";
repaint();
msg += ke.getKeyChar();
repaint();
Output: