[go: up one dir, main page]

0% found this document useful (0 votes)
16 views68 pages

Java Pro 1 To 48

The document provides a series of Java programming exercises, each demonstrating different programming concepts such as calculating the area of a circle, finding factorials, using control statements, and implementing object-oriented principles like inheritance and method overloading. Each exercise includes a code snippet and its expected output. The exercises range from basic operations to more complex structures, showcasing a variety of Java functionalities.

Uploaded by

jaydipsinh7285
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views68 pages

Java Pro 1 To 48

The document provides a series of Java programming exercises, each demonstrating different programming concepts such as calculating the area of a circle, finding factorials, using control statements, and implementing object-oriented principles like inheritance and method overloading. Each exercise includes a code snippet and its expected output. The exercises range from basic operations to more complex structures, showcasing a variety of Java functionalities.

Uploaded by

jaydipsinh7285
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 68

JAVA PROGRAM 1 TO 48

1) write a java program for find the area of circle

class area
{
public static void main(String args[])
{
double pi,r,a;
pi=3.1416;
r=5;
a=pi*r*r;
system.out.prinln("this is the area of circle->"+a);
}
}

OUTPUT--->
Area of circle=78.5

2>write a java program tht will display factorial of given number

class factorial
{
public static void main(String args[])
{
int num=5;
int result=1;
while(num>0)
{
result=result*num;
num--;
}
System.out.println("factorial of given no::"+result);
}
}

OUTPUT--->
factorial of given no:120

3>write a program that will find the largest no from the given two nos

class pro3
{
public static void main(String args[])
{
int a=52,b=35;
if(a<b)
{
System.out.prinln("the largest a=>"+a);
}
else
{
System.out.println("the largest b=>"+b);
}
}
}
output is ::-->
the largest b=35

4> write a java program that will find the largest no of given 3 nos

class pro4
{
public static void main(String args[])
{
int a=315,b=712,c=478;
System.out.println("largest value is:");

if(a>b)
{
if(a>c)
{
System.out.println("a=>"+a);
}
else
{
System.out.println("c=>"+c);
}
}
else
{
if(c>b)
{
System.out.println("c=>"+c);
}
else
{
System.out.println("b=>"+b);
}
}
}

output--->
largest value is:712

5>write a java program to show the use of switch statement

class pro5
{
public static void main(String args[])
{
char choice;
System.out.println("select your choice");
System.out.println("m-> madras");
System.out.println("b->bombay");
System.out.println("c->calcuta");
System.out.println("choice--->");

System.out.flush ();
try
{
switch(choice=(char)System.in.read())
{
case'M':
case'm':System.out.println("madras:booket 5");
break;
case'B':
case'b': System.out.println("bombay:booklet 9");
break;
case 'C':
case'c':System.out.println("calcuta:booklet 5");
break;
default:System.out.println("invalid choice(ic)");
}
}
catch(Exception e)
{
System.out.println("i/o Error");
}
}
}

output--->
select your choice
m->madras
b->bombay
c->calcuta
choice->>>m
madras:booklet 5

pro6>write a java program to find sum of digit given number

import java.util.*;
class pro6
{
public static void main(String args[])
{
int sum=0;
System.out.println("enter multidigit number:");
Scanner.input=new scanner(System.in);
int n=input.nextint();
int t=n;
while(n>0)
{
int p=n%10;
sum=sum+p;
n=n/10;

}
System.out.println("sum of digit"+t+"is:"+sum);
}
}
OUTPUT
enter multidigit number:456
sum of digit 456 is 15

7> write a java program that will display the sum of 1+1\2+1\3.....+1\n.

class pro7
{
public static void main(String args[])
{
double i,j;
double sum=0;
double num=3;
for(i=1;i<=num;i++)
{
j=1/i;
sum=sum+j;
}
System.out.println("\n\n sum of series is:--"+sum);
}
}

output
sum of series is-->1.8333333333333
8> write a java program that check weather the given no is prime or not

class pro8
{
public static void main(String args[])
{
int i,j=0,a=0,k=1;
for(i=1;i<=100;i++)
{
a=0;
for(j=2;j<=i-1;j++)
{
if(i%j==0)
{
a=1;
}
}
if(a!=1 && k<=25)
{
System.out.println(" this is the \t"+k+"\t prime number->"+i);
k=k+1;
}
}
}
}

output::--
this is 1 prime number=1
this is 2 prime number=2
this is 3 prime number=3

9>write a java program that implements the use of break statement

class pro9
{
public static void main(String args[])
{
for(int i=0;i<=10;i++)
{
if(i==5)
{
break;
}
System.out.println("BreakLoop"+i);
}
}
}

output:-
breakloop:0
breakloop:1
braekloop:2
breakloop:3
breakloop:4
10>write a java program that implements the use of continue statement

class pro10
{
public static void main(String args[])
{
for(int i=0;i<=10;i++)
{
if(i==5)
{
continue;
}
System.out.println("breakloop:"+i);
}
}
}

output:-
breakloop:0
breakloop:1
breakloop:2
breakloop:3
breakloop:4
breakloop:5
breakloop:6
breakloop:7
breakloop:8
breakloop:9
breakloop:10

11>write a java program that will accept command line arguments and display the same

import java.io.*;
class pro11
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str;
str=br.readLine();
System.out.println("this is your string:->"+str);
}
}

output
ABCD
this is your string:->ABCD

12>write a java program to sort the elements of an array in asscending order.

import java.io.*;
class pro12
{
public static void main(String args[]) throws IOException
{

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

System.out.println("how many element you want to enter::->");


int n=Integer.parseInt(br.readLine());
int arr[]=new int[n];

for(int i=0;i<n;i++)
{
System.out.print("enter integer::->")
arr[i]=Integer.parseInt(br.readLine());
}
int limit=n-1;
for(int i=0;i<limit;i++)
{
for(int j=0;j<limit-1;j++)
{
if(arr[j]<arr[j+1])
{
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}

System.out.println("these are ascending number:-")


for(int i=0;i<n;i++)
{
System.out.println(arr[i]);
}
}
}

output::-RUN NBUT NOT IN ASCENDING


how many element you want to sort?->>
4
enter int::->
12
enter int::->
32
enter int::->
1
enter int::->
34
this are ascending number:-
1
12
32
34

13>write a java program to create a student class and generate result of student

class student
{
int rollno;
void getno(int a )
{
rollno=a;
}
void putno()
{
System.out.println("rollno:"+rollno);
}
}
class test extends student
{
float sub1,sub2;
void getmark(int a,int b)
{
sub1=a;
sub2=b;
}
void putmark()
{
System.out.println("subject1"+sub1);
System.out.println("subject2"+sub2);
}
}
class result extends test
{
double total;
float per;
void display()
{
putno();
putmark();
int M=200;
total=sub1+sub2;
System.out.println("total--->"+total);
per=100*(sub1+sub2)/M;
System.out.println("percentage-"+per);
if (per>=40)
{
System.out.println("dist");
}
else if(per>=70)
{
System.out.println("first");
}
else if(per>=60)
{
System.out.println("second");
}
else if(per>=35)
{
System.out.println("third");
}
else
{
System.out.println("fail");
}
}
}
class pro13
{
public static void main(String args[])
{
result r=new result();
r.getno(1);
r.getmark(80,79);
r.display();
}
}

output
rollno:1
subject180.0
subject279.0
total--->159.0
percentage-79.5
dist

14>write a java program to create an employee class and generate slalary slip for the employee

class employee
{
double basic,da,hra,ma,pf,gross,net;
void salary()
{
basic c=2000;
da=basic*0.10;
hra=basic*0.75;
ma=200;
pf=basic*0.125;
gross=basic+da+hra+ma;
net=gross-pf;
}
void display()
{
System.out.println(basic+"\t\t"+da+"\t"+hra);
System.out.println(ma+"\t"+pf+"\t"+gross+"\t"+net);
}
}
class pro14
{
public static void main(String args[])
{
employee e=new employee();
System.out.println("basic\t da\t hra\t ma\t");
System.out.println("pf\t gross \t net \t");
System.out.println("---------------\n");
e.salary();
e.display();
}
}

output
basic-2000
da-3000
hra-3000
ma-50
pf-1000
net -600
gross salary-24400

15>write a java program which shows the use of Static member

class mathoperation
{
static float mul(float x,float y)
{
return x*y;
}
static float divide(float x,float y)
{
return x/y;
}
}
class pro15
{
public static void main(String args[])
{
float a=mathoperation.mul(4.0,5.0);
float b=mathoperation.divide(a,2.0);
System.out.println("b="+b);
}
}
output----> if this program not rum in float then write double behalf of float
b=10.0

16>write a java program which shows nesting of method

class nesting
{
int m,n;
nesting(int x,int y) //constructor method
{
m=x;
n=y;
}
int largest()
{
if(m>=n)
return m;
else
return n;
}
void display()
{
int large=largest();//call method and store result in variable
System.out.println("largest value is==>>"+large);
}
}
class pro16
{
public static void main(String args[])
{
nesting p1=new nesting(14,16);
}
}

OUTPUT---->>>
largest value is==>>16

17> write a java program which show the use of method overloading

class overloaddemo
{
void test()
{
System.out.println("no parameter");
}

void test(int a)
{
System.out.println("a:"+a);
}

void test(int a,int b)


{
System.out.println("a and b:"+a+""+b);
}

double test(double a)
{
System.out.println("double a:"+a);
return a*a;
}
}

class pro17
{
public static void main(String arge[])
{
overloaddemo ob=new overloaddemo();
double result;
ob.test();
ob.test(10);ob.test(10,20);
result=ob.test(123.2);
System.out.println("result of ob.test(123.2):"+result);
}
}

output--->
no parameter
a:10
a and b:1020
double a:123.2
result of ob.test(123.2):15178.240000000002
18> write a java program which implements the defaults constructer

class imple
{
int length;
int breath;
imple()
{
length=0;
breath=0;
}
void area()
{
int area;
area=2*(length+breath);
System.out.println("the area of circle:"+area);
}
}
class pro18
{
public static void main(String args[])
{
imple i1=new imple();
i1.area();
}
}

OUTPUT--->>
the area of circle:0
pro19>write a java program which implements the parameterized constructor

class opereation
{
int length;
int breath;
operation(int x,int y)
{
length=x;
breath=y;
}
void area()
{
int area;
area=2*(length+breath);
System.out.println("the area of circle:-+area);
}
}
class pro19
{
public static void main(String args[])
{
operation p1=new operation(5,7);
p1.area();
}
}
OUTPUT--->>
the area of circle:24

pro20> write a java program which implements the overloading of constructor

class ABC
{
int length;
int breath;

ABC()
{
length=0;
breath=0;
}
ABC(int x,int y)
{
length=x;
breath=y;
}
ABC(int z)
{
length=breath=z;
}
void area()
{
int area1;
area1=2*(length+breath);
System.out.println("the area of the circle:"+area1);
}
}
class pro20
{
public static void main(String args[])
{
ABC A1=new ABC();
ABC A2=new ABC(4,3);
ABC A3=new ABC(8);
A1.area();
A2.area();
A3.area();
}
}

OUTPUT==>>
the area of the circle:0
the area of the circle:14
the area of the circle:32

pro21>>write a java program which explain the concept of single inheritence

// a simple example of inheritance


// create a superclass
class A
{
int i,j;
void showij()
{
System.out.println("i and j: " +i+ " "+j);
}
}
// create a subclass by extending class A.
class B extends A
{
int k;
void showk()
{
System.out.println("k:"+k);
}
void sum()
{
System.out.println("i+j+k:" + (i+j+k));
}
}
class pro21
{
public static void main(String args[])
{
A superOb=new A();
B subOb=new B();
//the superclass may be used by itself.
superOb.i=10;
superOb.j=20;
System.out.println("contants of superOb:");
superOb.showij();
System.out.println();
/*the subclass has access to all public member of its superclass*/
subOb.i=7;
subOb.j=8;
subOb.k=9;
System.out.println("contents of subOb:");
subOb.showij();
subOb.showk();
System.out.println();
System.out.println("sum of i,j and k in subOb:");
subOb.sum();
}
}

OUTPUT--->>
contants of superOb:
i and j: 10 20

contents of subOb:
i and j: 7 8
k:9

sum of i,j and k in subOb:


i+j+k:24

pro22> write a java program which explains the concept of multilevel inheritance

class student
{
int rollno;
void getno(int n)
{
rollno=n;
}
void putno()
{
System.out.println("rollno=>"+rollno);
}
}
class test extends student
{
float sub1;
float sub2;
void getmark(int s1,int s2)
{
sub1=s1;
sub2=s2;
}
void putmark()
{
System.out.println("sub1=>"+sub1);
System.out.println("sub2=>"+sub2);
}
}
class result extends test
{
float total;
void display()
{
total=sub1+sub2;
putno();
putmark();
System.out.println("total marks=>"+total);
}
}
class pro22
{
public static void main(String args[])
{
result r1=new result();
r1.getno(111);
r1.getmark(44,30);
r1.display();
}
}

OUTPUT-->>
rollno=>111
sub1=>44.0
sub2=>30.0
total marks=>74.0
pro23> write a program which explains the concept of Hierachical inharitance

class A
{
void sum(int n,int m)
{
System.out.println("sum="+(n+m));
}
}

class B extends A
{
void sub(int x,int y)
{
System.out.println("sub="+(x-y));
}
}
class C extends A
{
void mul(int a,int b)
{
System.out.println("mul="+(a*b));
}
}
class pro23
{
public static void main(String args[])
{
B b1=new B();
b1.sum(35,55);
b1.sub(90,30);
C c1=new C();
c1.sum(155,255);
c1.mul(5,5);
}
}

OUTPUT-->>
sum=90
sub=60
sum=410
mul=25

pro24>>write a java program which show the method overriding

//method overriding
class A
{
int i,j;
A(int a,int b)
{
i=a;
j=b;
}
// display i and j
void show()
{
System.out.println("i and j:"+ i+ ""+j);
}
}
class B extends A
{
int k;
B(int a,int b, int c)
{
super(a,b);
k=c;
}
//display k- this is overrided show() in A
void show()
{
System.out.println("k:"+k);
}
}
class pro24
{
public static void main(String args[])
{
B subOb=new B(1,2,3);
subOb.show(); //this calls show() in B
}
}
OUTPUT-->>
k:3

pro25>> write a java program to implements final class and final method.

final class A
{
int a;
int b;
final void getno(int x,int y)
{
a=x;
b=y;
}
void display()
{
System.out.println("a="+a);
System.out.println("b="+b);
}
}
class B
{
int c;
int d;
void putno(int x,int y)
{
c=x;
d=y;
}
void display()
{
System.out.println("c=>"+c);
System.out.println("d=>"+d);
}
}
class pro25
{
public static void main(String args[])
{
A a1=new A();
a1.getno(10,20);
a1.display();
}
}

OUTPUT-->>
a=10
b=20

pro26>> write a java program to implementation abstarct class and absract method

abstract class Bank


{
abstract int getRateOfInterest();
}
class SBI extends Bank
{
int getRateOfInterest()
{return 7;}
}
class PNB extends Bank
{
int getRateOfInterest(){return 8;}
}

class pro26
{
public static void main(String args[])
{
Bank b;
b=new SBI();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
b=new PNB();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
}
}

output--->>

Rate of Interest is: 7 %


Rate of Interest is: 8 %
pro27>>write a java program implements interface

interface callback
{
void callback(int param);
}
class client implements callback
{
//implements callback's interface
public void callback(int p)
{
System.out.println("callback called with "+p);
}
}
class pro27
{
public static void main(String agrs[])
{
callback c =new client();
c.callback(42);
}
}

OUTPUT->
callback called with 42
pro28:--write a java program for multiple interface
class student
{
int rollno;
void getno(int n)
{
rollno=n;
}
void putno()
{
System .out.println("rollno:"+rollno);
}
}

class test extends student


{
float part1,part2;
void getmark(float n1,float n2)
{
part1=n1;
part2=n2;
}
void putmark()
{
System.out.println("marks obtained");
System.out.println("part1="+part1);
System.out.println("part2="+part2);
}
}
interface sports
{
float sportwt=6.0f;
void putwt();
}

class result extends test implements sports


{
float total;
public void putwt()
{
System.out.println("sportwt="+sportwt);
}
void display()
{
total=part1+part2+sportwt;
putno();
putmark();
putwt();
System.out.println("total score ="+total);
}
}
class pro28
{
public static void main(String args[])
{
result student1=new result();
student1.getno(1234);
student1.getmark(27.5f,33.0f);
student1.display();
}
}

OUTPUT-->>
rollno:1234
marks obtained
part1=27.5
part2=33.0
sportwt=6.0
total score =66.5

pro29:- write a java program shoe importing of class from other packegr
--->first step: write this program given bellow
second step :save this program in package1 folder jo apke folder k ander new folder banana hai

package package1;
public class bca
{
public void display_bca()
{
system.out.println("bca class");
}
}

first step: write this program given bellow


second step :save this program in package2 folder jo apke folder k ander new folder banana hai

package package2;
public class mca
{
public void display_mca()
{
System.ou.println("mca class");
}
}

in this third you write this program


save in your folder bt named pro29

import package1.bca;
import package2.*;
class pro29
{
public static void main(String agrs[])
{
bca b1=new bca();
mca m1=new mca();
b1.display_bca();
m1.display_mca();
}
}

OUTPUT-->
bca class
mca class
pro30:- write a java pro to implement method of math class
class pro30
{
public static void main(String args[])
{
double x=10;
double y=35;

System.out.println("the max no--"+Math.max(x,y));


System.out.println("the sqrt no--"+Math.sqrt(x));
System.out.println("the abs no--"+Math.abs(x));
System.out.println("the min no--"+Math.min(x,y));
}
}

OUTPUT
the max no--35.0
the sqrt no--3.1622776601683795
the abs no--10.0
the min no--10.0

pro31==>> write a java implement method of string class

class pro31
{
static string name[]={"madrass","delhi","ahmedabad",calcuta","bombat"};
public static void main(String args[])
{
int size=name.length;
string temp =null;
for (int i=0;i<size;i++)
{
for(int j=i+1;j<size;j++)
{
if(name[j].compare to(name[i])<0)
{
temp=name[i];
name[i]=name[j];
name[j]=temp;
}
}
}
for(int i=0;i<size;i++)
{
System.out.println(name[i]);
}
}
}

OUTPUT-->
ahmedabad
bombat
calcuta
delhi
madrass
pro32->> write a java program implements vector class

import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.*;

public class pro32


{

public static void main(String args[])


{

Vector list = new Vector();


int len=args.length;

for(int i=0;i<len;i++)
{

list.addElement(args[i]);
}

int size=list.size();
String str[]= new String[size];
list.copyInto(str);
for(int i=0;i<size;i++) {

System.out.println ("Element of Vector at position "+i+":"+str[i]);


}
}
}
Output-->
Run as java pro32 12 nick a 15.2
output ->
Element of Vector at position 0:12
Element of Vector at position 1:nick
Element of Vector at position 2:a
Element of Vector at position 3:15.2

pro33>>>write a java program method of stack class

import java.util.Stack;
public class pro33
{
public static void main(String args[])
{
Stack mystack=new Stack();
mystack.push(new Integer(10));
mystack.push(new Integer(20));
Integer stksum1=(Integer) mystack.pop();
Integer stksum2=(Integer) mystack.pop();
int stksum=stksum1.intValue()+stksum2.intValue();
System.out.println(stksum);
}
}

OUTPUT-->>
30
pro34>>write a java program which will read text and count all occurrence perticular word

import java.util.*;
import java.io.*;
import java.lang.*;

public class StringCount


{
public static void main(String args[]) throws IOException
{
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter String:--->");
String base =br1.readLine();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter SubString which you want to count-->");
String searchFor=br.readLine();
int len=searchFor.length();
int result =0;
if(len > 0)
{
int start=base.indexOf(searchFor);
while(start!=-1)
{
result++;
start=base.indexOf(searchFor,start+len);
}
}
System.out.println(result);
}
}

OUTPUT-->.
enter String:--->
NICK
enter SubString which you want to count-->
NIKS
0

pro35>>Write a Java Program which will read a string and rewrite it in the alphabetical order
eg. The word “STRING” should be written a “GINRST”.

import java.io.*;

class pro35
{
public static void main(String args[]) throws IOException
{
String str;
char s[];
DataInputStream in = new DataInputStream(System.in);
System.out.println("enter string:");
str=in.readLine();
s=str.toCharArray();//convert String in to character array
for(int i=0;i<str.length();i++)
{
for(int j=0;j<str.length();j++)
{
if(s[i]<=s[j])
{
char temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
}
System.out.println("after sorting the string.....");
System.out.println(s);
}
}

OUTPUT--
enter string:
shruti
after sorting the string.....
hirstu

pro36>>write a java program that create a thread using thread class

class A extends Thread


{
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println("\t form ThreadA: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("\t from Thread B:j="+j);
}
System.out.println("exit from B");
}
}

class C extends Thread


{
public void run()
{
for(int k=1;k<=5;k++)
{
System.out.println("\t from Tread C:k="+k);
}
System.out.println("exit form C");
}
}

class pro36
{
public static void main(String args[])
{
new A().start();
new B().start();
new C().start();
}
}
OUTPUT-->
form ThreadA:i= 1
from Thread B:j=1
form ThreadA:i= 2
from Thread B:j=2
from Tread C:k=1
form ThreadA:i= 3
from Thread B:j=3
from Tread C:k=2
form ThreadA:i= 4
from Thread B:j=4
from Tread C:k=3
form ThreadA:i= 5
from Thread B:j=5
from Tread C:k=4
exit from A
exit from B
from Tread C:k=5
exit form C

pro37>>Write a java program which shows the use of yield(), stop() and sleep() Methods.

class A extends Thread


{
public void run()
{
for(int i=1;i<=5;i++)
{
if(i==1)yield();
System.out.println("\t from Thread 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("\t from Thread 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("\t from Thread C:k="+k);
if(k==1)
try
{
sleep(1000);
}
catch(Exception e)
{
}
}
System.out.println("Exit from C");

}
}

class pro37
{
public static void main(String args[])
{
A threadA= new A();
B threadB= new B();
C threadC= new C();

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

OUTPUT-->>
start thread A
start thread B
from Thread A : i = 1
start thread C
from Thread B:j=1
from Thread A : i = 2
end of main thread
from Thread C:k=1
from Thread B:j=2
from Thread A : i = 3
from Thread B:j=3
from Thread A : i = 4
from Thread A : i = 5
exit from A
from Thread C:k=2
from Thread C:k=3
from Thread C:k=4
from Thread C:k=5
Exit from C

pro38>>.Write a java program which shows the Priority in Threads.

class A extends Thread


{
public void run()
{
System.out.println("threadA started");
for(int i=1; i<=4;i++)
{
System.out.println("\t from ThreadA :i="+i);
}
System.out.println("exit from A");
}
}
class B extends Thread
{
public void run()
{
System.out.println("threadB started");
for(int j=1; j<=4;j++)
{
System.out.println("\t from ThreadB:j="+j);
}
System.out.println("exit from B");
}
}
class C extends Thread
{
public void run()
{
System.out.println("threadC started");
for(int k=1; k<=4;k++)
{
System.out.println("\t from ThreadC:k="+k);
}
System.out.println("exit from C");
}
}
class pro38
{
public static void main(String args[])
{
A threadA=new A();
B threadB=new B();
C threadC=new C();
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 main thread");
}
}

OUTPUT-->
start thread A
start thread B
threadA started
start thread C
threadB started
from ThreadA :i=1
end of main thread
from ThreadB:j=1
threadC started
from ThreadA :i=2
from ThreadB:j=2
from ThreadC:k=1
from ThreadA :i=3
from ThreadB:j=3
from ThreadC:k=2
from ThreadA :i=4
from ThreadB:j=4
from ThreadC:k=3
exit from A
exit from B
from ThreadC:k=4
exit from C

PRO39>>write a java program which use of runable interface

class A implements Runnable


{
public void run()
{
for (int i=1;i<=10;i++)
{
System.out.println("\t ThreadA" +i);
}
System.out.println("end of ThreadA");
}
}

class pro39
{
public static void main(String args[])
{
A runable=new A();
Thread threadA=new Thread(runable);
threadA.start();
System.out.println("end of main Thread");
}
}

OUTPUT-->

end of main Thread


ThreadA1
ThreadA2
ThreadA3
ThreadA4
ThreadA5
ThreadA6
ThreadA7
ThreadA8
ThreadA9
ThreadA10
end of ThreadA

PRO40>>write a java program which use try and catch for exeption handling
class pro40
{
public static void main(String args[])
{
int a=10;
int b=5;
int c =5;
int x,y;
try
{
x=a/(b-c);
}
catch(ArithmeticException e)
{
System.out.println("divided by zero");
}
y=a/(b+c);
System.out.println("y="+y);
}
}

OUTPUT-->
divided by zero
y=1

PRO41>>write a java program which uses multiple catch block

class pro41
{
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);
}
}

OUTPUT-->>
array index error
y=2
PRO42>>write a java program which uses finaly statement
import java.lang.Exception;
class MyException extends Exception
{
MyException(String message)
{
super(message);
}
}

class pro42
{
public static void main(String args[])
{
int x=5,y=1000;
try
{
float z=(float) x /(float) y;
if(z<0.01)
{
throw new MyException("number is too small");
}
}
catch(MyException e)
{
System.out.println("caught my exception");
System.out.println(e.getMessage());
}
finally
{
System.out.println("I am always here");
}
}
}

OUTPUT-->
caught my exception
number is too small
I am always here

PRO43>>write a java program which uses nested try statement


class pro43
{
public static void main(String args[])
{
try
{
int a=2,b=4,c=2,x=7,z;
int p[]={2};
p[3]=33;
try
{
z=x/((b*b)-(4*a*c));
System.out.println("the value of z is = "+z);
}
catch(ArithmeticException e)
{
System.out.println("Division by zero in Arithmetic expression ");
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("array index is out-of-bounds");
}
}
}

OUTPUT--->>
array index is out-of-bounds

PRO44>>write a java program which shows throwing our own exception

import java.lang.Exception;
class MyException extends Exception
{
MyException(String message)
{
super(message);
}
}

class pro44
{
public static void main(String args[])
{
int x=5,y=1000;
try
{
float z=(float) x /(float) y;
if(z<0.01)
{
throw new MyException("number is too small");
}
}
catch(MyException e)
{
System.out.println("caught my exception");
System.out.println(e.getMessage());
}
finally
{
System.out.println("I am always here");
}
}
}

OUTPUT-->
caught my exception
number is too small
I am always here

PRO45>>create an applet program that print hello applet


import java.awt.*;
import java.applet.*;
public class pro45 extends Applet
{
public void paint (Graphics g)
{
g.drawString ("Hello Java",10,100);
}
}

<HTML>
<HEAD>
<BODY>
<APPLET CODE=pro45.class
WIDTH=400
HEIGHT=200>
</APPLET>
</BODY>
</HEAD>
</HTML>

OUTPUT-->>
hello java

PRO46>>create an Applet that use init(),start(),stop(),and distroy() method of applet

import java.awt.*;
import java.applet.*;
public class pro46 extends Applet
{
String str="";
public void init()
{
str=str+"init";
}
public void start()
{
str=str+"start";
}
public void stop()
{
str=str+"stop";
}
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.drawString(str,10,100);
}
}

<html>
<head>
<body>
<applet code=pro46.class
width=400
height=200>
</applet>
</body>
</head>
</html>
PRO47>>write an applet program to implemets the concept of passing parameter to applet

import java.awt.*;
import java.applet.*;
public class pro47 extends Applet
{
String str;
public void init()
{
str=getParameter("string");
if(str == null)
str ="Java";
str="hello"+str;
}
public void paint (Graphics g)
{
g.drawString(str,10,100);
}
}

<html>
<!parameterized html file>
<head>
<title> welcome to java applets</title>
<head>
<body>
<applet code =pro47.class
width=400
height=200>
<parameter name="string"
value="Applet!">
</applet>
</body>
</html>

OUTPUT-->>
HELLO java

PRO48>>write a applet program to implement various method of Graphics class


<html>
<body>
<applet code=pro48.class width=200 height=200>
</applet>
</body>
</html>

import java.awt.*;
import java.applet.*;
public class pro48 extends Applet
{
String s=new String();
String s1=new String();
String s2=new String();
Font f1=new Font("courier New",Font.BOLD,20);
public void paint(Graphics GA)
{
GA.setFont(f1);
GA.setColor(Color.blue);
GA.drawString("Illustration of method of graphics class ",200,250);
Font f2=GA.getFont();
s=f2.toString();
GA.drawString(s,5,540);
GA.setColor(Color.green);
Color col=GA.getColor();
s2=col.toString();
GA.drawString(s2,5,560);
GA.fillRect(500,15,70,90);
GA.drawRect(160,5,60,60);
GA.drawOval(10,120,155,95);
GA.setColor(Color.yellow);
GA.fillOval(700,140,50,150);
GA.setColor(Color.black);
GA.drawLine(380,100,200,180);
GA.drawArc(400,150,180,280,90,70);
int x2[]={200,120,280,240};
int z2=4,y2[]={260,370,370,270};
GA.setColor(Color.red);
GA.fillPolygon(x2,y2,z2);
GA.setColor(Color.red);
GA.drawRect(15,15,30,50);
FontMetrics f3=GA.getFontMetrics();
s1=f3.toString();
GA.drawString(s1,5,580);
GA.setColor(Color.magenta);
GA.fillRoundRect(510,400,90,80,20,20);
}
}

You might also like