0% found this document useful (0 votes)
140 views

Java & Oracle Msc. Computer Science

The document contains 8 Java programs with explanations and code to demonstrate different Java concepts: 1. A program to find the sum and average of two numbers. 2. A program to find the sum and average of an array and display it in reverse order. 3. A program to convert a decimal number to binary number. 4. A program to implement bubble sort. 5. A program to check if a string is a palindrome. 6. A program to find the area of a circle and square using constructor overloading. 7. A program to find the Fibonacci series. 8. A program to sum two numbers and concatenate two strings using method overloading.

Uploaded by

Sreeja shaji
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
140 views

Java & Oracle Msc. Computer Science

The document contains 8 Java programs with explanations and code to demonstrate different Java concepts: 1. A program to find the sum and average of two numbers. 2. A program to find the sum and average of an array and display it in reverse order. 3. A program to convert a decimal number to binary number. 4. A program to implement bubble sort. 5. A program to check if a string is a palindrome. 6. A program to find the area of a circle and square using constructor overloading. 7. A program to find the Fibonacci series. 8. A program to sum two numbers and concatenate two strings using method overloading.

Uploaded by

Sreeja shaji
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 37

JAVA & ORACLE MSc.

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)
{
}
}
}

public class sum_avg_array2


{
public static void main(String args[])
{
array_reve A=new array_reve();
A.getdata();
}
}

BPC COLLEGE,PIRAVOM 3
JAVA & ORACLE MSc.
COMPUTER SCIENCE

3.WRITE A PROGRAM TO CONVERT A DECIMAL NUMBER TO


BINARY NUMBER.
import java.io.*;
public class deci_bin4
{
public static void main(String args[])
{
int n;
try
{
DataInputStream in=new DataInputStream(System.in);
System.out.print("Enter the decimal number:");
n=Integer.parseInt(in.readLine());
int p[ ]=new int[10];
int i=0;
while(n>0)
{
p[i]=n%2;
n=n/2;
i++;
}
System.out.println();
System.out.print("The binary number is:");
int j;
for(j=i-1;j>=0;j--)
{
System.out.print(p[j]);
}
}
catch(Exception e)
{}
}}

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

6. WRITE A PROGRAM TO FIND THE AREA OF CIRCLE & SQUARE


USING CONSTRUCTOR OVERLOADING

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

System.out.println("Enter Your Choice");


ch=Integer.parseInt(in.readLine());
switch(ch)
{
case 1:
{

System.out.println("Enter the Size of the Square");


s=Integer.parseInt(in.readLine());
a1=new Area(s);
break;

BPC COLLEGE,PIRAVOM 9
JAVA & ORACLE MSc.
COMPUTER SCIENCE
case 2:
{

System.out.println("Enter the radius of the Circle");


r=Float.parseFloat(in.readLine());
a1=new Area(r);
break;

}
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

8.WRITE APROGRAM TO SUM OF TWO NUMBERS AND STRING


USING METHOD OVERLOADING

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

9.WRITE A PROGRAM TO FIND THE AREA OF


CIRCLE,SQUARE,RECTANGLE & TRIANGLE USING INHERITANCE.

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

public class Catch29


{
public static void main(String args[])
{
int mn=0,a,b,i;
int c[ ]=new int[5];
do
{
try
{
DataInputStream in=new DataInputStream(System.in);

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

13.WRITE A PROGRAM TO IMPLEMENT yield() ,stop(),AND sleep()


IN THREAD

class A extends Thread


{
public void run()
{
for(int i=1;i<=5;i++)
{
if (i==1)yield();
System.out.println("\tfrom tread A:i="+i);
}
System.out.println("exit from A");
}
}
class B extends Thread
{
public void run()
{
for(int j=1;j<=5;j++)
{
System.out.println("\tfrom tread B:j="+j);
if(j==3)stop();
}
System.out.println("exit from B");
}
}
class C extends Thread
{
public void run()
{
for(int k=1;k<=5;k++)
{
System.out.println("\tform tread C:k="+k);
if(k==1)
try
{
sleep(1000);
}
catch(Exception e)
{
}
}
System.out.println("exit from C");
}
}

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

14.MULTITHREAD PGM USING RUNNABLE INTERFACE

class X implements Runnable


{
public void run()
{
for(int i=0;i<=10;i++)
{
System.out.println("\tThreadX:"+i);
}
System.out.println("End of ThreadX");
}}
class inter_run31
{
public static void main(String args[])
{
X runnable=new X();
Thread threadX=new Thread(runnable);
threadX.start();
System.out.println("End of main thread");
}
}

BPC COLLEGE,PIRAVOM 28
JAVA & ORACLE MSc.
COMPUTER SCIENCE
15. CREATE A MULTITHREADED PROGRAM AND ASSIGN
PRIORITIES.

class A extends Thread


{
public void run()
{
System.out.println("thread A started");
for(int i=1;i<=4;i++)
{
System.out.println("From ThreadA:i="+i);
}
System.out.println("Exit from A");
}
}
class B extends Thread
{
public void run()
{
System.out.println("thread B started");
for(int j=1;j<=4;j++)
{
System.out.println("From ThreadB:j="+j);
}
System.out.println("Exit from B");
}
}
class C extends Thread
{
public void run()
{
System.out.println("thread C started");
for(int k=1;k<=4;k++)
{
System.out.println("From Thread C:K="+k);
}
System.out.println("Exit from C");
}
}
class Threadpriority33
{
public static void main(String args[])
{
A threadA=new A();
B threadB=new B();
C threadC=new C();

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

17. WRITE A PROGRAM TO DRAW HUMAN FACE USING APPLET

import java.io.*;
import java.awt.*;
import java.applet.*;
/*<APPLET CODE=Face WIDTH=200 HEIGHT=400>
</APPLET>*/

public class Face extends Applet


{
public void paint(Graphics g)
{
g.drawOval(40,40,120,150); //head
g.drawOval(57,75,30,20); //left eye
g.drawOval(110,75,30,20); //right eye
g.fillOval(68,81,10,10); //pupil left
g.fillOval(121,81,10,10); //pupil right
g.drawOval(85,100,30,30); //nose
g.fillArc(60,125,80,40,180,180); //mouth
g.drawOval(25,92,15,30); //left ear
g.drawOval(160,92,15,30); //right ear
}
}

BPC COLLEGE,PIRAVOM 32
JAVA & ORACLE MSc.
COMPUTER SCIENCE

18.IMPLIMENT MOUSE LISTENER USING ADAPTER CLASS.

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

public void mouseEntered(MouseEvent me)


{
adapterdemo.showStatus("Mouse Entered");
}
public void mouseExited(MouseEvent me)
{
adapterdemo.showStatus("Mouse Exited");
}
public void mousePressed(MouseEvent me)
{
adapterdemo.showStatus("MousePressed");
}
}

BPC COLLEGE,PIRAVOM 33
JAVA & ORACLE MSc.
COMPUTER SCIENCE

19.COPY ONE FILE INTO ANOTHER

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

20. WRITE A PROGRAM TO ADD TWO NUMBERS USING RMI

Sum

import java.rmi.*;

public interface Sum extends Remote


{
public int Sum(int i,int j)throws RemoteException;
}

SumImpl

import java.rmi.*;
import java.rmi.server.*;

public class SumImpl extends UnicastRemoteObject implements Sum


{
public SumImpl() throws RemoteException

{
super();
}
public int Sum(int i,int j) throws RemoteException
{
return i+j;
}
}

SumClient

import java.rmi.*;
import java.io.*;

public class SumClient


{
public static void main(String args[])
{
int a,b;

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

public class SumServer


{
public static void main(String args[])
{
try
{
Sum h=new SumImpl();
Naming.rebind("server",h);
System.out.println("object is registered");
System.out.println("new server is waitin for client");
}
catch(Exception e)
{
System.out.println("Error:"+e);
}
}
}

BPC COLLEGE,PIRAVOM 36
JAVA & ORACLE MSc.
COMPUTER SCIENCE

BPC COLLEGE,PIRAVOM 37

You might also like