Java & Oracle Msc. Computer Science
Java & Oracle Msc. Computer Science
COMPUTER SCIENCE
1. WRITE A PROGRAM TO FIND THE SUM AND AVERAGE OF TWO
NUMBERS.
import java.io.*;
public class sum_avg1
{
public static void main(String args[])
{
int a,b,s=0;
float ave=0;
try
{
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter the First number");
a=Integer.parseInt(in.readLine());
System.out.println("Enter the Second number");
b=Integer.parseInt(in.readLine());
s=a+b;
ave=s/2;
System.out.println("The sum of "+a+" and "+b+" are :"+s);
System.out.println("The average of "+a+" and "+b+" are :"+ave);
}
catch(Exception e)
{
}
}
}
BPC COLLEGE,PIRAVOM 1
JAVA & ORACLE MSc.
COMPUTER SCIENCE
2.WRITE A PROGRAM TO FIND THE SUM AND AVERAGE OF AN
ARRAY AND DISPLAY IT IN REVEERSE ORDER.
import java.io.*;
class array_reve
{
void getdata()
{
int sum=0,n,i,j;
float ave;
try
{
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter the limit");
n=Integer.parseInt(in.readLine());
System.out.println("Enter the elements into the array");
int a[]=new int[n];
for(i=0;i<n;i++)
{
a[i]=Integer.parseInt(in.readLine());
}
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
ave=sum/n;
System.out.println("The the array is:");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
System.out.println("Sum of the elements is:"+sum);
System.out.println("Average of the elements is:"+ave);
System.out.println("The the array in reverse order is:");
BPC COLLEGE,PIRAVOM 2
JAVA & ORACLE MSc.
COMPUTER SCIENCE
for(i=n-1;i>=0;i--)
{
System.out.println(a[i]);
}
}
catch(Exception e)
{
}
}
}
BPC COLLEGE,PIRAVOM 3
JAVA & ORACLE MSc.
COMPUTER SCIENCE
BPC COLLEGE,PIRAVOM 4
JAVA & ORACLE MSc.
COMPUTER SCIENCE
4. WRITE A PROGRAM TO IMPLIMENT BUBBLE SORT.
import java.io.*;
class sort
{
int n,i,j;
public void getdata()
{
try
{
DataInputStream in=new DataInputStream(System.in);
System.out.print("Enter the limit:");
n=Integer.parseInt(in.readLine());
int a[]=new int[n];
System.out.println("Enter the elements ");
for(i=0;i<n;i++)
{
a[i]=Integer.parseInt(in.readLine());
}
int temp;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.println("The elements after sorting is:");
BPC COLLEGE,PIRAVOM 5
JAVA & ORACLE MSc.
COMPUTER SCIENCE
for(i=0;i<n;i++)
{
System.out.println(a[i]+" ");
}
}
catch(Exception e)
{
}
}
}
public class Bubble6
{
public static void main(String args[])
{
sort S=new sort();
S.getdata();
}
}
BPC COLLEGE,PIRAVOM 6
JAVA & ORACLE MSc.
COMPUTER SCIENCE
5. STRING PALINDROME
import java.io.*;
class Strpalin
{
String s1,s2;
StringBuffer s3=new StringBuffer();
char c;
Strpalin()
{
DataInputStream in=new DataInputStream(System.in);
try
{
System.out.println("Enter the String");
s1=in.readLine();
}
catch(Exception e)
{
}
}
void strcopy()
{
int j=0;
for(int i=s1.length()-1;i>=0;i--)
{
c=s1.charAt(i);
s3=s3.append(c);
}
s2=s3.toString();
BPC COLLEGE,PIRAVOM 7
JAVA & ORACLE MSc.
COMPUTER SCIENCE
}
void strcompare()
{
if(s1.equals(s2))
System.out.println("String is palindrome");
else
System.out.println(" String is not palindrome");
}
void display()
{
System.out.println("Given String="+s1);
}
}
class StrPalimdrom10
{
public static void main(String args[])
{
Strpalin S=new Strpalin();
S.strcopy();
System.out.println(" Result \n***********");
S.display();
S.strcompare();
}
}
BPC COLLEGE,PIRAVOM 8
JAVA & ORACLE MSc.
COMPUTER SCIENCE
import java.io.*;
class Area
{
Area(int a)
{
System.out.println("Area of square="+(a*a));
}
Area(float r)
{
System.out.println("Area of circle="+(3.14*r*r));
}
}
class MainArea13
{
public static void main(String args[])
{
DataInputStream in=new DataInputStream(System.in);
int ch,s;
Area a1;
float r;
try
{
do
{
System.out.println(" MENU \n1.Area of Square\n2.Area of
circle\n3.Exit");
BPC COLLEGE,PIRAVOM 9
JAVA & ORACLE MSc.
COMPUTER SCIENCE
case 2:
{
}
case 3:
break;
default:
System.out.println("Invalid choice");
break;
}
}
while(ch!=3);
}
catch(Exception e)
{
}
}
}
BPC COLLEGE,PIRAVOM 10
JAVA & ORACLE MSc.
COMPUTER SCIENCE
7.WRITE A PROGRAM TO FIND FIBONACCI SERIES
import java.io.*;
class fib
{
int x,y,z,i;
fib(int n)
{
x=0;
y=1;
i=1;
System.out.print(x+" ");
System.out.print(y+" ");
for(i=3;i<=n;i++)
{
z=x+y;
System.out.print(+z+" ");
x=y;
y=z;
}
}
}
class fib14
{
public static void main(String args[])
{
DataInputStream in=new DataInputStream(System.in);
try
{
System.out.println("Enter the limit");
int n=Integer.parseInt(in.readLine());
System.out.println("fibonacci series is");
fib f=new fib(n);
BPC COLLEGE,PIRAVOM 11
JAVA & ORACLE MSc.
COMPUTER SCIENCE
}
catch(Exception e)
{
}
}
BPC COLLEGE,PIRAVOM 12
JAVA & ORACLE MSc.
COMPUTER SCIENCE
import java.io.*;
class sum1
{
void sum(int a,int b)
{
System.out.println("sum is:"+(a+b));
}
void sum(String s1,String s2)
{
System.out.println("sum of 2 string is:"+(s1.concat(s2)));
}
}
class pgm15
{
public static void main(String args[])
{
sum1 s1,s2;
int ch;
String s3,s4;
try
{
DataInputStream in=new DataInputStream(System.in);
do
{
System.out.println("\n\t\tMENU");
System.out.println("\n1.sum of 2 integers");
System.out.println("\n2.sum of 2 Strings");
System.out.println("\n3.Exit");
System.out.println("\nEnter your choice");
BPC COLLEGE,PIRAVOM 13
JAVA & ORACLE MSc.
COMPUTER SCIENCE
ch=Integer.parseInt(in.readLine());
switch(ch)
{
case 1:
{
s1=new sum1();
System.out.println("enter 2 numbers\n");
int p=Integer.parseInt(in.readLine());
int q=Integer.parseInt(in.readLine());
s1.sum(p,q);
break;
}
case 2:
{
s2=new sum1();
System.out.println("enter 1'st string\n");
s3=in.readLine();
System.out.println("enter 2'nd string\n");
s4=in.readLine();
s2.sum(s3,s4);
break;
}
case 3:
break;
default:
System.out.println("\n invalid operation");
break;
}
}while(ch!=3);
}
catch(Exception e) {}
}
}
BPC COLLEGE,PIRAVOM 14
JAVA & ORACLE MSc.
COMPUTER SCIENCE
import java.io.*;
abstract class Figure
{
double x,y,z;
Figure(double a,double b,double c)
{
x=a;
y=b;
z=c;
}
Figure(double a,double b)
{
x=a;
y=b;
}
Figure(double a)
{
x=y=a;
}
abstract void area();
}
class Rectangle extends Figure
{
Rectangle(double a,double b)
{
super(a,b);
}
void area()
{
System.out.print("Area of a Rectangle="+(x*y));
}
}
class Square extends Figure
{
Square(double a)
{
super(a);
}
void area()
{
System.out.print("Area of a Square="+(x*y));
BPC COLLEGE,PIRAVOM 15
JAVA & ORACLE MSc.
COMPUTER SCIENCE
}
}
class Triangle extends Figure
{
Triangle(double a,double b)
{
super(a,b);
}
void area()
{
double s1=(x*y)/2;
System.out.print("Area of a Triangle="+s1);
}
}
class Circle extends Figure
{
Circle(double a)
{
super(a);
}
void area()
{
System.out.print("Area of a Circle="+(3.14*x*y));
}
}
class Inherit19
{
public static void main(String args[])
{
int ch;
Figure F;
try
{
DataInputStream in=new DataInputStream(System.in);
do
{
System.out.println("\n\t\t\tMENU");
System.out.println("\n1.AREA OF A SQUARE");
System.out.println("\n2.AREA OF A RECTANGLE");
System.out.println("\n3.AREA OF A TRIANGLE");
System.out.println("\n4.AREA OF A CIRCLE");
System.out.println("\nEnter your choice:");
ch=Integer.parseInt(in.readLine());
switch(ch)
{
case 1:
BPC COLLEGE,PIRAVOM 16
JAVA & ORACLE MSc.
COMPUTER SCIENCE
{
double s;
System.out.println("\nEnter the side of the square:");
s=Double.parseDouble(in.readLine());
F=new Square(s);
F.area();
break;
}
case 2:
{
double b1;
double l1;
System.out.println("\nEnter the length and breadth of a rectangle");
l1=Double.parseDouble(in.readLine());
b1=Double.parseDouble(in.readLine());
F=new Rectangle(l1,b1);
F.area();
break;
}
case 3:
{
double x,y;
System.out.println("\nEnter the sides of a triangle");
x=Double.parseDouble(in.readLine());
y=Double.parseDouble(in.readLine());
F=new Triangle(x,y);
F.area();
break;
}
case 4:
{
double r;
System.out.println("\nEnter the radious of the circle");
r=Double.parseDouble(in.readLine());
F=new Circle(r);
F.area();
break;
}
case 5:
break;
default:
System.out.println("\nWrong choice");
break;
}
}while(ch!=5);
}
catch(Exception e)
BPC COLLEGE,PIRAVOM 17
JAVA & ORACLE MSc.
COMPUTER SCIENCE
{
}
}}
BPC COLLEGE,PIRAVOM 18
JAVA & ORACLE MSc.
COMPUTER SCIENCE
10. PGM TO PRINT EMPLOYEE DETAILS USING THE CONCEPT OF
MULTILEVEL INHERITANCE
import java.io.*;
class Details
{
int cd;
String na,dept;
Details(int x,String y,String z)
{
cd=x;
na=y;
dept=z;
}
}
class EAllowance extends Details
{
int ta,da,hra,bsal;
EAllowance(int a,String c,String b,int w,int x,int y,int z)
{
super(a,b,c);
ta=w;
da=x;
hra=y;
bsal=z;
}
}
class NetSalary extends EAllowance
{
int salary;
NetSalary(int a,String b,String c,int w,int x,int y,int z)
{
super(a,b,c,w,x,y,z);
}
void finalSalary()
{
salary=ta+da+hra+bsal;
}
}
class Employee extends NetSalary
{
Employee(int a,String b,String c,int x,int w,int y,int z)
{
super(a,b,c,w,x,y,z);
}
void putDetails() throws Exception
{
BPC COLLEGE,PIRAVOM 19
JAVA & ORACLE MSc.
COMPUTER SCIENCE
System.out.print("\n"+cd+"\t"+na+"\t"+dept+"\t\t"+ta+"\t"+hra+"\t"+bsal+"\t
\t"+salary+"\n");
}
}
class Emp
{
public static void main(String args[])
{
DataInputStream din=new DataInputStream(System.in);
int code,TA,DA,HRA,basic,n,i,j,k;
String Name,department;
try
{
System.out.println("Enter the no : of Employees");
n=Integer.parseInt(din.readLine());
Employee E[]=new Employee[n];
int c[]=new int [n];
for(i=0;i<n;i++)
{
System.out.println("Employee"+(i+1)+"\n");
System.out.println("Enter the code,Name,department,TA,DA,HRA,Basic
salary\n");
code=Integer.parseInt(din.readLine());
Name=din.readLine();
department=din.readLine();
TA=Integer.parseInt(din.readLine());
DA=Integer.parseInt(din.readLine());
HRA=Integer.parseInt(din.readLine());
basic=Integer.parseInt(din.readLine());
E[i]=new Employee(code,Name,department,TA,DA,HRA,basic);
E[i].finalSalary();
}
for(i=0;i<n;i++)
{
c[i]=E[i].salary;
}
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
{
int temp;
if(c[i]<c[j])
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
BPC COLLEGE,PIRAVOM 20
JAVA & ORACLE MSc.
COMPUTER SCIENCE
}
}
System.out.println("Employee Details\n");
System.out.print("\n Code\tname\tDepartment\tTA\tDA\tHRA|tBasic
salary\t\tnetSal\n");
k=-1;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(c[i]==E[j].salary&&c[i]!=k)
{
E[j].putDetails();
}
}
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
BPC COLLEGE,PIRAVOM 21
JAVA & ORACLE MSc.
COMPUTER SCIENCE
11.USING PACKAGE FIND THE nCr OF A NUMBER
package fpack;
public class fact
{
public int fact1(int x)
{
int f=1,i,n;
n=x;
for(i=1;i<=n;i++)
{
f=f*i;
}
return(f);
}
}
import fpack.fact;
import java.io.*;
class NCR24
{
public static void main(String args[])
{
try
{
DataInputStream in=new DataInputStream(System.in);
int n,r,d;
int n1,r1,d1,rslt;
System.out.println("Enter the value of n and r");
n=Integer.parseInt(in.readLine());
r=Integer.parseInt(in.readLine());
fact F=new fact();
n1=F.fact1(n);
r1=F.fact1(r);
d=n-r;
d1=F.fact1(d);
rslt=(n1)/(r1*d1);
System.out.println(n+"C"+r+" = "+rslt);
}
catch(Exception e)
{
}
}
BPC COLLEGE,PIRAVOM 22
JAVA & ORACLE MSc.
COMPUTER SCIENCE
12..WRITE A PROGRAM TO IMPLEMENT MULTIPLE CATCH
STATEMENT
import java.io.*;
System.out.println("***MENU***");
System.out.println("1.Arithmeticexception");
System.out.println("2.ArrayIndexOutofBoundsException");
System.out.println("3.NumberFormatException");
System.out.println("4.Exit");
System.out.println("Enter your choice");
mn=Integer.parseInt(in.readLine());
switch(mn)
{
case 1:
{
System.out.println("Enter the numbers");
a=Integer.parseInt(in.readLine());
BPC COLLEGE,PIRAVOM 23
JAVA & ORACLE MSc.
COMPUTER SCIENCE
b=Integer.parseInt(in.readLine());
System.out.println(a+"/"+b+" = "+(a/b));
break;
}
case 2:
{ System.out.println("\nEnter the Array Size:");
a=Integer.parseInt(in.readLine());
System.out.println("Enter the Array elements:");
for(i=0;i<a;i++)
c[i]=Integer.parseInt(in.readLine());
System.out.println("Array Elements are:");
for(i=0;i<a;i++)
System.out.println(c[i]+"\n");
break;
}
case 3:
{ System.out.println("\nEnter the array size:");
a=Integer.parseInt(in.readLine());
System.out.println("Enter the array elements");
for(i=0;i<a;i++)
c[i]=Integer.parseInt(in.readLine());
System.out.println("Array elements are:");
for(i=0;i<a;i++)
System.out.println(c[i]+"\n");
break;
}
case 4:
break;
default:
{
System.out.println("wrong choice");
BPC COLLEGE,PIRAVOM 24
JAVA & ORACLE MSc.
COMPUTER SCIENCE
break;
}
}
}
catch(ArithmeticException e)
{
System.out.println(e+" is Quoted");
}
catch(ArrayIndexOutOfBoundsException d)
{
System.out.println(d+" is Quoted");
}
catch(NumberFormatException f)
{
System.out.println(f+" is Quoted");
}
catch(Exception g)
{
System.out.println(g+"is Quoted");
}
}while(mn!=4);
}
}
BPC COLLEGE,PIRAVOM 25
JAVA & ORACLE MSc.
COMPUTER SCIENCE
BPC COLLEGE,PIRAVOM 26
JAVA & ORACLE MSc.
COMPUTER SCIENCE
class inter_y_s_s
{
public static void main(String args[])
{
A ThreadA=new A();
B ThreadB=new B();
C ThreadC=new C();
System.out.println("Start tread A");
ThreadA.start();
System.out.println("Start tread B");
ThreadB.start();
System.out.println("Start tread C");
ThreadC.start();
System.out.println("End of mainThread");
}
}
BPC COLLEGE,PIRAVOM 27
JAVA & ORACLE MSc.
COMPUTER SCIENCE
BPC COLLEGE,PIRAVOM 28
JAVA & ORACLE MSc.
COMPUTER SCIENCE
15. CREATE A MULTITHREADED PROGRAM AND ASSIGN
PRIORITIES.
BPC COLLEGE,PIRAVOM 29
JAVA & ORACLE MSc.
COMPUTER SCIENCE
threadC.setPriority(Thread.MAX_PRIORITY);
threadB.setPriority(threadA.getPriority()+1);
threadA.setPriority(Thread.MIN_PRIORITY);
System.out.println("Start Thread A");
threadA.start();
System.out.println("Start Thread B");
threadB.start();
System.out.println("Start Thread C");
threadC.start();
System.out.println("End of the main Thread");
}
}
BPC COLLEGE,PIRAVOM 30
JAVA & ORACLE MSc.
COMPUTER SCIENCE
16.ADD TWO NUMBERS USING APPLET
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*<applet code=Add_App33 WIDTH=300 HEIGHT=100>
</Applet>*/
public class Add_App33 extends Applet implements ActionListener
{
TextField t1,t2,t3;
Label l1,l2,l3;
Button b1;
public void init()
{
setLayout(null);
t1=new TextField(5);
t2=new TextField(5);
t3=new TextField(5);
l1=new Label("Enter the first number");
l2=new Label("Enter the second number");
l3=new Label("Result");
b1=new Button("sum");
add(l1);
l1.setBounds(20,20,120,20);
add(t1);
t1.setBounds(170,20,100,20);
add(l2);
l2.setBounds(20,50,130,20);
add(t2);
t2.setBounds(170,50,100,20);
add(l3);
l3.setBounds(20,70,100,20);
add(t3);
t3.setBounds(170,70,100,20);
add(b1);
b1.setBounds(180,90,50,30);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{int x=0,y=0,z=0;
String s1,s2;
s1=t1.getText();
s2=t2.getText();
x=Integer.parseInt(s1);
y=Integer.parseInt(s2);
z=x+y;
t3.setText(String.valueOf(z));}}
BPC COLLEGE,PIRAVOM 31
JAVA & ORACLE MSc.
COMPUTER SCIENCE
import java.io.*;
import java.awt.*;
import java.applet.*;
/*<APPLET CODE=Face WIDTH=200 HEIGHT=400>
</APPLET>*/
BPC COLLEGE,PIRAVOM 32
JAVA & ORACLE MSc.
COMPUTER SCIENCE
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code=Adapter39 width=300 height=100>
</applet>
*/
public class Adapter39 extends Applet
{
public void init()
{
addMouseListener(new MyMouseAdapter(this));
}
}
class MyMouseAdapter extends MouseAdapter
{
Adapter39 adapterdemo;
public MyMouseAdapter(Adapter39 adapterdemo)
{
this.adapterdemo=adapterdemo;
}
public void mouseClicked(MouseEvent me)
{
adapterdemo.showStatus("Mouse Clicked");
}
BPC COLLEGE,PIRAVOM 33
JAVA & ORACLE MSc.
COMPUTER SCIENCE
import java.io.*;
class COPY39
{
public static void main(String args[])
{
try
{
FileReader f1=new FileReader("e.txt");
FileWriter f2=new FileWriter("f.txt");
int ch;
while((ch=f1.read())!=-1)
{f2.write(ch);
}
f1.close();
f2.close();
}
catch(Exception e)
{
}
}
}
BPC COLLEGE,PIRAVOM 34
JAVA & ORACLE MSc.
COMPUTER SCIENCE
Sum
import java.rmi.*;
SumImpl
import java.rmi.*;
import java.rmi.server.*;
{
super();
}
public int Sum(int i,int j) throws RemoteException
{
return i+j;
}
}
SumClient
import java.rmi.*;
import java.io.*;
BPC COLLEGE,PIRAVOM 35
JAVA & ORACLE MSc.
COMPUTER SCIENCE
try
{
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
System.out.println("Enter the first number");
a=Integer.parseInt(br.readLine());
System.out.println("Enter the second number");
b=Integer.parseInt(br.readLine());
Sum h=(Sum)Naming.lookup("rmi://localhost/server");
System.out.println("SUM IS:"+h.Sum(a,b));
}
catch(Exception e)
{
System.out.println("Error:"+e);
}
}
}
SumServer
import java.rmi.*;
import java.rmi.server.*;
import java.net.*;
BPC COLLEGE,PIRAVOM 36
JAVA & ORACLE MSc.
COMPUTER SCIENCE
BPC COLLEGE,PIRAVOM 37