SUM OF DIGITS
import java.io.*;
class Sumofdigits
int n,s=0,a,d=0;
System.out.println("Enter a number");
String str=br.readLine();
n=Integer.parseInt(str);
while(n>0)
d=n%10;
s=s+d;
n=n/10;
OUTPUT
1|Page
Dept Of MCA,RIT KOTTAYAM
REVERSE OF A NUMBER
import java.io.*;
class Reverse
int n,s=0,d=0,rev=0;
System.out.println("Enter a number");
String str=br.readLine();
n=Integer.parseInt(str);
while(n>0)
d=n%10;
rev=rev*10+d;
n=n/10;
}}
OUTPUT
2|Page
Dept Of MCA,RIT KOTTAYAM
EVEN OR ODD
import java.io.*;
class Oddoreven
int n;
System.out.println("Enter a number");
String str=br.readLine();
n=Integer.parseInt(str);
if(n%2==0)
System.out.println("Number is even");
else
System.out.println("Number is odd");
OUTPUT
Enter a number :5
Number is odd
3|Page
Dept Of MCA,RIT KOTTAYAM
import java.io.*;
class Prime
int a,b,i,j,c=0;
System.out.println("Enter starting");
String str1=br.readLine();
a=Integer.parseInt(str1);
System.out.println("Enter ending");
String str2=br.readLine();
b=Integer.parseInt(str2);
System.out.println("Prime numbers");
for(i=a;i<=b;i++)
for(j=2;j<=i/2;j++)
if(i%j==0)
c=1;
break;
4|Page
Dept Of MCA,RIT KOTTAYAM
OUTPUT
Enter starting: 5
Enter ending: 15
Prime numbers
11
13
5|Page
Dept Of MCA,RIT KOTTAYAM
MENU OPERATIONS
import java.io.*;
class Menu
int opt=0,n,rev=0,s=0,d=0,a,b,i,j,c=0;
System.out.println("-------MENU-------");
System.out.println("1.Sum of digits");
System.out.println("2.Reverse of a number");
System.out.println("4.Prime numbers");
System.out.println("5.Exit");
while(opt!=5)
String str1=br.readLine();
opt=Integer.parseInt(str1);
switch(opt)
6|Page
Dept Of MCA,RIT KOTTAYAM
String str2=br.readLine();
n=Integer.parseInt(str2);
while(n>0)
d=n%10;
s=s+d;
n=n/10;
break;
String str3=br.readLine();
n=Integer.parseInt(str3);
while(n>0)
d=n%10;
rev=rev*10+d;
n=n/10;
break;
String str4=br.readLine();
n=Integer.parseInt(str4);
7|Page
Dept Of MCA,RIT KOTTAYAM
if(n%2==0)
System.out.println("Number is even");
else
System.out.println("Number is odd");
break;
String str5=br.readLine();
a=Integer.parseInt(str5);
System.out.println("Enter ending");
String str6=br.readLine();
b=Integer.parseInt(str6);
System.out.println("Prime numbers");
for(i=a;i<=b;i++)
for(j=2;j<=i/2;j++)
if(i%j==0)
c=1;
break;
if((c==0)&&(i!=1))
System.out.println(i);
8|Page
Dept Of MCA,RIT KOTTAYAM
c=0;
break;
case 5:break;
default:System.out.println("Invalid option");
continue;
OUTPUT
-------MENU--------
1.Sum of digits
2.Reverse of a number
4.Prime numbers
5.Exit
Enter a number:586
9|Page
Dept Of MCA,RIT KOTTAYAM
import java.io.*;
class Fibonacci
int n,f1=0,f2=1,f3=0;
String str=br.readLine();
n=Integer.parseInt(str);
while(f3<=n)
System.out.println(f3);
f1=f2;
f2=f3;
f3=f1+f2;
}}}
OUTPUT
0,1,1,2,3,5
10 | P a g e
Dept Of MCA,RIT KOTTAYAM
import java.io.*;
class Factorial
int n,i;
long f=1;
System.out.println("Enter a number");
String str=br.readLine();
n=Integer.parseInt(str);
for(i=1;i<=n;i++)
f=f*i;
OUTPUT
Enter a number :5
Factorial is :120
11 | P a g e
Dept Of MCA,RIT KOTTAYAM
Aim: Write a program to input a decimal number and convert it into binary number.
import java.io.*;
import java.lang.Math;
class DToB{
int deci,i=0,n;
double bin=0;
long a;
String str=br.readLine();
deci=Integer.parseInt(str);
while(deci>0)
n=deci%2;
bin=bin+(n*(Math.pow(10,i)));
deci=deci/2; i++; }
a=(long)bin; }
OUTPUT
12 | P a g e
Dept Of MCA,RIT KOTTAYAM
Aim: Write a program to input a binary number and convert it into decimal number
import java.io.*;
import java.lang.Math;
class BToD
int i=0,n,bin,a;
double deci=0;
String str=br.readLine();
bin=Integer.parseInt(str);
while(bin>0)
n=bin%10;
deci=deci+(n*(Math.pow(2,i)));
bin=bin/10;
i++;
a=(int)deci;
13 | P a g e
Dept Of MCA,RIT KOTTAYAM
String str=br.readLine();
n=Integer.parseInt(str);
while(f3<=n)
f1=f2;
f2=f3;
f3=f1+f2;
if(f3==n)
c=1;
break;
OUTPUT
14 | P a g e
Dept Of MCA,RIT KOTTAYAM
FIBONACCI OR NOT
Aim: Write a program to check whether the given number is Fibonacci number or not.
import java.io.*;
class FibCheck
int n,f1=0,f2=1,f3=0,c=0;
String str=br.readLine();
n=Integer.parseInt(str);
while(f3<=n)
f1=f2;
f2=f3;
f3=f1+f2;
if(f3==n)
c=1;
break;
if(c==1)
15 | P a g e
Dept Of MCA,RIT KOTTAYAM
else
OUTPUT
16 | P a g e
Dept Of MCA,RIT KOTTAYAM
import java.io.*;
class PrimeFactor
int i,j,k=0,c=0,num;
System.out.println("Enter a number");
String str=br.readLine();
num=Integer.parseInt(str);
for(i=1;i<=num;i++)
if(num%i==0)
a[k]=i;
++k;
17 | P a g e
Dept Of MCA,RIT KOTTAYAM
for(i=0;i<k;i++)
for(j=2;j<=a[i]/2;j++)
if(a[i]%j==0)
c=1;
break;
if((c==0)&&(a[i]!=1))
System.out.println(a[i]);
c=0;
OUTPUT
18 | P a g e
Dept Of MCA,RIT KOTTAYAM
OPERATIONS ON MATRIX
import java.io.*;
class Matrix
int i,j,m,n,p,q,k,x;
String str1=br.readLine();
m=Integer.parseInt(str1);
String str2=br.readLine();
n=Integer.parseInt(str2);
for(i=0;i<m;i++)
19 | P a g e
Dept Of MCA,RIT KOTTAYAM
for(j=0;j<n;j++)
String str3=br.readLine();
a[i][j]=Integer.parseInt(str3);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
String str4=br.readLine();
b[i][j]=Integer.parseInt(str4);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
for(i=0;i<m;i++)
20 | P a g e
Dept Of MCA,RIT KOTTAYAM
for(j=0;j<n;j++)
System.out.print(c[i][j]+" ");
System.out.println();
String str5=br.readLine();
m=Integer.parseInt(str5);
String str6=br.readLine();
n=Integer.parseInt(str6);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
String str7=br.readLine();
a[i][j]=Integer.parseInt(str7);
21 | P a g e
Dept Of MCA,RIT KOTTAYAM
String str8=br.readLine();
p=Integer.parseInt(str8);
String str9=br.readLine();
q=Integer.parseInt(str9);
if(n==p)
for(i=0;i<p;i++)
for(j=0;j<q;j++)
String str10=br.readLine();
b[i][j]=Integer.parseInt(str10);
for(i=0;i<n;i++)
for(j=0;j<p;j++)
c[i][j]=0;
for(k=0;k<p;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
22 | P a g e
Dept Of MCA,RIT KOTTAYAM
else
System.out.println("size mismatch");
for(i=0;i<m;i++)
for(j=0;j<q;j++)
System.out.print(c[i][j]+" ");
System.out.println();
String str11=br.readLine();
m=Integer.parseInt(str11);
String str12=br.readLine();
23 | P a g e
Dept Of MCA,RIT KOTTAYAM
n=Integer.parseInt(str12);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
String str13=br.readLine();
a[i][j]=Integer.parseInt(str13);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
System.out.print(a[j][i]+" ");
System.out.println();
24 | P a g e
Dept Of MCA,RIT KOTTAYAM
class MatrixOperations
int opt=0;
System.out.println("-------MENU--------");
System.out.println("MATRIX OPERATIONS");
System.out.println("1.Addition");
System.out.println("2.Multiplication");
System.out.println("3.Transpose");
System.out.println("4.Exit");
while(opt!=4)
String str14=br.readLine();
opt=Integer.parseInt(str14);
switch(opt)
case 1:m.add();
break;
case 2:m.mul();
break;
case 3:m.transpose();
25 | P a g e
Dept Of MCA,RIT KOTTAYAM
break;
case 4:break;
default:System.out.println("Invalid option");
continue;}}}}
OUTPUT
-------MENU--------
MATRIX OPERATIONS
1.Addition
2.Multiplication
3.Transpose
4.Exit
0 3 0
0 0 0
1 2 0
0 0 1
3 0 2
0 0 0
26 | P a g e
Dept Of MCA,RIT KOTTAYAM
BANK ACCOUNT
Aim: a.Design a class Account to represent a bank account with following data members
(acc_no,depositor name,acc_type,acc_balance)and methods(to assign initial values,to deposit an
amunt,to withdraw amount(minimum balance should be maintained),to display name and balance etc.
b.Also try the above program by incorporating constructors to provide initial values.
import java.io.*;
class Account
int acc_no;
double acc_balance,m,n,bal;
String acc_type,depositor_name;
String str1=br1.readLine();
acc_no=Integer.parseInt(str1);
depositor_name=br1.readLine();
27 | P a g e
Dept Of MCA,RIT KOTTAYAM
acc_type=br1.readLine();
String str2=br1.readLine();
acc_balance=Double.parseDouble(str2);
String str3=br.readLine();
m=Double.parseDouble(str3);
acc_balance=acc_balance+m; }
String str4=br.readLine();
n=Double.parseDouble(str4);
bal=acc_balance-n;
if(bal<500)
else
acc_balance=acc_balance-n;
28 | P a g e
Dept Of MCA,RIT KOTTAYAM
void putData(){
System.out.println("Account number:"+acc_no);
System.out.println("Depositor name:"+depositor_name);
System.out.println("Account type:"+acc_type);
System.out.println("Account balance:"+acc_balance);}}
class Bank1
int opt=0;
ac.getData();
System.out.println("-----TRANSACTIONS-----");
System.out.println("1.Deposit");
System.out.println("2.Withdrawal");
System.out.println("3.Display details");
System.out.println("4.Exit");
while(opt!=4)
String str5=br.readLine();
opt=Integer.parseInt(str5);
switch(opt)
29 | P a g e
Dept Of MCA,RIT KOTTAYAM
case 4:break;
default:System.out.println("Invalid option");
break; }}}}
OUTPUT
-----TRANSACTIONS-----
1.Deposit
2.Withdrawal
3.Display details
4.Exit
Account number:123
Depositor:MINU
Account type:sb
Account balance:6000.0
30 | P a g e
Dept Of MCA,RIT KOTTAYAM
METHOD OVERLOADING
Aim: Write a java program that uses an overloaded method volume() that returns volume of
different structures. The first version that takes one float side of a cube and returns it’s volume.
The second version takes float radius and float height,and returns the volume of a cylinder .The
third version takes float length,float height and float width of a rectangular box and returns the
volume.
import java.io.*;
class Overload
float volume(double a)
return(a*a*a);
return(3.14*r*r*h);
return(h*w*l);
class MethodOverload
31 | P a g e
Dept Of MCA,RIT KOTTAYAM
float a,l,h,r,w,h1;
a=Float.parseFloat(br.readLine());
r=Float.parseFloat(br.readLine());
h=Float.parseFloat(br.readLine());
l=Float.parseFloat(br.readLine());
h1=Float.parseFloat(br.readLine());
w=Float.parseFloat(br.readLine());
System.out.println("Volume of cube="+ob.volume(a));
System.out.println("Volume of cylinder="+ob.volume(r,h));
32 | P a g e
Dept Of MCA,RIT KOTTAYAM
OUTPUT
Volume of cube:27.0
Volume of a rectangle: 60
33 | P a g e
Dept Of MCA,RIT KOTTAYAM
Aim: write a java program to demonstrate the use of ‘this’ and ‘super’.
import java.io.*;
class Point
int x,y;
Point(int x,int y)
this.x=x;
this.y=y;
int z;
super(x,y);
this.z=z;
class ThisSuper
34 | P a g e
Dept Of MCA,RIT KOTTAYAM
System.out.println("X="+p.x);
System.out.println("Y="+p.y);
System.out.println("Z="+p.z);
OUTPUT
X=10
Y=20
Z=30
35 | P a g e
Dept Of MCA,RIT KOTTAYAM
import java.io.*;
class A
String name;
A()
name="Jayalakshmi";
void show()
System.out.println("Name:"+name);
class B extends A
String job;
B()
job="Software Engineer";
36 | P a g e
Dept Of MCA,RIT KOTTAYAM
void show()
System.out.println("Job:"+job);
class C extends B
double salary;
C()
salary=25000;
void show()
System.out.println("Salary:"+salary);
class Dynamic
A a=new A();
B b=new B();
C c=new C();
System.out.println("Employ details");
37 | P a g e
Dept Of MCA,RIT KOTTAYAM
A ref;
ref=a;
ref.show();
ref=b;
ref.show();
ref=c;
ref.show();
OUTPUT
Employee Details
Name: Jayalakshmi
Salary: 25000
38 | P a g e
Dept Of MCA,RIT KOTTAYAM
ABSTRACT CLASS
Aim: Create an abstract class named Shape with methods calcArea() and calcPeri(). Extend this
abstract class in subclasses Rectangle,Circle and Triangle. Create a java program using
Rectangle,Circle and Triangle class
import java.io.*;
import java.lang.*;
l=Double.parseDouble(br.readLine());
b=Double.parseDouble(br.readLine());
void calcArea()
System.out.println("Area of rectangle="+(l*b));
39 | P a g e
Dept Of MCA,RIT KOTTAYAM
void calcPeri()
System.out.println("Perimeter of rectangle="+(2*(l+b)));
public double r;
r=Double.parseDouble(br.readLine());
void calcArea()
System.out.println("Area of circle="+(3.14*r*r));
void calcPeri()
System.out.println("Perimeter of circle="+(2*3.14*r));
40 | P a g e
Dept Of MCA,RIT KOTTAYAM
bs=Double.parseDouble(br.readLine());
h=Double.parseDouble(br.readLine());
hp=Double.parseDouble(br.readLine());
void calcArea()
double s,result,s1;
s=(bs+h+hp)/2;
s1=s*(s-bs)*(s-h)*(s-hp);
result=Math.pow(s1,1/2);
System.out.println("Area of triangle="+result);
void calcPeri()
System.out.println("Perimeter of triangle="+(bs+h+hp));
41 | P a g e
Dept Of MCA,RIT KOTTAYAM
class AbstractClass
Shape s;
s=re;
re.getData1();
s.calcArea();
s.calcPeri();
s=c;
c.getData2();
s.calcArea();
s.calcPeri();
s=t;
t.getData3();
s.calcArea();
s.calcPeri();
42 | P a g e
Dept Of MCA,RIT KOTTAYAM
OUTPUT
Area of rectangle: 20
Perimeter of rectangle:=18
Perimeter of circle:=37.68
Area of triangle=25
43 | P a g e
Dept Of MCA,RIT KOTTAYAM
Aim: Design and implement a n example where multiple inheritance can be incorporated using
interface
import java.io.*;
interface Bubble
{
public void bubble_sort(int p[],int n);
}
interface Insertion
{
public void insertion_sort(int q[],int m);
}
class Sort implements Bubble,Insertion
{
inti,j,temp,n;
public void disp(int x[],int n)
{
for(i=0;i<n;i++)
{
System.out.println(x[i]);
}
}
public void bubble_sort(int a[],int n)
{
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
public void insertion_sort(int a[],int n)
{
for(i=0;i<n;i++)
{
for(j=i;j<n-i;j++)
{
if(a[i]>a[j])
{
44 | P a g e
Dept Of MCA,RIT KOTTAYAM
temp=a[i];
a[i]=a[j];
a[j]=temp;}}}
}
}
classMultInterface
{
public static void main(String args[])throws IOException
{
int a[]=new int[10];
int b[]=new int[10];
inti,j,n;
OUTPUT
45 | P a g e
Dept Of MCA,RIT KOTTAYAM
ILLUSTRATION OF PACKAGES
Aim: -Define a package “ARITHOP” and include classes Addition, Subtraction, Multiplication, and
Division.
Design these classes accordingly now import the package in another program to test for arithmetic
Operations?
packagearithop
import java.io.*;
public class Addition
{
public void add(inta,int b)
{
int c;
c=a+b;
System.out.println(c);
}
}
packagearithop;
public class Subtraction
{
public void sub(inta,int b)
{
int c;
c=a-b;
System.out.println(c);
}
}
packagearithop;
public class Multiplication
{
public void mul(inta,int b)
{
int c;
c=a*b;
System.out.println(c);
}
}
packagearithop;
public class Division
{
public void div(inta,int b)
46 | P a g e
Dept Of MCA,RIT KOTTAYAM
{
int c;
c=a/b;
System.out.println(c);
}
}
import java.io.*;
importarithop.*;
importarithop.Subtraction;
class Package
{
public static void main(String args[])throws IOException
{
inta,b,ch;
Addition ad=new Addition();
Subtraction su=new Subtraction();
Multiplication mu=new Multiplication();
Division di=new Division();
BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the numbers:");
a=Integer.parseInt(br.readLine());
b=Integer.parseInt(br.readLine());
do
{
System.out.println("1.Addition \n2.Subtraction \n3.Multiplication
\n4.Division\n5.exit\n");
System.out.print("Enter the choice:");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println("Sum of the numbers");
ad.add(a,b);
break;
case 2:
System.out.println("Difference of the numbers");
su.sub(a,b);
break;
case 3:
System.out.println("Product of the numbers");
mu.mul(a,b);
break;
case 4:
System.out.println("Quotient ");
di.div(a,b);
break;
case 5:
break;
47 | P a g e
Dept Of MCA,RIT KOTTAYAM
}
}while(ch!=5);
}
}
OUTPUT
48 | P a g e
Dept Of MCA,RIT KOTTAYAM
Aim: -Illustrate the methods available with string and string buffer class?
import java.io.*;
class se{
se(inti)
String word;
//length method:
word = word.trim();
49 | P a g e
Dept Of MCA,RIT KOTTAYAM
String str;
try{
sest=new se(1);
str = in.readLine();
str += ", This is the illustration of SringBuffer class and it's functions. BY SWAT";
System.out.print(strbuf.length());
strbuf.append(str);
System.out.println(strbuf);
strbuf.delete(0,str.length());
//append()
strbuf.append("Hello");
strbuf.append("World");
System.out.println(strbuf);
//insert()
strbuf.insert(5,"_Java ");
System.out.println(strbuf);
//reverse()
strbuf.reverse();
System.out.println(strbuf);
50 | P a g e
Dept Of MCA,RIT KOTTAYAM
strbuf.reverse();
System.out.println(strbuf);
//setCharAt()
strbuf.setCharAt(5,' ');
System.out.println(strbuf);
//charAt()
System.out.println(strbuf.charAt(6));
//substring()
System.out.println(strbuf.substring(3,7));
//deleteCharAt()
strbuf.deleteCharAt(3);
System.out.println(strbuf);
//capacity()
System.out.println(strbuf.capacity());
strbuf.delete(6,strbuf.length());
System.out.println(strbuf);
catch(StringIndexOutOfBoundsException e)
System.out.println(e.getMessage());
//se.fn();
51 | P a g e
Dept Of MCA,RIT KOTTAYAM
OUTPUT
Length: 30
indexOf('s'): 28
first character: P
AT
HelloWorld
Hello_Java World
Hello_Java World
Capacity of StringBufferobject : 81
Helo J
52 | P a g e
Dept Of MCA,RIT KOTTAYAM
importjava.awt.*;
importjava.applet.*;
//import java.graphics.*;
/*<applet code="Face" height=300 width=300>
</applet>
*/
53 | P a g e
Dept Of MCA,RIT KOTTAYAM
OUTPUT
54 | P a g e
Dept Of MCA,RIT KOTTAYAM
METHODS IN APPLET
importjava.awt.*;
importjava.applet.*;
import java.net.*;
/*<applet code="ApMethod" width=300 height=300>
<param name=Name value=Afja>
</applet>*/
public class ApMethod extends Applet
{
String Name;
public void init()
{
setBackground(Color.black);
setForeground(Color.white)
}
public void start()
{
Name=getParameter("Name");
}
public void paint(Graphics g)
{
String abc;
URL url=getCodeBase();
abc="codebase"+url.toString();
g.drawString(abc,50,150);
url=getDocumentBase();
abc="documentbase"+url.toString();
g.drawString(abc,70,180);
g.drawString("Name is"+Name,100,200);
g.drawString("Haiii, this is applet window",50,100);
showStatus("status window");
}
}
55 | P a g e
Dept Of MCA,RIT KOTTAYAM
OUTPUT
56 | P a g e
Dept Of MCA,RIT KOTTAYAM
Aim: Applet to display the name and address of your college.UsesetBackground(),SetFont() and
setForeground() methods to set the font of text,colorof text and color of background.Display an image
also.
importjava.awt.*;
importjava.applet.*;
/*<applet code="ApCollege" height=400 width=600><param name="image" value="rit.jpg">
</applet>*/
public class ApCollege extends Applet
{
Image img;
public void init()
{
img=getImage(getDocumentBase(),getParameter("image"));
}
public void paint(Graphics g)
{
Font font = new Font("Times New Roman",Font.BOLD,16);
setBackground(Color.white);
setForeground(Color.black);
g.setFont(font);
g.drawString("Rajiv Gandhi Institute of Technology",10,35);
g.drawString("Govt.EnggCollege,Velloor P.O,Kottayam.",10,50);
g.drawImage(img,100,80,this); }
}
57 | P a g e
Dept Of MCA,RIT KOTTAYAM
OUTPUT
58 | P a g e
Dept Of MCA,RIT KOTTAYAM
EXCEPTION HANDLING
Aim: write a java program to generate for permanent employees in an organization.use exception
handling to handle errors that arise during salary calculation.
import java.io.*;
class salary
{
floatbp,ta,da,hr,pf,intrst;
void read() throws IOException
{
try
{
BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the basic pay");
bp=Float.parseFloat(br.readLine());
System.out.println("Enter the travel allowance");
ta=Float.parseFloat(br.readLine());
System.out.println("Enter the daily allowance");
da=Float.parseFloat(br.readLine());
System.out.println("Enter the house rent allowance");
hr=Float.parseFloat(br.readLine());
System.out.println("Enter the PF");
pf=Float.parseFloat(br.readLine());
System.out.println("Enter the loan interest");
intrst=Float.parseFloat(br.readLine());
}
catch(IOException e)
{}}
voidshowdata()
{
System.out.print("Basic pay:Rs."+bp+"\n");
System.out.print("Travel allowance:Rs."+ta+"\n");
System.out.print("Daily allowance:Rs."+da+"\n");
System.out.print("House rent allowance:Rs."+hr+"\n"); }}
classpayslip extends salary
{
doubletotal,ded,alwnc;
voidcalc()
{
alwnc=ta+da+hr;
ded=pf+intrst;
total=bp+alwnc-ded;}
59 | P a g e
Dept Of MCA,RIT KOTTAYAM
void show()
{
try
{
if(total<0)
throw new ArithmeticException("Negative salary!!!\n");
else
System.out.println("Total salary:Rs."+total+"\n");
}
catch(ArithmeticException e)
{
System.out.println("------------------------");
System.out.println(e.getMessage());
}}}
classmainslip extends payslip
{
public static void main(String args[]) throws IOException
{
payslip p=new payslip();
p.read();
p.calc();
System.out.println("\n\n Salary Details\n--------------------");
p.showdata();
p.show();}}
OUTPUT
Salary Details
--------------------
Basic pay:Rs.150.0
Travel allowance:Rs.20.0
Daily allowance:Rs.30.0
House rent allowance:Rs.20.0
------------------------
Negative salary!!!
60 | P a g e
Dept Of MCA,RIT KOTTAYAM
MULTITHREADING IN JAVA
Aim: write a java program to create three threads;AThread,BThread and CThread .Start these threads
sequentially in the same order but the output should display the message in BThread first.
import java.io.*;
importjava.lang.*;
class A extends Thread
{
public void run()
{
try
{
Thread.sleep(200);
System.out.println("AThread started!");
System.out.println("Exit from A");
}
catch(InterruptedException e)
{
System.out.println("Interrupted!");
}
}
}
61 | P a g e
Dept Of MCA,RIT KOTTAYAM
Thread.sleep(300);
System.out.println("CThread started!");
System.out.println("Exit from C");
}
catch(InterruptedException e)
{
System.out.println("Interrupted!");
}
}
}
class Threads
{
public static void main(String args[]) throws IOException
{
A AThread=new A();
B BThread=new B();
C CThread=new C();
BThread.setPriority(Thread.MAX_PRIORITY);
AThread.setPriority(Thread.NORM_PRIORITY);
CThread.setPriority(Thread.MIN_PRIORITY);
AThread.start();
BThread.start();
CThread.start();
}
}
OUTPUT
BThread started!
Exit from B
AThread started!
Exit from A
CThread started!
Exit from C
62 | P a g e
Dept Of MCA,RIT KOTTAYAM
FILE OPERATIONS
import java.io.*;
class Files
{
}
catch(FileNotFoundException e)
{
System.out.println(e.getMessage());
return;
}
try
{
fout=new FileOutputStream(args[1]);
}
catch(FileNotFoundException e)
{
System.out.println(e.getMessage());
return;
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("copy file from To");
return;
}
try
{
do
{
i=fin.read();
if(i!=-1)
fout.write(i);
63 | P a g e
Dept Of MCA,RIT KOTTAYAM
}while(i!=-1);
}
catch(IOException e)
{
System.out.println(e.getMessage());
return;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e.getMessage());
return;
}
fin.close();
fout.close();
}
}
OUTPUT
File1: Niya.txt
Content:Fromniya
File2:Afja.txt
Content:
After execution:
File1: Niya.txt
Content:Fromniya
File2:Afja.txt
Content:Fromniya
64 | P a g e
Dept Of MCA,RIT KOTTAYAM
FRAMES IN APPLET
65 | P a g e
Dept Of MCA,RIT KOTTAYAM
OUTPUT
66 | P a g e
Dept Of MCA,RIT KOTTAYAM
CALCULATOR
importjava.awt.*;
importjava.awt.event.*;
importjava.applet.*;
/* <applet code="calculator" width=300 height=300>
</applet>*/
public class calculator extends Applet implements ActionListener
{
int v1,v2,result;
TextField t1;
Button b[]=new Button[10];
Button add,sub,mul,div,clear,mod,EQ;
char op;
public void init()
{
t1=new TextField(50);
GridLayoutgl=new GridLayout(5,4);
setLayout(gl);
for(inti=0;i<10;i++)
{
b[i]=new Button(""+i);
}
add=new Button("+");
sub=new Button("-");
mul=new Button("*");
div=new Button("/");
mod=new Button("%");
clear=new Button("clear");
EQ=new Button("=");
t1.addActionListener(this);
add(t1);
for(inti=0;i<10;i++)
{
add(b[i]);
}
add(add);
add(sub);
add(mul);
add(div);
add(mod);
add(clear);
add(EQ);
for(inti=0;i<10;i++)
{
b[i].addActionListener(this);
67 | P a g e
Dept Of MCA,RIT KOTTAYAM
}
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
mod.addActionListener(this);
clear.addActionListener(this);
EQ.addActionListener(this);
}
68 | P a g e
Dept Of MCA,RIT KOTTAYAM
result=v1+v2;
else if(op=='-')
result=v1-v2;
else if(op=='*')
result=v1*v2;
else if(op=='/')
result=v1/v2;
else if(op=='%')
result=v1%v2;
t1.setText(""+result);
}
if(str.equals("clear"))
{
t1.setText("");
}
}
}
OUTPUT
69 | P a g e
Dept Of MCA,RIT KOTTAYAM
importjava.awt.*;
importjava.awt.event.*;
importjava.applet.*;
/*<applet code="MenuDemo" width=200 height=200>
</applet>*/
classMenuFrame extends Frame
{
String msg="";
CheckboxMenuItem debug;
MenuFrame(String title)
{
super(title);
MenuBarmbr=new MenuBar();
setMenuBar(mbr);
Menu file=new Menu("File");
MenuItem item1,item2,item3,item4,item5;
file.add(item1=new MenuItem("New"));
file.add(item2=new MenuItem("Open"));
file.add(item3=new MenuItem("Close"));
file.add(item4=new MenuItem("-"));
file.add(item5=new MenuItem("Qiut"));
mbr.add(file);
Menu edit=new Menu("Edit");
MenuItem item6,item7,item8,item9;
edit.add(item6=new MenuItem("Cut"));
edit.add(item7=new MenuItem("Copy"));
edit.add(item8=new MenuItem("Paste"));
edit.add(item9=new MenuItem("-"));
Menu sub=new Menu("Special");
MenuItem item10,item11,item12;
sub.add(item10=new MenuItem("First"));
sub.add(item11=new MenuItem("second"));
sub.add(item12=new MenuItem("Third"));
edit.add(sub);
debug=new CheckboxMenuItem("Debug");
edit.add(debug);
mbr.add(edit);
MyMenuHandler handler=new MyMenuHandler(this);
item1.addActionListener(handler);
item2.addActionListener(handler);
item3.addActionListener(handler);
item4.addActionListener(handler);
item5.addActionListener(handler);
70 | P a g e
Dept Of MCA,RIT KOTTAYAM
item6.addActionListener(handler);
item7.addActionListener(handler);
item8.addActionListener(handler);
item9.addActionListener(handler);
item10.addActionListener(handler);
item11.addActionListener(handler);
item12.addActionListener(handler);
debug.addItemListener(handler);
71 | P a g e
Dept Of MCA,RIT KOTTAYAM
else if(arg.equals("Edit"))
msg+="Edit.";
else if(arg.equals("Cut"))
msg+="Cut.";
else if(arg.equals("Copy"))
msg+="Copy.";
else if(arg.equals("Paste"))
msg+="Paste.";
else if(arg.equals("First"))
msg+="First.";
else if(arg.equals("Second"))
msg+="Second.";
else if(arg.equals("Third"))
msg+="Third.";
else if(arg.equals("Debug"))
msg+="Debug.";
menuFrame.msg=msg;
menuFrame.repaint();
}
public void itemStateChanged(ItemEventie)
{
menuFrame.repaint();
}
}
public class MenuDemo extends Applet
{
Frame f;
public void init()
{
f=new MenuFrame("MenuDemo");
int width=Integer.parseInt(getParameter("width"));
int height=Integer.parseInt(getParameter("height"));
setSize(new Dimension(width,height));
f.setSize(width,height);
f.setVisible(true);
}
public void start()
{
f.setVisible(true);
}
public void stop()
{
f.setVisible(false);
}
}
72 | P a g e
Dept Of MCA,RIT KOTTAYAM
OUTPUT
73 | P a g e
Dept Of MCA,RIT KOTTAYAM
JDBC ILLUSTRATION
Aim: Create a database named college using MS Access with a table student having following fields Roll
Number(primary key),Name,Branch,TotalMark.Initialize the table with different values.write a program
to display the details,insert,modify .
import java.io.*;
import java.sql.*;
classStudentdetails
{
public static void main(String args[])throws IOException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection db=DriverManager.getConnection("jdbc:odbc:dsstud","","");
Statement obj=db.createStatement();
obj.executeUpdate("create table student(rollnointeger,namevarchar(20),branch varchar(20),total
integer,primary key(rollno))");
obj.executeUpdate("insert into student values(104,'salu','mca',500)");
System.out.println("insert successfully");
int choice;
do
{
System.out.println("........MENU...........");
System.out.println("***********************");
System.out.println("1.INSERT");
System.out.println("2.MODIFY");
System.out.println("3.DELETE");
System.out.println("4.DISPLAY");
BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in));
intch,roll,mark;
String sname,sbranch;
System.out.println("Enter your choice");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println("Enter roll number");
roll=Integer.parseInt(br.readLine());
System.out.println("Enter name");
sname=br.readLine();
System.out.println("Enter branch");
sbranch=br.readLine();
System.out.println("Enter total marks");
mark=Integer.parseInt(br.readLine());
74 | P a g e
Dept Of MCA,RIT KOTTAYAM
obj.close();
System.out.println("upadate successfully");
break;
case 3:
System.out.println("Enter roll number");
roll=Integer.parseInt(br.readLine());
obj.executeUpdate("delete from student where rollno="+roll+"");
obj.close();
System.out.println("deleted");
break;
case 4:
Statement DataRequest=db.createStatement();
ResultSetrs;
String query="select * from student";
rs=DataRequest.executeQuery(query);
75 | P a g e
Dept Of MCA,RIT KOTTAYAM
System.out.println("\tROLLNO\t\tNAME\t\tBRANCH\t\tTOTAL");
while(rs.next())
{
roll=Integer.parseInt(rs.getString("rollno"));
sname=rs.getString("name");
sbranch=rs.getString("branch");
mark=Integer.parseInt(rs.getString("total"));
System.out.println("\t"+roll+"\t\t"+sname+"\t\t"+sbranch+"\t\t"+mark);
}
break;
default:
System.out.println("Invalid Choice");
break;
}
System.out.println("Do u want to continue if yes press 1 otherwise press any other number key");
choice=Integer.parseInt(br.readLine());
}while(choice==1);
db.close();
}
catch(ClassNotFoundException e)
{
System.err.println(e.getMessage());
}
catch(Exception c)
{
System.err.println(c.getMessage());
}
}
}
OUTPUT
........MENU...........
***********************
1.INSERT
2.MODIFY
3.DELETE
4.DISPLAY
Enter your choice
4
ROLLNO NAME BRANCH TOTAL
102 meenamca 520
104 salumca 500
200 vidhyamtech 680
108 shinumca 600
202 zeramtech 460
Do u want to continue if yes press 1 otherwise press any other number key
1
........MENU...........
***********************
1.INSERT
76 | P a g e
Dept Of MCA,RIT KOTTAYAM
2.MODIFY
3.DELETE
4.DISPLAY
Enter your choice
2
Enter roll number
102
Enter the field you wish to modify
1.Name
2.Branch
3.Total Marks
3
Enter total marks
530
upadate successfully
Do u want to continue if yes press 1 otherwise press any other number key 2
77 | P a g e
Dept Of MCA,RIT KOTTAYAM
78 | P a g e