You are on page 1of 41

DATE:5-7-12 EXPT NO:1

Display
ALGORITHM: Step 1: Start Step 2:Print message Step 3:Stop

Program:
class display { public static void main(String args[]) { System.out.println("welcome"); } } OUTPUT: E:\ITCE>javac display.java E:\ITCE>java display welcome

Result: Thus program is

executed

DATE:5-7-12 EXPT NO:2

Temp conversion
ALGORITHM: Step 1: Start Step 2:f=(9/5)c+32 Step 3:Stop Program: import java.io.*; class temperat1 { public static void main(String args[])throws IOException { double F,C; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); C=Double.parseDouble(br.readLine()); F=((9/5)*C)+32; System.out.println("The temp in farenheit is:"+F); } } OUTPUT: E:\ITCE>javac temperat1.java E:\ITCE>java temperat1 5 The temp in farenheit is:41 DATE: 5-7-12 EXPT NO:3

Result: Thus program is

executed

AREA OF CIRCLE
ALGORITHM: Step 1: Start Step 2:c=3.14*r*r Step 3Print c Step 4:Stop PROGRAM: import java.io.*; class AREA { public static void main(String args[])throws IOException { double r,c; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); r=Double.parseDouble(br.readLine()); c=3.14*r*r; System.out.println("The Area of circle is"+c); } } OUTPUT: E:\ITCE>javac AREA.java E:\ITCE>java AREA 2 The Area of circle is 12.56

Result: Thus program is

executed

DATE: 5-7-12 EXPT NO:3

Sum of two numbers


ALGORITHM: 1. Start 2. Initialize a,b,c 3. Calculate c=a+b 4. Print c 5. Stop Program: class arith { public static void main(String args[]) { int a,b,c; a=5; b=6; c=a+b; System.out.println("c is"+c); } } OUTPUT: E:\ITCE>javac arith.java E:\ITCE>java Arith C=11 DATE: 5-7-12 EXPT NO:4

Result: Thus program is

executed

Sum of numbers by taking value from user


ALGORITHM 1. Start 2. Initialize a,b,c 3. Read a,b from user 4. Calculate c=a+b 5. Print c 6. Stop Program: import java.io.*; class arith { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); float a,b; System.out.println("enter number"); a=Float.parseFloat(br.readLine()); b=Float.parseFloat(br.readLine()); c=a+b; System.out.println(sum is"+c); } } OUTPUT: E:\ITCE>javac arith1.java E:\ITCE>java ARith Enter 34 sum is=7

Result: Thus program is

executed

DATE: 5-7-12 EXPT NO:5

To perform arithmetic expression


ALGORITHM: 1. Start 2. Initialize a,b,c,d,e,f 3. Read Input 4. Calculate c=a+b, d=a-b, e=a*b, f=a/b 5. Print c,d,e,f 6. Stop Program: import java.io.*; class arith { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); float a,b,c,d,e,f; System.out.println("enter number"); a=Float.parseFloat(br.readLine()); b=Float.parseFloat(br.readLine()); c=a+b; d=a*b; e=a-b; f=a/b; System.out.println(a+"+"+b+"="+c); System.out.println(a+"*"+b+"="+d); System.out.println(a+"-"+b+"="+e); System.out.println(a+"/"+b+"="+f); } } OUTPUT: E:\jaid>javac p.java E:\jaid>java arith enter number 4

5 4.0+5.0=9.0 4.0*5.0=20.0 4.0-5.0=-1.0 4.0/5.0=0.8

Result: Thus program is

executed

DATE: 5-7-12 EXPT NO:6

Odd and even


ALGORITHM 1. Start 2. Initialize n 3. Print c 4. Stop Program: import java.io.*; class arith { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int n; System.out.println("enter" ); n=Integer.parseInt(br.readLine()); if(n%2==0) { System.out.println("even"); } else if(n%2==1) { System.out.println("odd"); } } } OUTPUT: E:\jaid>javac even.java E:\jaid>java arith enter 5 odd

Result: Thus program is

executed

DATE: 5-7-12 EXPT NO:7

Greatest of three numbers


ALGORITHM 1. Start 2. Initialize a,b,c 3. Read a,b from user 4. Print c 5. Stop Program: import java.io.*; class gtn { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter"); int a,b,c; a=Integer.parseInt(br.readLine()); b=Integer.parseInt(br.readLine()); c=Integer.parseInt(br.readLine()); if((a>b)&&(a>c)) { System.out.println("a is greatest"); } else if(b>c) { System.out.println("b is greatest"); } else { System.out.println("c is greatest"); } } } Output:

E:\jaid>javac great.java E:\jaid>java gtn enter 3 4 5 c is greatest

Result: Thus program is

executed

DATE: 5-7-12 EXPT NO:8

Sum avg class of marks


ALGORITHM 1. Start 2. Initialize m1,m2,m3,m4,m5 3. Read a,b from user 4. Calculate avg ,sum 5. Print c 6. Stop Program: import java.io.*; class arith { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); float m1,m2,m3,m4,m5,tot,avg; System.out.println("enter marks"); m1=Float.parseFloat(br.readLine()); m2=Float.parseFloat(br.readLine()); m3=Float.parseFloat(br.readLine()); m4=Float.parseFloat(br.readLine()); m5=Float.parseFloat(br.readLine()); tot=m1+m2+m3+m4+m5; System.out.println("tot="+tot); avg=tot/5.0f; System.out.println("avg="+avg); if(avg>=60) { System.out.println("first class"); } if(avg>=50&&avg<60) { System.out.println("second class"); } if(avg>=40&&avg<50)

{ System.out.println("third class"); } if(avg<40) { System.out.println("fail"); } } } Output: E:\jaid>javac enter.java E:\jaid>java arith enter marks 9 34 67 22 3 tot=135.0 avg=27.0 fail

Result: Thus program is

executed

DATE: 5-7-12 EXPT NO:9

Switch case to perform arithmetic expression


ALGORITHM 1. Start 2. Initialize a,b,c 3. Read a,b from user 4. Calculate c=a+b 5. Print c 6. Stop Program: import java.io.*; class ja { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("1.add"); System.out.println("2.sub"); System.out.println("3.mult"); System.out.println("4.div"); System.out.println("enter"); int a,b,c; c=Integer.parseInt(br.readLine()); a=Integer.parseInt(br.readLine()); b=Integer.parseInt(br.readLine()); switch(c) { case 1:c=a+b; break; case 2:c=a-b; break; case 3:c=a*b; break; case 4:c=a/b; break; default :System.out.println("invalid");

break; } System.out.println("result"+c); } } Output: E:\jaid>javac switch.java E:\jaid>java ja 1.add 2.sub 3.mult 4.div enter 4 10 10 1

Result: Thus program is

executed

DATE:12-7-12 EXPT NO:10

Sum of n numbers
ALGORITHM 1. Start 2. Initialize a,b,c 3. Read a,b from user 4. for(i=1;i<=n;i++) 5. Print c 6. Stop Program: import java.io.*; class arith { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int s=0,n,i; System.out.println("enter"); n=Integer.parseInt(br.readLine()); for(i=1;i<=n;i++) { s=s+i; } System.out.println("answer is"+s); } } Output: E:\jaid>javac sum1.java E:\jaid>java arith enter 5 answer is15

Result: Thus program is

executed

DATE: 12-7-12 EXPT NO:11

Factorial of a number
Algorithm: 1. Start 2. Initialize f,n,i 3. Use for(i=1;i<=n;i++) 4. F=f*i 5. stop Program: import java.io.*; class fact { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int f=1,n,i; System.out.println("enter"); n=Integer.parseInt(br.readLine()); for(i=1;i<=n;i++) { f=f*i; } System.out.println("answer is"+f); } } output E:\jaid>javac fact.java E:\jaid>java fact enter 9 answer is362880

Result: Thus program is

executed

EXPT :12 DATE:2-8-12

Sum using classes and objects


Algorithm: 1. initialize a,b 2. get data c,d 3. a=c,b=d 4. return a+b 5. stop Program: import java.io.*; class sum { int a,b; void getdata(int c,int d) { a=c; b=d; } int sum1() { return(a+b); } } class clsmain { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int d,e; d=Integer.parseInt(br.readLine()); e=Integer.parseInt(br.readLine()); sum r=new sum(); r.getdata(d,e); System.out.println("the sum is"+r.sum1()); } }

Output: D:\jaid>javac clsadd.java D:\jaid>java clsadd 10 20 30 Result: Thus program is executed.

EXPT :13 DATE:2-8-12

Area of rectangle
Algorithm: 1. start 2. initialize length,breadth 3. return(length*breadth) 4. read a,b 5. print area 6. stop Program: class clsrect { int l; int b; room(int x,int y) { l=x; b=y; } int area() { return(l*b); } } class clsmain { public static void main(String args[])throwsIOException { Bufferedreader br=new BufferedReader(new Input Stream Reader(System.in)); int a,b; System.out.println("enter"); a=Integer.parseInt(br.readLine()); b=Integer.parseInt(br.readLine()); Rectangle r2=new Rectangle(); r1.getdata(a,b); System.out.println("area is"+area());

} } Output: Enter 3 2 Area is:6 Result: Thus program is executed.

EXPT :14 DATE:9-8-12

Static members
Algorithm: 1. start 2. initialize x,y 3. return(x*y) 4. print output 5. stop program: class clsstatic { Static mul(float x,float y) { return(x*y); } class clsmain { public static void main (String args[]) { float m=cls Static mul(4.0f,5.0f); float n=cls Static divide (10.0f,2.0f); System.out.println("m"); System.out.println("n"); } } } Output: 4 5 20 Result: Thus program is executed.

EXPT :15 DATE:16-8-12

Nesting of methods
Algorithm: 1. start 2. initialize x,y 3. return(m>n)?m:n) print output 4. stop Program: class cls nesting { int m,n; cls nesting(int x,int y) { m=x; n=y; } int largest() { return((m>n)?m:n); } void display() { int large=largest() System.out.println("largest"+large); } } class clsmain { public static void main(String args[]) { cls nesting n1=new cls nesting(10,20); nesting.display(); } }

Output: X=10 Y=20 Y is largest Result: Thus program is executed

EXPT :16 DATE:16-8-12

Extending the room


Algorithm: 1. start 2. int l,b 3. return (l*b),return(l*b*h) 4. print area,volume 5. stop Program: class room { int l; int b; room(int x,int y) { l=x; b=y; } int area() { return(l*b); } } class studyroom extends room { int h; studyroom(int x,int y,int z) { super(x,y); h=z; } int volume() { return(l*b*h); } }

class clshome { public static void main(String args[]) { studyroom s1=new studyroom(12,10,10); System.out.println("area="+s1.area()); System.out.println("volume="+s1.volume()); } } Output: l=12 b=10 h=10 area=120 volume=1200 Result: Thus program is executed

EXPT :17 DATE:16-8-12

Ascending order
Algorithm: 1. start 2. for(i=1;i<n;i++) 3. if a[i]<a[j] 4. t=a[i],a[i]=a[j];a[j]=t 5. print a[i] 6. stop Program: import java.io.*; class ascending { public static void main(String args[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int n,i,t,j; int a[]=new int[100]; System.out.println("Enter number n:"); n=Integer.parseInt(br.readLine()); System.out.println("Enter numbers:"); for(i=0;i<n;i++) a[i]=Integer.parseInt(br.readLine()); for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) if(a[i]>a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } System.out.println("the numbers are :"); for(i=0;i<n;i++) System.out.println(a[i]); }

} Output: Enter n=3 Enter numbers=10 20 1 Numbers=1 10 20 Result: Thus program is executed

EXPT :18 DATE:16-8-12

Sum of 2d arrays
Algorithm: 1. start 2. for(i=1;i<m;i++) 3. c[i][j]=a[i][j]+b[i][j] 4. print c[i][j] 5. stop program: import java.io.*; class matrix { public static void main(String args[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int n,i,m,j; int a[][]=new int[10][10]; int c[][]=new int[10][10]; int b[][]=new int[10][10]; System.out.println("Enter number m:"); m=Integer.parseInt(br.readLine()); System.out.println("Enter number n:"); n=Integer.parseInt(br.readLine()); System.out.println("Enter numbers of A:"); for(i=0;i<m;i++) for(j=0;j<n;j++) { a[i][j]=Integer.parseInt(br.readLine()); } System.out.println("Enter numbers of B:"); for(i=0;i<m;i++) for(j=0;j<n;j++) { b[i][j]=Integer.parseInt(br.readLine()); } System.out.println("The sum is"); for(i=0;i<m;i++)

{ for(j=0;j<n;j++) { c[i][j]=a[i][j]+b[i][j]; System.out.print(c[i][j]+"\t"); } System.out.println(); } } } Output: Enter number m: 2 Enter number n:2 Enter numbers of A: 12 2 23 4 Enter numbers of B: 2 23 4 5 Sum is 14 25 27 Result: Thus program is executed 9

EXPT :19 DATE:23-8-12

Maximum in n numbers
Algorithm: 1. start 2. for(i=1;i<n-1;i++) 3. if a[i]>max,max=a[i] 4. print max 5. stop Program: import java.io.*; class max { public static void main(String args[])throws IOException Bufferedreader br=new Bufferedreader(new InputStreamReader(System.in)); int n,i; System.out.println("enter"); n=Integer.parseIntbr.readline(); int a[]=new Int[n]; int max=a[0]; for(i=1;i<n-1;i++) { a[i]=Integer.parseInt(br.readine()); if(a[i]>max) max=a[i] } System.out.println("greatest",max); } }

Output: Enter:5 2 3 5 15 6 Greatest is:15 Result: Thus program is executed

EXPT :20 DATE: 30-8-12

SET OF NAMES IN ASCENDING ORDER


Algorithm: 1. start 2. for(i=1;i<n-1;i++) for(j=1;i<n;j++) 3. sname[i]=sname[j]; sname[j]=t; 4. print output 5. stop program: import java.io.*; class arith { public static void main(String args[])throws IOException { int i,j,n; String Sname[]=new String[25]; System.out.println("enter tot no. of names"); ufferedReader br=new BufferedReader(new InputStreamReader(System.in)); n=Integer.parseInt(br.readLine()); System.out.println("Enter names:"); for(i=0;i<n;i++) { S name[i]=br.readLine(); } for(j=0;j<n;j++) if ((sname[i]).compare to(sname[j]>0)) { t=S.name[i]; sname[i]=sname[j]; sname[j]=t; } System.out.println("after sorting"); for(i=0;i<n;i++) System.out.println(sname[i]); } }

Output: Enter tot no.of names: 3 Enter names Alex Maths Cow After sorting: Alex cow maths result: Thus program is executed

EXPT :21 DATE:30-8-12

IMPLEMENTING INTERFACE FOR AREA


Algorithm: 1. Start 2. Float x,float y 3. Return (x*y) 4. Circle c=new circle() 5. Stop Program: Interface area { Final static float PI=3.14f System.out.println(enter); } 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*y); } Class clsmain { Public static void main(string args[]) { Area a; Rectangle rect =new rectangle() Circle c= new circle()

Output: Enter 4 5 Area 62.8 Result: Thus progam is executed

EXPT :22 DATE:14-9-12

MULTIPLE INHERITANCE TO ENTER MARKS


Algorithm: 1. Start 2. Float m1,float m2 3. M1=a M2=b 4. Print output 5. Stop Program: class student { int rno; void get no(inth) { r no=n; } void put no() { sydtem.out.println("reg no:",rno); } } class test extends student { float m1,m2; void get marks(int a,int b); { m1=a; m2=b; } void put marks() { system.out,println("m1:"m1); system.out,println("m2:"m2); } interface sports { float sport wt=6.0f; void put wt()

} class results extends test implements sports { float bot; public void put wt() { system.out.println("sports wt"+sports wt); } void display() { tot=m1+m2+sportwt; put no(); put marks(); output: enter 26 59 77 Regno:26 m1:59 m2:77 sports wt:60 tot:196 result: Thus program is executed

EXPT :19 DATE:14-9-12

PACKAGE ON DISPLAY
Algorithm: 1. Start 2. Create package dis 3. For(i=0;i<79;i++) 4. Display a=new display() 5. stop program: package DIS public class display { Void displayline() { Int I; For(i=0;i<79;i++) System.out.println(*); System.out.println(); } } To execute create folder DIS d:\jaid>md DIS d:\jaid>cd DIS d:\jaid\DIS> d:\jaid\DIS>javac display d:\jaid\DIS>cd.. program: import java.io.*; import java DIS.*; Class clsmain { Public static void main() { Display d=new display() d.displayLine(); d.displayLine(); d.displayLine(); } }

Output: 5 ***** Result: Thus program is executed

EXPT :24 DATE:14-9-12

APPLET TO BUILD A HOUSE


Algorithm: 1. Start 2. Initialize applet code 3. Draw line with coordinates g.drawLine() 4. stop Program: import java.io.*; import java.awt.*; import java.applet.*; /*<applet code="appdemo.class"width=500 height=500> </applet> */ public class appdemo extends Applet { public void paint(Graphics g) { g.drawLine(0,100,50,50); g.drawLine(50,50,100,100); g.drawLine(100,100,100,200); g.drawLine(100,200,0,200); g.drawLine(0,200,0,100); g.drawLine(50,50,200,50); g.drawLine(200,50,250,100); g.drawLine(250,100,250,200); g.drawLine(250,200,100,200); g.drawLine(250,100,0,100); } }

Output:

Result: Thus program is executed