JAVA PRO 1 TO 48
JAVA PRO 1 TO 48
JAVA PRO 1 TO 48
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
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
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
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
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
import java.io.*;
class pro12
{
public static void main(String args[]) throws IOException
{
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;
}
}
}
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
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
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);
}
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
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
OUTPUT--->>
contants of superOb:
i and j: 10 20
contents of subOb:
i and j: 7 8
k:9
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
//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
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--->>
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);
}
}
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");
}
}
package package2;
public class mca
{
public void display_mca()
{
System.ou.println("mca class");
}
}
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;
OUTPUT
the max no--35.0
the sqrt no--3.1622776601683795
the abs no--10.0
the min no--10.0
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.*;
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++) {
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.*;
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
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 pro37
{
public static void main(String args[])
{
A threadA= new A();
B threadB= new B();
C threadC= new C();
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
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
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-->
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
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
OUTPUT--->>
array index is out-of-bounds
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
<HTML>
<HEAD>
<BODY>
<APPLET CODE=pro45.class
WIDTH=400
HEIGHT=200>
</APPLET>
</BODY>
</HEAD>
</HTML>
OUTPUT-->>
hello java
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
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);
}
}