Prog – 1 : WAP in JAVA to find the greater of two numbers
import [Link];
class greate
{
public static void main(String args[])
{
DataInputStream in=new DataInputStream([Link]);
int a=22,b=23;
try
{
[Link]("enter the value of a ");
a=[Link]([Link]());
[Link]("enter the value of b ");
b=[Link]([Link]());
}
catch(Exception e)
{
}
if(a>b)
{
[Link]("a is greater");
}
else
[Link]("b is greater");
}
1
Output :
enter the value of a 5
enter the value of b 9
b is greater
2
Prog – 2 : WAP in JAVA to perform arithmetic operations
between two floating point numbers
class aoperation
{
public static void main(String args[])
{
float a = 15.0f;
float b = 5.0f;
float c = a + b;
[Link](" the add is " + c );
float d = a - b;
[Link](" the sub is " + d );
float e = a*b;
[Link](" the mul is " + e );
float f = a/b;
[Link](" the div is " + f);
float g = a%b;
[Link](" the mod is " + g);
}
}
3
Output :
the add is 20.0
the sub is 10.0
the mul is 75.0
the div is 3.0
the mod is 0.0
4
Prog – 3 : WAP in JAVA to interchange the value of three
variables without using fourth variable(assignment operator)
class assign
{
public static void main(String args[])
{
float a = 15.0f;
float b = 5.0f;
float c = 10.2f;
a=a+b+c;
b=a-(b+c);
c=a-(b+c);
a=a-(b+c);
[Link](" a = " + a );
[Link](" b = " + b );
[Link](" c = " + c );
}
}
5
Output :
a = 10.200001
b = 15.000001
c = 5.0
6
Prog – 4 : WAP in JAVA demonstrate nested if statement
import [Link];
class nestedif
{
public static void main(String args[])
{
DataInputStream in = new DataInputStream([Link]);
int a=0 , b=0 , c=0;
try
{
[Link]("enter the value of a , b & c:");
a = [Link]([Link]());
b = [Link]([Link]());
c = [Link]([Link]());
}
catch(Exception e)
{
}
if(a>b)
{
if(a>c)
[Link]( a + " is greatest.");
else
[Link]( b + " is greatest.");
}
else if(b>c)
[Link]( b + " is greatest.");
else
[Link]( c + " is greatest.");
}
}
7
Output:
enter the value of a , b & c:
10
20
90
90 is greatest.
8
Prog – 5 : WAP in JAVA demonstrate switch statement
import [Link];
class operations
{
public static void main(String args[])
{
DataInputStream in = new DataInputStream([Link]);
int a=0,b=0;
int i=0;
int add=0 , sub=0 , mul = 0, div=0;
try
{
[Link]("enter value of a and b :");
a = [Link]([Link]());
b = [Link]([Link]());
[Link]("Enter the choice :");
[Link]("1: Addition");
[Link]("2: Subtraction");
[Link]("3: Multiplication");
[Link]("4: Division");
i = [Link]([Link]());
}
catch(Exception e)
{
}
switch(i)
{
case 1 :
add=a+b;
[Link]("Addition = " + add);
break;
case 2 :
sub=a-b;
[Link]("subtraction = " + sub);
break;
case 3 :
mul=a*b;
[Link]("multiplication = " + mul);
break;
9
case 4 :
div=a/b;
[Link]("Division = " + div);
break;
default :
[Link]("Invalid choice");
}
}
}
10
Output:
enter value of a and b :
10
20
Enter the choice :
1: Addition
2: Subtraction
3: Multiplication
4: Division
1
Addition = 30
11
Prog – 6 : WAP in JAVA to find the area of rectangle using
the concept of classes and methods
class rectangle
{
int l ;
int br;
void getdata(int x,int y)
{
l = x;
br = y;
}
int rectarea()
{
int area = l * br;
return(area);
}
}
class rectarea
{
public static void main(String args[])
{
int area1,area2;
rectangle rect1 = new rectangle();
rectangle rect2 = new rectangle();
rect1.l = 10;
[Link] = 5;
area1= rect1.l * [Link];
[Link](20 , 25);
area2 = [Link]();
[Link]("area1 is " + area1);
[Link]("area2 is " + area2);
}
}
12
Output:
area1 is 50
area2 is 500
13
Prog – 7 : WAP in JAVA to demonstrate the concept of
constructors
class constr
{
int l , br;
constr(int x,int y)
{
l = x;
br = y;
}
int rectarea()
{
return(l * br);
}
}
class area
{
public static void main(String args[])
{
constr rect1 = new constr(14,10);
int area1 = [Link]();
[Link]("area is" + area1);
}
}
14
Output:
area is140
15
Prog – 8 : WAP in JAVA for static members
class stat
{
static float mul(float x , float y)
{
return(x*y);
}
static float div(float x , float y)
{
return(x/y);
}
}
class test1
{
public static void main(String agrs[])
{
float a = [Link](4.0 , 5.0);
float b = [Link](a , 2.0);
[Link](“ The result = “ + b);
}
}
16
Output:
b = 10.0
17
Prog –9 : WAP in JAVA for object as an argument to the
method
class student
{
int age;
int fees;
void data()
{
age = 20;
fees = 540;
}
void getdata(student s)
{
age = [Link];
fees = [Link];
}
void display()
{
[Link]("age =" + age);
[Link]("fees = " + fees);
}
}
class program
{
public static void main(String args[])
{
student s1 = new student();
student s2 = new student();
[Link]();
[Link]();
[Link](s1);
[Link]();
}
}
18
Output:
age =20
fees = 540
age =20
fees = 540
19
Prog – 10 : WAP in JAVA for call by value and call by
reference methods
class student
{
int age;
double fees;
void data(int a , double b)
{
age = a;
fees = b;
}
void getdata(student s)
{
age = age + [Link];
fees = fees + [Link];
}
void display()
{
[Link]("Age = " + age);
[Link]("fees = " + fees);
}
}
class program1
{
public static void main(String args[])
{
student s1 = new student();
student s2 = new student();
[Link](30,4569);
[Link]();
[Link](s1);
[Link]();
}
}
20
Output:
Age = 30
fees = 4569.0
Age = 60
fees = 9138.0
21
Prog – 11 : WAP in JAVA to find the factorial of a number
using the concept of recursion
class factorial
{
int fact(int n)
{
int result;
if(n==1)
return(1);
else
resut = fact(n-1) * n;
return(result);
}
}
class recur
{
public static void main(String args[])
{
factorial f = new factorial();
[Link](“The factorial = ” + [Link](3));
[Link](“The factorial = ” + [Link](4));
[Link](“The factorial = ” + [Link](5));
}
}
22
Output:
The factorial = 6
The factorial = 24
The factorial = 120
23
Prog – 12 : WAP in JAVA to demonstrate the concept of
method overriding
class super1
{
int x;
super1(int x)
{
this.x = x;
}
void display()
{
[Link]("value is" + x);
}
}
class sub extends super1
{
int y;
sub(int x,int y)
{
super(x);
this.y = y;
}
void display()
{
[Link]("value is" + x);
[Link]("value is" + y);
}
}
class test
{
public static void main(String args[])
{
sub s1 = new sub(10,12);
[Link]();
}
}
24
Output:
value is10
value is12
25
Prog – 13 : WAP in JAVA to find the sum of first ten natural
numbers
class sum
{
public static void main(String args[])
{
int n[] = {1,2,3,4,5,6,7,8,9,10};
int i;
[Link]("the given array elements are:");
for(i =0;i<10;i++)
{
[Link](n[i]);
}
int sum = 0;
for(i =0;i<10;i++)
{
sum = sum + n[i];
}
[Link]("The sum is" + sum);
}
}
26
Output:
the given array elements are:
1
2
3
4
5
6
7
8
9
10
The sum is55
27
Prog – 14 : WAP in JAVA to demonstrate the concept of type
conversion
class type
{
public static void main(String args[])
{
int i;
float sum = 0.0f;
for(i = 1;i<= 5;i++)
{
sum = sum + 1/float(i);
[Link]("i = " + i );
[Link]("sum = " + sum);
}
}
}
28
Output:
i=1
sum = 1.0
i=2
sum = 1.5
i=3
sum = 1.83
i=4
sum = 2.08
i=5
sum = 2.28
29
Prog – 15 : WAP in JAVA to search an element from the list by
using the concept of array length
class lsearch
{
public static void main(String args[])
{
int n[] = {4,8,9,6,10,13,12,14,11,13};
int a,i,flag = 0;
a = 12;
for(i=0;i<10;i++)
{
[Link](n[i]);
}
for(i=0;i<[Link];i++)
{
if (n[i] == a)
{
flag = 1;
[Link]("number is found");
break;
}
}
if(flag == 0)
{
[Link]("The number is not found");
}
}
}
30
Output:
4
8
9
6
10
13
12
14
11
13
number is found
31
Prog – 16 : WAP in JAVA to demonstrate two-dimensional
array
class tda
{
public static void main(String args[])
{
int [][]a ={{4,5,3},{16,12,10},{2,9,8}};
int min = a[0][0];
for(int i=0;i<[Link];i++)
{
for(int j=0;j<a[i].length;j++)
{
if(min>a[i][j])
{
min=a[i][j];
}
}
}
[Link]("The minimum number = " + min);
}
}
32
Output:
The minimum number = 2
33
Prog – 17 : WAP in JAVA to implement the interfaces for
calculating the area of a rectangle and a circle
interface area
{
final static float pi = 3.14f;
float compute(float x, float y);
}
class rectangle implements area
{
public float compute(float x,float y)
{
return(x*y);
}
}
class circle implements area
{
public float compute(float x,float y)
{
return(pi*x*x);
}
}
class test1
{
public static void main(String args[])
{
rectangle rect = new rectangle();
circle cir = new circle();
area area1;
area1 = rect;
[Link]("Area of rectangle = " + [Link](5,10));
area1 = cir;
[Link]("Area of circle = " + [Link](10,1));
}
}
34
Output:
Area of rectangle = 50.0
Area of circle = 314.0
35
Prog – 18 : WAP in JAVA to implement the concept of
accessing interface variable in multiple inheritance
class student
{
int rollno;
void getnumber(int n)
{
rollno = n;
}
void putnumber()
{
[Link]("rollno is" + rollno);
}
}
class test extends student
{
float m1,m2;
void getmarks(float m3,float m4)
{
m1 = m3;
m2 = m4;
}
void putmarks()
{
[Link]("The marks is");
[Link]("The marks of m1 = " + m1);
[Link]("The marks of m2 = " + m2);
}
}
interface sports
{
float sweight = 5.0f;
void putweight();
}
class result extends test implements sports
{
float total;
public void putweight()
{
[Link]("sports weight = " + sweight);
}
void display()
36
{
total = m1+m2+sweight;
putnumber();
putmarks();
putweight();
[Link]("total score =" + total);
}
}
class hybrid
{
public static void main(String args[])
{
result student1 = new result();
[Link](55);
[Link](93.0f,54.0f);
[Link]();
}
}
37
Output:
rollno is55
The marks is
The marks of m1 = 93.0
The marks of m2 = 54.0
sports weight = 5.0
total score =152.0
38
Prog – 19 : WAP in JAVA to throw our own exceptions
import [Link];
class myexception extends Exception
{
myexception(String message)
{
super(message);
}
}
class test2
{
public static void main(String args[])
{
int x = 5;
int y = 1000;
try
{
float z = (float)x/(float)y;
if(z<0.01)
{
throw new myexception("no. is too small");
}
}
catch(myexception e)
{
[Link]("caught my exception");
[Link]([Link]());
}
finally
{
[Link]("errors in programs");
}
}
}
39
Output:
caught my exception
no. is too small
errors in programs
40
Prog – 20 : WAP in JAVA for using try and catch for exception
handling
class exception
{
public static void main(String args[])
{
int a = 10;
int b = 5;
int c = 5;
int x,y;
try
{
x = a/(b-c);
}
catch(ArithmaticExceptoin e)
{
[Link]("division by zero");
}
y = a/(b+c);
[Link]("y = " + y);
}
}
41
Output:
division by zero
y=1
42
Prog – 21 : WAP in JAVA for multiple catch statement for
exception handling
class exc1
{
public static void main(String args[])
{
int a[]={5,10};
int b = 5;
try
{
int x = a[2]/(b-a[1]);
}
catch(ArithmeticException e)
{
[Link]("division by zero");
}
catch(ArrayIndexOutOfBoundsException e)
{
[Link]("Array index error");
}
catch(ArrayStoreException e)
{
[Link]("wrong data type");
}
int y = a[1]/a[0];
[Link](" y = " + y);
}
}
43
Output:
Array index error
y=2
44
Prog – 22: WAP in JAVA for runnable interface
class a implement Runnable
{
public void run()
{
for(int i=1;i<=10;i++)
{
[Link]("Thread a is =" + i);
}
[Link](" exit Thread a");
}
}
class test6
{
public static void main(String args[])
{
a runnable = new a();
Thread threada = new thread(runnable);
[Link]();
[Link](" exit of main Thread ");
}
}
45
Output:
exit of main Thread
Thread a is =1
Thread a is =2
Thread a is =3
Thread a is =4
Thread a is =5
Thread a is =6
Thread a is =7
Thread a is =8
Thread a is =9
Thread a is =10
exit Thread a
46
Prog – 23 : WAP in JAVA using thread methods
import [Link];
class a extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
if(i==1)
yield();
[Link]("the result of thread a" + i);
}
[Link]("Exit from a");
}
}
class b extends Thread
{
public void run()
{
for(int j=1;j<=5;j++)
{
if(j==3)
stop();
[Link]("the result of thread b" + j);
}
[Link]("Exit from b");
}
}
class c extends Thread
{
public void run()
{
for(int k=1;k<=5;k++)
{
if(k==1)
[Link]("the result of thread c" + k);
}
try
{
sleep(1000);
}
catch(Exception e)
47
{
}
[Link]("Exit from c");
}
}
class th1
{
public static void main(String args[])
{
a a1 = new a();
b b1 = new b();
c c1 = new c();
[Link]("Start from thread a");
[Link]();
[Link]("Start from thread b");
[Link]();
[Link]("Start from thread c");
[Link]();
}
}
48
Output:
Start from thread a
Start from thread b
Start from thread c
the result of thread b1
the result of thread b2
the result of thread a
the result of thread a
the result of thread c
the result of thread a
the result of thread a
the result of thread a
Exit from a
Exit from c
49
Prog – 24 : WAP in JAVA to set thread priorities on various
threads
import [Link];
class A extends Thread
{
public void run()
{
[Link]("threadA started");
for(int i=1;i<=4;i++)
{
[Link]("From Thread A : i = " + i);
}
[Link]("Exit from A");
}
}
class B extends Thread
{
[Link]("threadB started");
public void run()
{
for(int j=1;j<=4;j++)
{
[Link]("From Thread B : j = " + j);
}
[Link]("Exit from B");
}
}
class C extends Thread
{
[Link]("threadC started");
public void run()
{
for(int k=1;k<=4;k++)
{
[Link]("From Thread C : k = " + k);
}
[Link]("Exit from C");
}
}
class th8
{
public static void main(String args[])
50
{
A a1 = new A();
B b1 = new B();
C c1 = new C();
[Link](Thread.MAX_PRIORITY);
[Link]([Link]( ) + 1);
[Link](Thread.MIN_PRIORITY);
[Link]("Start thread A");
[Link]( );
[Link]("Start thread B ");
[Link]( );
[Link]("Start thread C ");
[Link]( );
[Link]("End of main thread ");
}
}
51
Output:
Start thread A
Start thread B
Start thread C
threadB started
From Thread B : j = 1
From Thread B : j = 2
threadC started
From Thread C : k = 1
From Thread C : k = 2
From Thread C : k = 3
From Thread C : k = 4
Exit from C
End of main thread
From Thread B : j = 3
From Thread B : j = 4
Exit from B
threadA started
From Thread A : i = 1
From Thread A : i = 2
From Thread A : i = 3
From Thread A : i = 4
Exit from A
52
Prog – 25 : WAP in JAVA to for creating applets
Notepad code-:
import [Link].*;
import [Link].*;
public class First extends applet
{
String str;
public void init()
{
str = get parameter("String");
if(str==null)
str = " Java";
str="hello" + str;
}
public void paint(Graphics g)
{
[Link](str,10,100);
}
}
HTML code -:
<HTML>
<Head>
<Title>
My Applet program
</Title>
</Head>
<Body>
<Applet
Code = [Link]
WIDTH= 400;
HEIGHT= 300;
< PARAM NAME = “String”
VALUE = “Applet”>
</Body>
</HTML>
53
Output :
54
Prog – 26 : WAP in JAVA for creating two packages and
import these packages into another program
package package1;
public class temp
{
public double t;
public void getdata(double t)
{
this.t= t;
}
public void tempmethod()
{
double f = ((1.8 * t) + 32) ;
[Link]("Temperature in degree fahernheit =" + f);
}
}
package package2;
public class recarea
{
public int l, b;
public void getdata(int l , int b)
{
this.l= l;
this.b= b;
}
public void areamethod()
{
int area = l*b;
[Link]("Area of rectangle is =" + area);
}
}
import [Link];
import [Link];
class areatemp
{
public static void main(String args[])
{
recarea obj1 = new recarea();
[Link](10,20);
55
[Link]();
temp obj2 new temp();
[Link](99);
[Link]();
}
}
56
Output :
Area of rectangle is =200
Temperature in degree fahernheit =98.60000000000001
57