Vous êtes sur la page 1sur 57

Prog – 1 : WAP in JAVA to find the greater of two numbers

import java.io.DataInputStream;
class greate
{
public static void main(String args[])
{
DataInputStream in=new DataInputStream(System.in);
int a=22,b=23;
try
{
System.out.println("enter the value of a ");
a=Integer.parseInt(in.readLine());
System.out.println("enter the value of b ");
b=Integer.parseInt(in.readLine());
}
catch(Exception e)
{
}
if(a>b)
{
System.out.println("a is greater");
}
else
System.out.println("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;
System.out.println(" the add is " + c );
float d = a - b;
System.out.println(" the sub is " + d );
float e = a*b;
System.out.println(" the mul is " + e );
float f = a/b;
System.out.println(" the div is " + f);
float g = a%b;
System.out.println(" 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);

System.out.println(" a = " + a );
System.out.println(" b = " + b );
System.out.println(" c = " + c );
}
}

5
Output :

a = 10.200001
b = 15.000001
c = 5.0

6
Prog – 4 : WAP in JAVA demonstrate nested if statement

import java.io.DataInputStream;
class nestedif
{
public static void main(String args[])
{
DataInputStream in = new DataInputStream(System.in);
int a=0 , b=0 , c=0;
try
{
System.out.println("enter the value of a , b & c:");
a = Integer.parseInt(in.readLine());
b = Integer.parseInt(in.readLine());
c = Integer.parseInt(in.readLine());

}
catch(Exception e)
{
}

if(a>b)
{
if(a>c)
System.out.println( a + " is greatest.");
else
System.out.println( b + " is greatest.");
}
else if(b>c)
System.out.println( b + " is greatest.");
else
System.out.println( 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 java.io.DataInputStream;
class operations
{
public static void main(String args[])
{
DataInputStream in = new DataInputStream(System.in);
int a=0,b=0;
int i=0;
int add=0 , sub=0 , mul = 0, div=0;
try
{
System.out.println("enter value of a and b :");
a = Integer.parseInt(in.readLine());
b = Integer.parseInt(in.readLine());
System.out.println("Enter the choice :");
System.out.println("1: Addition");
System.out.println("2: Subtraction");
System.out.println("3: Multiplication");
System.out.println("4: Division");
i = Integer.parseInt(in.readLine());
}
catch(Exception e)
{
}
switch(i)
{
case 1 :
add=a+b;
System.out.println("Addition = " + add);
break;

case 2 :
sub=a-b;
System.out.println("subtraction = " + sub);
break;

case 3 :
mul=a*b;
System.out.println("multiplication = " + mul);
break;

9
case 4 :
div=a/b;
System.out.println("Division = " + div);
break;

default :
System.out.println("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;
rect1.br = 5;
area1= rect1.l * rect1.br;
rect2.getdata(20 , 25);
area2 = rect2.rectarea();

System.out.println("area1 is " + area1);


System.out.println("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 = rect1.rectarea();
System.out.println("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 = stat.mul(4.0 , 5.0);
float b = stat.div(a , 2.0);
System.out.println(“ 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 = s.age;
fees = s.fees;
}
void display()
{
System.out.println("age =" + age);
System.out.println("fees = " + fees);
}
}
class program
{
public static void main(String args[])
{
student s1 = new student();
student s2 = new student();
s1.data();
s1.display();
s2.getdata(s1);
s2.display();
}
}

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 + s.age;
fees = fees + s.fees;
}
void display()
{
System.out.println("Age = " + age);

System.out.println("fees = " + fees);


}
}
class program1
{
public static void main(String args[])
{
student s1 = new student();
student s2 = new student();
s1.data(30,4569);
s1.display();
s1.getdata(s1);
s1.display();
}
}

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();
System.out.println(“The factorial = ” + f.fact(3));
System.out.println(“The factorial = ” + f.fact(4));
System.out.println(“The factorial = ” + f.fact(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()
{
System.out.println("value is" + x);
}
}
class sub extends super1
{
int y;
sub(int x,int y)
{
super(x);
this.y = y;
}
void display()
{
System.out.println("value is" + x);
System.out.println("value is" + y);
}
}

class test
{
public static void main(String args[])
{
sub s1 = new sub(10,12);
s1.display();
}
}

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;
System.out.println("the given array elements are:");
for(i =0;i<10;i++)
{
System.out.println(n[i]);
}
int sum = 0;
for(i =0;i<10;i++)
{
sum = sum + n[i];
}
System.out.println("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);
System.out.println("i = " + i );
System.out.println("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++)
{
System.out.println(n[i]);
}
for(i=0;i<n.length;i++)
{
if (n[i] == a)
{
flag = 1;
System.out.println("number is found");
break;
}
}
if(flag == 0)
{
System.out.println("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<a.length;i++)
{
for(int j=0;j<a[i].length;j++)
{
if(min>a[i][j])
{
min=a[i][j];
}
}
}
System.out.println("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;
System.out.println("Area of rectangle = " + area1.compute(5,10));
area1 = cir;
System.out.println("Area of circle = " + area1.compute(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()
{
System.out.println("rollno is" + rollno);
}
}
class test extends student
{
float m1,m2;
void getmarks(float m3,float m4)
{
m1 = m3;
m2 = m4;
}
void putmarks()
{
System.out.println("The marks is");
System.out.println("The marks of m1 = " + m1);
System.out.println("The marks of m2 = " + m2);
}
}
interface sports
{
float sweight = 5.0f;
void putweight();
}
class result extends test implements sports
{
float total;
public void putweight()
{
System.out.println("sports weight = " + sweight);
}
void display()

36
{
total = m1+m2+sweight;
putnumber();
putmarks();
putweight();
System.out.println("total score =" + total);
}
}
class hybrid
{
public static void main(String args[])
{
result student1 = new result();
student1.getnumber(55);
student1.getmarks(93.0f,54.0f);
student1.display();
}
}

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 java.lang.Exception;
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)
{
System.out.println("caught my exception");
System.out.println(e.getMessage());
}
finally
{
System.out.println("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)
{
System.out.println("division by zero");
}
y = a/(b+c);
System.out.println("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)
{
System.out.println("division by zero");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array index error");
}
catch(ArrayStoreException e)
{
System.out.println("wrong data type");
}
int y = a[1]/a[0];
System.out.println(" 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++)
{
System.out.println("Thread a is =" + i);
}
System.out.println(" exit Thread a");
}
}
class test6
{
public static void main(String args[])
{
a runnable = new a();
Thread threada = new thread(runnable);
Threada.start();
System.out.println(" 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 java.lang.Thread;
class a extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
if(i==1)
yield();
System.out.println("the result of thread a" + i);
}
System.out.println("Exit from a");
}
}
class b extends Thread
{
public void run()
{
for(int j=1;j<=5;j++)
{
if(j==3)
stop();
System.out.println("the result of thread b" + j);
}
System.out.println("Exit from b");
}
}
class c extends Thread
{
public void run()
{
for(int k=1;k<=5;k++)
{
if(k==1)
System.out.println("the result of thread c" + k);
}
try
{
sleep(1000);
}
catch(Exception e)

47
{
}
System.out.println("Exit from c");
}
}
class th1
{
public static void main(String args[])
{
a a1 = new a();
b b1 = new b();
c c1 = new c();
System.out.println("Start from thread a");
a1.start();
System.out.println("Start from thread b");
b1.start();
System.out.println("Start from thread c");
c1.start();
}
}

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 java.lang.Thread;
class A extends Thread
{
public void run()
{
System.out.println("threadA started");
for(int i=1;i<=4;i++)
{
System.out.println("From Thread A : i = " + i);
}
System.out.println("Exit from A");
}
}
class B extends Thread
{
System.out.println("threadB started");
public void run()
{
for(int j=1;j<=4;j++)
{
System.out.println("From Thread B : j = " + j);
}
System.out.println("Exit from B");
}
}
class C extends Thread
{
System.out.println("threadC started");
public void run()
{
for(int k=1;k<=4;k++)
{
System.out.println("From Thread C : k = " + k);
}
System.out.println("Exit from C");
}
}
class th8
{
public static void main(String args[])

50
{
A a1 = new A();
B b1 = new B();
C c1 = new C();
c1.setPriority(Thread.MAX_PRIORITY);
b1.setPriority(a1.getPriority( ) + 1);
a1.setPriority(Thread.MIN_PRIORITY);
System.out.println("Start thread A");
a1.start( );
System.out.println("Start thread B ");
b1.start( );
System.out.println("Start thread C ");
c1.start( );
System.out.println("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 java.awt.*;
import java.applet.*;
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)
{
g.drawString(str,10,100);
}
}
HTML code -:

<HTML>
<Head>
<Title>
My Applet program
</Title>
</Head>
<Body>
<Applet
Code = First.class
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) ;
System.out.println("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;
System.out.println("Area of rectangle is =" + area);
}
}

import package1.temp;
import package2.recarea;
class areatemp
{
public static void main(String args[])
{
recarea obj1 = new recarea();
obj1.getdata(10,20);

55
obj1.areamethod();
temp obj2 new temp();
obj2.getdata(99);
obj2.tempmethod();
}
}

56
Output :

Area of rectangle is =200


Temperature in degree fahernheit =98.60000000000001

57

Vous aimerez peut-être aussi