Anda di halaman 1dari 78

Dept Of MCA,RIT KOTTAYAM

PROGRAM NO:1 DATE:12-01-13

SUM OF DIGITS

Aim: Write a program to find the sum of digits of a number.

import java.io.*;

class Sumofdigits

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

int n,s=0,a,d=0;

System.out.println("Enter a number");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

String str=br.readLine();

n=Integer.parseInt(str);

while(n>0)

d=n%10;

s=s+d;

n=n/10;

System.out.println("Sum of digits of number is"+s);

OUTPUT

Enter a number :19

Sum of digits of number is :10

1|Page
Dept Of MCA,RIT KOTTAYAM

PROGRAM NO:2 DATE:12-01-13

REVERSE OF A NUMBER

Aim :Write a program to find the reverse of a number.

import java.io.*;

class Reverse

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

int n,s=0,d=0,rev=0;

System.out.println("Enter a number");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

String str=br.readLine();

n=Integer.parseInt(str);

while(n>0)

d=n%10;

rev=rev*10+d;

n=n/10;

System.out.println("Reverse of number is"+rev);

}}

OUTPUT

Enter a number :123

Reverse of number is :321

2|Page
Dept Of MCA,RIT KOTTAYAM

PROGRAM NO:3 DATE:12-01-13

EVEN OR ODD

Aim: Program to check odd or even

import java.io.*;

class Oddoreven

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

int n;

System.out.println("Enter a number");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

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

PROGRAM NO:4 DATE:12-01-13

PRIME NUMBER SERIES

Aim:Write a program to list a set of prime numbers with in a range.

import java.io.*;

class Prime

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

int a,b,i,j,c=0;

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

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

PROGRAM NO:5 DATE:12-01-13

MENU OPERATIONS

Aim: Write a program to create a menu and perform the operations.


1.Find the sum of digits.
2.Find the reverse of a number.
3.Check the given number is even or odd.
4.List a set of prime number up to a range.

import java.io.*;

class Menu

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

int opt=0,n,rev=0,s=0,d=0,a,b,i,j,c=0;

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("-------MENU-------");

System.out.println("1.Sum of digits");

System.out.println("2.Reverse of a number");

System.out.println("3.Odd or even checking");

System.out.println("4.Prime numbers");

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

while(opt!=5)

System.out.println("Enter your option");

String str1=br.readLine();

opt=Integer.parseInt(str1);

switch(opt)

6|Page
Dept Of MCA,RIT KOTTAYAM

case 1:System.out.println("Enter a number");

String str2=br.readLine();

n=Integer.parseInt(str2);

while(n>0)

d=n%10;

s=s+d;

n=n/10;

System.out.println("Sum of digits of number is"+s);

break;

case 2:System.out.println("Enter a number");

String str3=br.readLine();

n=Integer.parseInt(str3);

while(n>0)

d=n%10;

rev=rev*10+d;

n=n/10;

System.out.println("Reverse of number is"+rev);

break;

case 3:System.out.println("Enter a number");

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;

case 4:System.out.println("Enter starting");

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

3.Odd or even checking

4.Prime numbers

5.Exit

Enter your option:2

Enter a number:586

Reverse of a number is: 685

Enter your option:5

9|Page
Dept Of MCA,RIT KOTTAYAM

PROGRAM NO:6 DATE:12-01-13


FIBONACCI SERIES

Aim:Write a program to generate Fibonacci series.

import java.io.*;

class Fibonacci

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

int n,f1=0,f2=1,f3=0;

System.out.println("Enter the limit");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

String str=br.readLine();

n=Integer.parseInt(str);

System.out.println("The fibonacci series is");

while(f3<=n)

System.out.println(f3);

f1=f2;

f2=f3;

f3=f1+f2;

}}}

OUTPUT

Enter the limit :5

The fibonacci series is :

0,1,1,2,3,5

10 | P a g e
Dept Of MCA,RIT KOTTAYAM

PROGRAM NO:7 DATE:22-01-13


FACTORIAL OF A NUMBER

Aim: Write a program to find factorial of a number.

import java.io.*;

class Factorial

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

int n,i;

long f=1;

System.out.println("Enter a number");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

String str=br.readLine();

n=Integer.parseInt(str);

for(i=1;i<=n;i++)

f=f*i;

System.out.println("The factorial is "+f);

OUTPUT

Enter a number :5

Factorial is :120

11 | P a g e
Dept Of MCA,RIT KOTTAYAM

PROGRAM NO:8 DATE: 22-01-13

DECIMAL TO BINARY CONVERSION

Aim: Write a program to input a decimal number and convert it into binary number.

import java.io.*;

import java.lang.Math;

class DToB{

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

int deci,i=0,n;

double bin=0;

long a;

System.out.println("Enter a decimal number");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

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; }

System.out.println("The binary equivalent is "+a);}}

OUTPUT

Enter a decimal number :10

The binary equivalent is :1010

12 | P a g e
Dept Of MCA,RIT KOTTAYAM

PROGRAM NO:9 DATE: 22-01-13


BINARY TO DECIMAL CONVERSION

Aim: Write a program to input a binary number and convert it into decimal number

import java.io.*;

import java.lang.Math;

class BToD

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

int i=0,n,bin,a;

double deci=0;

System.out.println("Enter a binary number");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

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;

System.out.println("The decimal equivalent is "+a);

13 | P a g e
Dept Of MCA,RIT KOTTAYAM

System.out.println("Enter the number");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

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

Enter a binary number :1010

The decimal equivalent is :10

14 | P a g e
Dept Of MCA,RIT KOTTAYAM

PROGRAM NO:10 DATE:02-02-13

FIBONACCI OR NOT

Aim: Write a program to check whether the given number is Fibonacci number or not.

import java.io.*;

class FibCheck

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

int n,f1=0,f2=1,f3=0,c=0;

System.out.println("Enter the number");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

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

System.out.println("The given number is fibonacci");

else

System.out.println("The given number is not fibonacci");

OUTPUT

Enter the number 2

The given number is fibonacci

16 | P a g e
Dept Of MCA,RIT KOTTAYAM

PROGRAM NO:11 DATE:02-02-13

PRIME FACTORS OF A NUMBER

Aim: Write a program to find the prime factors of a number.

import java.io.*;

class PrimeFactor

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

int i,j,k=0,c=0,num;

int a[]=new int[30];

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

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;

System.out.println("The prime factors of "+num+" are: ");

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

Enter a number :15

The prime factors of 15 are :

18 | P a g e
Dept Of MCA,RIT KOTTAYAM

PROGRAM NO:12 DATE:10-02-13

OPERATIONS ON MATRIX

Aim: Write a program to perform operations addition,multiplication and transpose on matrix

import java.io.*;

class Matrix

int i,j,m,n,p,q,k,x;

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

int a[][]=new int[10][10];

int b[][]=new int[10][10];

int c[][]=new int[10][10];

void add() throws IOException

System.out.println("Enter the size of matrices");

String str1=br.readLine();

m=Integer.parseInt(str1);

String str2=br.readLine();

n=Integer.parseInt(str2);

System.out.println("Enter the 1st matrix");

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);

System.out.println("Enter the 2nd matrix");

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];

System.out.println("The resultant matrix");

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();

void mul() throws IOException

System.out.println("Enter the size of 1st matrix");

String str5=br.readLine();

m=Integer.parseInt(str5);

String str6=br.readLine();

n=Integer.parseInt(str6);

System.out.println("Enter the 1st matrix");

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

System.out.println("Enter the size of 2nd matrix");

String str8=br.readLine();

p=Integer.parseInt(str8);

String str9=br.readLine();

q=Integer.parseInt(str9);

if(n==p)

System.out.println("Enter the 2nd matrix");

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");

System.out.println(" resultant matrix");

for(i=0;i<m;i++)

for(j=0;j<q;j++)

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

System.out.println();

void transpose()throws IOException

System.out.println("Enter the size of matrix");

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);

System.out.println("Enter the matrix");

for(i=0;i<m;i++)

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

String str13=br.readLine();

a[i][j]=Integer.parseInt(str13);

System.out.println(" resultant matrix");

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

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

int opt=0;

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

Matrix m=new Matrix();

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)

System.out.println("Enter your option");

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

Enter your option:

Enter the matrix:

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

PROGRAM NO:13 DATE:10-02-13

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

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

int acc_no;

double acc_balance,m,n,bal;

String acc_type,depositor_name;

void getData() throws IOException

BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter account number");

String str1=br1.readLine();

acc_no=Integer.parseInt(str1);

System.out.println("Enter depositor name");

depositor_name=br1.readLine();

27 | P a g e
Dept Of MCA,RIT KOTTAYAM

System.out.println("Enter account type");

acc_type=br1.readLine();

System.out.println("Enter account balance");

String str2=br1.readLine();

acc_balance=Double.parseDouble(str2);

void deposit()throws IOException

{ System.out.println("Enter amount to be deposit");

String str3=br.readLine();

m=Double.parseDouble(str3);

acc_balance=acc_balance+m; }

void withdraw()throws IOException

System.out.println("Enter amount to be withdraw");

String str4=br.readLine();

n=Double.parseDouble(str4);

bal=acc_balance-n;

if(bal<500)

System.out.println("Your balance is less than 500,so withdrawal is impossible");

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

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

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

int opt=0;

Account ac=new Account();

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)

System.out.println("Enter your option");

String str5=br.readLine();

opt=Integer.parseInt(str5);

switch(opt)

29 | P a g e
Dept Of MCA,RIT KOTTAYAM

case 1:ac.deposit(); break;

case 2:ac.withdraw(); break;

case 3:ac.putData(); break;

case 4:break;

default:System.out.println("Invalid option");

break; }}}}

OUTPUT

Enter account number:123

Enter depositor name: MINU

Enter account type:sb

Enter account balance:6000.0

-----TRANSACTIONS-----

1.Deposit

2.Withdrawal

3.Display details

4.Exit

Enter your option:3

Account number:123

Depositor:MINU

Account type:sb

Account balance:6000.0

30 | P a g e
Dept Of MCA,RIT KOTTAYAM

PROGRAM NO:14 DATE:10-02-13

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);

float volume(double r,double h)

return(3.14*r*r*h);

float volume(double l,double h,double w)

return(h*w*l);

class MethodOverload

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

31 | P a g e
Dept Of MCA,RIT KOTTAYAM

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

float a,l,h,r,w,h1;

Overload ob=new Overload();

System.out.println("Enter side of cube");

a=Float.parseFloat(br.readLine());

System.out.println("Enter radius and height of cylinder");

r=Float.parseFloat(br.readLine());

h=Float.parseFloat(br.readLine());

System.out.println("Enter length,height and width of rectangular box");

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));

System.out.println("Volume of rectangular box="+ob.volume(l,h1,w));

32 | P a g e
Dept Of MCA,RIT KOTTAYAM

OUTPUT

Enter side of cube:3

Volume of cube:27.0

Enter radius and height of cylinder:4

Volume of a cylinder: 251.20000000000000

Enter length,width and height of rectangle box:4

Volume of a rectangle: 60

33 | P a g e
Dept Of MCA,RIT KOTTAYAM

PROGRAM NO:15 DATE:28-02-13

USE OF THIS AND SUPER

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;

class Point3D extends Point

int z;

Point3D(int x,int y,int z)

super(x,y);

this.z=z;

class ThisSuper

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

34 | P a g e
Dept Of MCA,RIT KOTTAYAM

Point3D p=new Point3D(10,20,30);

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

PROGRAM NO:16 DATE: 28-02-13

DYNAMIC METHOD DISPATCH

Aim: Write a java program to display the details of employee,working in an organization.


Illustrate the concept of Dynamic method dispatch.

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

public static void main(String args[])

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

Job: Software Engineer

Salary: 25000

38 | P a g e
Dept Of MCA,RIT KOTTAYAM

PROGRAM NO:17 DATE:28-02-13

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.*;

abstract class Shape

abstract void calcArea();

abstract void calcPeri();

class Rectangle extends Shape

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

public double l,b;

void getData1()throws IOException

System.out.println("Enter length &breadth of rectangle");

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)));

class Circle extends Shape

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

public double r;

void getData2()throws IOException

System.out.println("Enter radious of circle");

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

class Triangle extends Shape

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

public double bs,h,hp;

void getData3()throws IOException

System.out.println("Enter base,height&hypotenuse of triangle");

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

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

Rectangle re=new Rectangle();

Circle c=new Circle();

Triangle t=new Triangle();

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

Enter the length & breadth of rectangle:

Area of rectangle: 20

Perimeter of rectangle:=18

Enter the radius of the circle:

Area of circle: 50.24

Perimeter of circle:=37.68

Enter base,height&hypotenuse of triangle

Area of triangle=25

43 | P a g e
Dept Of MCA,RIT KOTTAYAM

PROGRAM NO:18 DATE:15-03-13

MULTIPLE INHERITANCE USING INTERFACE

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;

BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in) );


System.out.println("Enter the no.of elements:");
n=Integer.parseInt(br.readLine());
System.out.println("Enter the elements of array: ");
for(i=0;i<n;i++)
{
a[i]=Integer.parseInt(br.readLine());
}

Sort S=new Sort();


System.out.println("Bubble sorted array:");
S.bubble_sort(a,n);
S.disp(a,n);
System.out.println("Inserton sorted array");
S.insertion_sort(a,n);
S.disp(a,n);
}

OUTPUT

Enter the no.of elements:


4
Enter the elements of array:
2
3
7
1
Bubble sorted array:
1
2
3
7
Inserton sorted array 1 2 3 7

45 | P a g e
Dept Of MCA,RIT KOTTAYAM

PROGRAM NO:19 DATE:15-03-13

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

Enter the numbers:


3
2
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.exit

Enter the choice:3


Product of the numbers
6
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.exit

Enter the choice:5

48 | P a g e
Dept Of MCA,RIT KOTTAYAM

PROGRAM NO:20 DATE:15-03-13

STRING AND STRING BUFFER CLASS

Aim: -Illustrate the methods available with string and string buffer class?

import java.io.*;

class se{

se(inti)

String word;

word = "Program by SreeLal,Rahul,Anish";

//length method:

int length = word.length();

System.out.println("Length: " + length);

//2. use the case functions:

System.out.println("toUpperCase: " + word.toUpperCase());

System.out.println("toLowerCase: " + word.toLowerCase());

//3. use the trim function

word = word.trim();

System.out.println("trim: " + word);

//4. check for a certain character using indexOf()

System.out.println("indexOf('s'): " + word.indexOf('s'));

//5. print out the beginning character using charAt()

System.out.println("first character: " + word.charAt(0));

//6. make the string shorter

word = word.substring(0, 4);

System.out.println("shorter string: " + word);

49 | P a g e
Dept Of MCA,RIT KOTTAYAM

public class stringBuffer{

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

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

String str;

try{

sest=new se(1);

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

str = in.readLine();

str += ", This is the illustration of SringBuffer class and it's functions. BY SWAT";

StringBufferstrbuf = new StringBuffer();

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.print("Reversed string : ");

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.print("Character at 6th position : ");

System.out.println(strbuf.charAt(6));

//substring()

System.out.print("Substring from position 3 to 6 : ");

System.out.println(strbuf.substring(3,7));

//deleteCharAt()

strbuf.deleteCharAt(3);

System.out.println(strbuf);

//capacity()

System.out.print("Capacity of StringBuffer object : ");

System.out.println(strbuf.capacity());

//delete() and length()

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

toUpperCase: PROGRAM BY SREELAL,RAHUL,ANISH

toLowerCase: program by sreelal,rahul,anish

trim: Program by SreeLal,Rahul,Anish

indexOf('s'): 28

first character: P

shorter string: Prog

Enter your name: anish

0anish, This is the illustration ofSringBuffer class and it's functions. BY SW

AT

HelloWorld

Hello_Java World

Reversed string :dlroWavaJ_olleH

Hello_Java World

Hello Java World

Character at 6th position : J

Substring from position 3 to 6 : lo J

Helo Java World

Capacity of StringBufferobject : 81

Helo J

52 | P a g e
Dept Of MCA,RIT KOTTAYAM

PROGRAM NO:21 DATE:20-03-13

USE OF GRAPHICAL METHODS IN APPLET

Aim:Draw a smiling human face using graphical methods(Applets)

importjava.awt.*;
importjava.applet.*;
//import java.graphics.*;
/*<applet code="Face" height=300 width=300>
</applet>
*/

public class Face extends Applet


{

public void paint(Graphics g)


{
g.drawString("welcome...",10,20);
g.drawOval(105,100,116,120);
g.drawOval(132,135,15,7);//LE
g.drawOval(178,135,15,7);//RE
g.fillOval(136,134,8,8);//LIR
g.fillOval(182,134,8,8);//LIR
g.drawOval(154,152,15,15);//N
g.drawArc(128,130,35,11,70,70);//LEB
g.drawArc(174,130,35,11,70,70);//REB
g.fillArc(146,165,35,20,180,180);

53 | P a g e
Dept Of MCA,RIT KOTTAYAM

OUTPUT

54 | P a g e
Dept Of MCA,RIT KOTTAYAM

PROGRAM NO:22 DATE20-03-13

METHODS IN APPLET

Aim:Illustrate with the help of an applet program ,the methods-getParameter(), getCodeBase(),


showStatus(),getDocumentBase().

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

PROGRAM NO:23 DATE:20-03-13

APPLET TO DISPLAY NAME AND ADDRESS OF COLLEGE

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

PROGRAM NO:24 DATE:03-04-13

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

Enter the basic pay


150
Enter the travel allowance
20
Enter the daily allowance
30
Enter the house rent allowance
20
Enter the PF
150
Enter the loan interest
180

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

PROGRAM NO:25 DATE:03-04-13

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!");
}
}
}

class B extends Thread


{
public void run()
{
try
{
Thread.sleep(100);
System.out.println("BThread started!");
System.out.println("Exit from B");
}
catch(InterruptedException e)
{
System.out.println("Interrupted!");
}
}
}

class C extends Thread


{
public void run()
{
try
{

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

PROGRAM NO:26 DATE:16-04-13

FILE OPERATIONS

Aim:Program to read from file and write to a file(IO)

import java.io.*;
class Files
{

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


{
FileInputStream fin;
FileOutputStreamfout;
inti;
try
{
try
{
fin=new FileInputStream(args[0]);

}
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

PROGRAM NO:27 DATE:16-04-13

FRAMES IN APPLET

Aim:Program to create simple frame,which can be closed.


importjava.awt.*;
importjava.awt.event.*;
import java.io.*;
public class Frames
{
public static void main(String args[])
{
Frame f=new Frame("Frame demo");
Label l=new Label("Illustration of Frame",Label.CENTER);
f.add(l);
f.setSize(300,300);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
}

65 | P a g e
Dept Of MCA,RIT KOTTAYAM

OUTPUT

66 | P a g e
Dept Of MCA,RIT KOTTAYAM

PROGRAM NO:28 DATE:20-04-13

CALCULATOR

Aim:Write a java program to create a simple 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);
}

public void actionPerformed(ActionEventae)


{
String str=ae.getActionCommand();
charch=str.charAt(0);
if ( Character.isDigit(ch))
t1.setText(t1.getText()+str);
else
if(str.equals("+"))
{
v1=Integer.parseInt(t1.getText());
op='+';
t1.setText("");
}
else if(str.equals("-"))
{
v1=Integer.parseInt(t1.getText());
op='-';
t1.setText("");
}
else if(str.equals("*"))
{
v1=Integer.parseInt(t1.getText());
op='*';
t1.setText("");
}
else if(str.equals("/"))
{
v1=Integer.parseInt(t1.getText());
op='/';
t1.setText("");
}
else if(str.equals("%"))
{
v1=Integer.parseInt(t1.getText());
op='%';
t1.setText("");
}
if(str.equals("="))
{
v2=Integer.parseInt(t1.getText());
if(op=='+')

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

PROGRAM NO:29 DATE:20-04-13

DESIGNING A SIMPLE MENU

Aim:Illustrate the menu class by designing a simple menu of your choice.

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);

MyWindowAdapter adapter=new MyWindowAdapter(this);


addWindowListener(adapter);
}
public void paint(Graphics g)
{
if(debug.getState())
g.drawString("debug on",10,200);
else
g.drawString("debug off",10,200);
}
}
classMyWindowAdapter extends WindowAdapter
{
MenuFramemenuFrame;
publicMyWindowAdapter(MenuFramemenuFrame)
{
this.menuFrame=menuFrame;
}
public void windowClosing(WindowEvent we)
{
menuFrame.setVisible(false);
}
}
classMyMenuHandler implements ActionListener,ItemListener
{
MenuFramemenuFrame;
publicMyMenuHandler(MenuFramemenuFrame)
{
this.menuFrame=menuFrame;
}
public void actionPerformed(ActionEventae)
{
String msg="You r in";
String arg=(String)ae.getActionCommand();
if(arg.equals("New"))
msg+="New.";
else if(arg.equals("Open"))
msg+="Open.";
else if(arg.equals("Close"))
msg+="Close.";
else if(arg.equals("Quit"))
msg+="Quit.";

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

PROGRAM NO:30 DATE:22-04-13

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.executeUpdate("insert intostudent values("+roll+",'"+sname+"','"+sbranch+"',"+mark+")");


System.out.println("insert successfully");
obj.close();
break;
case 2:
System.out.println("Enter roll number");
roll=Integer.parseInt(br.readLine());
int c;
System.out.println("Enter the field you wish to modify");
System.out.println("1.Name");
System.out.println("2.Branch");
System.out.println("3.Total Marks");
c=Integer.parseInt(br.readLine());
switch(c)
{
case 1:
System.out.println("Enter name");
sname=br.readLine();
obj.executeUpdate("update student set name='"+sname+"'where rollno="+roll+"");
break;
case 2:
System.out.println("Enter branch");
sbranch=br.readLine();
obj.executeUpdate("update student set branch='"+sbranch+"' where rollno= "+roll+"");
break;
case 3:
System.out.println("Enter total marks");
mark=Integer.parseInt(br.readLine());
obj.executeUpdate("update student set total="+mark+" where rollno="+roll+"");
break;
default:
System.out.println("Invalid Choice");
break;
}

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

Anda mungkin juga menyukai