Você está na página 1de 13

(1) public class hello { public static void main (String args[]) { System.out.

println(" Hello, Welcome to JAVA world"); } }

(2) public class commandp { public static void main (String args[]) { System.out.println(" This is from command prompt"); System.out.println (args[0]); System.out.println (args[2]); } }

(3) class light { public static void main(String args[]) { int lightspeed; long days; long seconds; long distance; lightspeed=186000; days=1000; seconds=days*24*60*60; distance=lightspeed*seconds; System.out.print ("In"+days); System.out.print("days light will travel about"); System.out.println(distance+"miles"); } }

(4) class chtest { public static void main(String args[]) { char ch1, ch2; ch1=88; ch2='y'; System.out.println("ch1 and ch2"); System.out.println(ch1+" "+ch2); } }

(5) class optest { public static void main(String args[]) { int a=2; int b=5; int c=7; a +=3; b *=2; c++; System.out.println("a:"+a); System.out.println("b:"+b); System.out.println("c:"+c); } }

(6) class Conversion { public static void main(String args[]) { byte b; int i=126; double d=323.123; // double d=655381.123; System.out.println("Conversion of int to byte"); b=(byte)i; System.out.println("i and b\t"+i+" "+b); System.out.println("Conversion of double to int"); i=(int)d;

System.out.println("d and i\t"+d+" "+i); System.out.println("Conversion of byte to double"); d=b; System.out.println(" d :"+d); } }

(7) class average { public static void main(String args[]) { double nums[]={10.1, 11.2, 12.3, 13.4, 14.4}; double result=0; int i; for(i=0; i<5; i++) result=result+nums[i]; System.out.println("Average is "+result/5); } }

(8) class twoDarray { public static void main(String args[]) { int twoD[][]=new int [4][5]; int i,j,k=0; for(i=0; i<4; i++) for(j=0; j<5; j++) { twoD[i][j]=k; k++; } for(i=0; i<4; i++) { for(j=0; j<5; j++) System.out.print(twoD[i][j]+" "); System.out.println(); } } }

(9) class fortest1 { public static void main(String args[]) { int n; for(n=10; n>1; n--) System.out.println("number "+n); } }

(10) class nestedfor { public static void main(String args[]) { int i,j; for(i=0; i<6; i++) { for(j=i; j<6; j++) System.out.print("*"); System.out.println(); } } } (11) class dowhile { public static void main(String args[]) { int n=10; do { System.out.println("number "+n); n--; } while(n>3); } }

(12) class switchcase { public static void main(String args[]) { for(int i=0; i<6; i++) switch(i) { case 0: System.out.println("i is zero"); break; case 1: System.out.println("i is one"); break; default: System.out.println("i is more than one"); } } }

(13) class box1 { double width; double height; double depth; } class demo1 { public static void main(String args[]) { box1 mybox1=new box1(); box1 mybox2=new box1(); double volume; mybox1.width=10; mybox1.height=20; mybox1.depth=30; mybox2.width=3; mybox2.height=6; mybox2.depth=9; volume=mybox1.width*mybox1.height*mybox1.depth; System.out.println("Volume is "+volume); volume=mybox2.width*mybox2.height*mybox2.depth; System.out.println("Volume is "+volume); } }

(14) class box2 { double width; double height; double depth; void volume() { System.out.println("volume is" + width*height*depth); } } class demo2 { public static void main(String args[]) { box2 mybox1=new box2(); box2 mybox2=new box2(); mybox1.width=10; mybox1.height=20; mybox1.depth=30; mybox2.width=3; mybox2.height=6; mybox2.depth=9; mybox1.volume(); mybox2.volume(); } }

(15) class box3 { double width; double height; double depth; double volume() { return width*height*depth; } } class demo3 { public static void main(String args[]) { box3 mybox1=new box3(); box3 mybox2=new box3(); double value; mybox1.width=10; mybox1.height=20;

mybox1.depth=30; mybox2.width=3; mybox2.height=6; mybox2.depth=9; value=mybox1.volume(); System.out.println ("Volume is "+value); //value=mybox2.volume(); System.out.println ("Volume is "); System.out.println(mybox2.volume()); } }

(16) class box4 { double width; double height; double depth; double volume() { return width*height*depth; } void setDim(double w, double h, double d) { width=w; height=h; depth=d; } }

(17) class demo4 { public static void main(String args[]) { box4 mybox1=new box4(); box4 mybox2=new box4(); mybox1.setDim(10,20,30); System.out.println(mybox1.volume()); mybox2.setDim(3,6,9); System.out.println(mybox2.volume()); } }

(18) class Box { double width; double height; double depth; Box() { System.out.println("Constructing Box"); width=10; height=20; depth=30; } double volume() { return width*height*depth; } }

(19) class demo5 { public static void main(String args[]) { Box mybox1=new Box(); double vol; vol=mybox1.volume(); System.out.println("Volume is:"+vol); } }

(20) class box6 { double width; double height; double depth; box6(double w, double h, double d) { width=w; height=h; depth=d; } double volume() {

} }

return width*height*depth;

class demo6 { public static void main(String args[]) { box6 mybox1=new box6(10,20,30); box6 mybox2=new box6(3,6,9); double result; result=mybox1.volume(); System.out.println("Volume is:"+result); result=mybox2.volume(); System.out.println("Volume is:"+result); } }

(21) class con { double width; double height; double depth; con(double w, double h, double d) { width=w; height=h; depth=d; } con () { width=10; height=10; depth=10; } con(double len) { width=height=depth=len; } double volume() { return width*depth*height; } }

(22) class overloadcons { public static void main(String args[]) { con mybox1=new con(10,20,30); con mybox2=new con(); con mybox3=new con(12); double result; result=mybox1.volume(); System.out.println("mybox1 is "+result); result=mybox2.volume(); System.out.println("mybox2 is "+result); result=mybox3.volume(); System.out.println("mybox3 is "+result); }}

(23) class overloaddemo { void test() { System.out.println("No parameters"); } void test(int a) { System.out.println("a is:"+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; } }

(24) class overload { public static void main(String args[]) { 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) is:"+(ob.test(123.2))); } }

(25) class Factorial { int fact(int n) { int result; if (n==1) return 1; result = fact(n-1)*n; return result; } } class Recursion { public static void main(String args[]) { Factorial f=new Factorial(); System.out.println("Factorial of 3 is "+f.fact(3)); System.out.println("Factorial of 4 is "+f.fact(4)); System.out.println("Factorial of 5 is "+f.fact(5)); } }

(26) class Test { int a; public int b; private int c; void setc(int i) { c=i; } int getc() { return c; } }

(27) class AccessTest { public static void main(String args[]) { Test ob=new Test(); ob.a=10; ob.b=20; ob.setc(100); System.out.println("a, b, and c: "+ob.a+" "+ob.b+" "+ob.getc()); } }

(28) class test { void meth(int i, int j) { i *=2; j /=2; System.out.println("the values of i & J:"+i+" } }

"+j);

(29) class callbyvalue { public static void main(String args[]) { test ob=new test(); int a=Integer.parseInt(args[0]); int b=20; System.out.println("a and b before calling:"+a+" "+b); ob.meth(a,b); System.out.println("a and b after calling:"+a+" " +b); } }

(30) class set { int a,b; set(int i, int j) { a=i; b=j; } void meth(set d) { d.a *=2; d.b /=2; } } (31) class callbyref { public static void main(String args[]) { set ob=new set(15,20); System.out.println("ob.a and ob.b before calling:"+ob.a+" "+ob.b); ob.meth(ob); System.out.println("ob.a and ob.b after calling:"+ob.a+" "+ob.b); } }

Você também pode gostar