File: /home/dilesh/desktop/javaaug/q.java Page 1 of 12: Public Class Public Static Void Int Int
File: /home/dilesh/desktop/javaaug/q.java Page 1 of 12: Public Class Public Static Void Int Int
java
INTRODUCTION TO JAVA PROGRAMMING ******************************** 1:** public class test { public static void main(string args[]) { int i=5; int x=(i==5)?5:5.0; System.out.println(x); } } output ****** test.java:3: cannot find symbol symbol : class string location: class test public static void main(string args[]) ^ test.java:6: possible loss of precision found : double required: int int x=(i==5)?5:5.0; ^ 2 errors
Page 1 of 12
2:: ****** public class test1 { public static void main(String args[]) { int i; if(i) {System.out.println("java"); } else {System.out.println("c"); } } } OUTPUT *********** test1.java:6: incompatible types found : int required: boolean if(i) ^ 1 error
File: /home/dilesh/Desktop/javaaug/q.java
int i; i=10; i++; System.out.println(i);
Page 2 of 12
} public static void main(String args[]) { local l=new local(); l.show(); /* System.out.println(i);*/ } } OUTPUT **************** 11 ____________ 2:: ^^^^^^^^^^^^^
public class local2 { void show() { int i=10; i++; System.out.println(i); } public static void main(String args[]) { local2 l=new local2(); l.show(); //System.out.println(local2.i); } OUTPUT **************** 11 INSTANCE VARIABLES ******************* 1::instance: **** public class in { int i,j;//declared inside class but outside block,method,constructor. void show() { i=10; j=i++; } public static void main(String args[]) { in i1=new i1(); i1.show(); in i2=new i2(); System.out.println(i1.i+"\t"+i1.j);//instance variable called directly System.out.println(i2.i+"\t"+i2.j);// } }
File: /home/dilesh/Desktop/javaaug/q.java
2::instance *************************** class x { int i; void show() { i++; } } public class in3 { void fun() { x ab=new x(); ab.i=10; System.out.println(ab.i); } public static void main(String args[]) { in3 i1=new in3(); } }
Page 3 of 12
STATIC VARIABLES -----------------------------------------********************************************* 1:static ============ class x { int i; static void show() { x ab=new x(); ab.i=10; System.out.println(ab.i); } } public class stat { public static void main(String args[]) { x ab=new x(); ab.i++; System.out.println(ab.i); ab.show(); } } output **************** 1 10 2:static ============ class x{ static int i; void show() { i++; } static void fun() {
File: /home/dilesh/Desktop/javaaug/q.java
x ab=new x(); ab.show(); ab.i+=4;
Page 4 of 12
} } public class stat2 { void display() { x.i++;/*static variable accessed outside class by nonstatic method through class name*/ } public static void main(String args[]) { x.i+=10;/*static variable accessed outside class by static method through class name*/ stat2 n1=new stat2(); n1.display(); x.fun(); System.out.println(x.i); } } output ****** 16 3:static *********** public class stat3 { static int i; int j; void stat3() { j=i++; show(); } void show() { int j=10; i=j++; } static void fun() { stat3 c1=new stat3(); c1.show(); c1.i++; i=c1.j++; System.out.println(i+"\t"+c1.j); } public static void main(String args[]) { i+=5; stat3 c1=new stat3(); c1.j=++i; c1.j++; fun(); System.out.println(i+"\t"+c1.j); }
File: /home/dilesh/Desktop/javaaug/q.java
output ****** 1 7
Page 5 of 12
0 0
new operator in linked list ************************************************ class node { int data; node next; } public class link { public static void main(String args[]) { node n1=new node(); node temp; temp=n1; temp.data=10; temp.next=new node(); temp=temp.next; temp.data=30; temp=n1; while(temp!=null) { System.out.println("data is "+ temp.data); temp=temp.next; } } } output ************* data is 10 data is 30
Execution of static and nonstatic block *************************************** public class y { int i; static int j; void fun() { i+=5; System.out.println(i); } { i++; System.out.println(i); } public static void main(String args[]) { y tt=new y(); tt.fun(); } } output
File: /home/dilesh/Desktop/javaaug/q.java
****** 1 6 for order of execution ********************** public class A { int i; static int j; A() { i++; j++; } A(int a) { j++; i=a; i++; } void show() { i++; j++; } { i++; j++; } static{ A t1=new A(); t1.i++; j++; System.out.println(t1.i+"\t"+j); } public static void main(String args[]) { A tt1=new A(); tt1.show(); A tt2=new A(10); System.out.println(tt1.i+"\t"+j); System.out.println(tt2.i+"\t"+j); } { i++; j++; } static{ System.out.println("Dilesh"); } } output -----4 4 Dilesh 4 11 11 11 Command line argument ********************* public class z
Page 6 of 12
File: /home/dilesh/Desktop/javaaug/q.java
{ public static void main(String args[]) { System.out.println(args[0]); System.out.println(args[1]); } } output ****** *java z hi hello hi hello *java z hi hi Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at z.main(z.java:6)
Page 7 of 12
Array initialisation -------------------public class xa { public static void main(String args[]) { int arr[];//declaration arr=new int[1];//initialisation arr[0]=10; //arr[1]=20; System.out.println(arr[0]); change(arr); System.out.println(arr[0]);
} static void change(int a[]) { a[0]=2000; } } output -----10 2000 Array index exception --------------------public class xb { public static void main(String ar[]) { int i=0; while(i++ < ar.length) { System.out.println(ar[i]);
File: /home/dilesh/Desktop/javaaug/q.java
} } output -----java xb B C D E F C D E F Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at xb.main(xb.java:9) within println only concatenation occurs -------------------------------------public class xc { public static void main(String ar[]) { System.out.println(ar[0]+ar[1]); } } output -----java xc 10 20 1020 Autoboxing ---------public class xd { public static void main(String args []) { Boolean bb1=new Boolean("java"); System.out.println(bb1); boolean b1=true; bb1=b1;//autoboxing System.out.println(bb1); } } output -----false true unboxing -------public class xe { public static void main(String args[]) { Boolean bb1=new Boolean("a"); boolean b=bb1; if(b) {System.out.println("A"); } else if(b=false) {System.out.println("B"); } else if(!b) {System.out.println("C");
Page 8 of 12
File: /home/dilesh/Desktop/javaaug/q.java
} else {System.out.println("D"); } } output ****** C conversion of string class to datatype **************************************** 1:: -----public class xf { public static void main(String ar[]) { boolean b=Boolean.parseBoolean("dilesh"); System.out.println(b); byte bb=Byte.parseByte("11"); System.out.println(bb); short s=Short.parseShort("67"); System.out.println(s); int i=Integer.parseInt("53"); System.out.println(i); float f=Float.parseFloat("58"); System.out.println(f); } } output ****** false 11 67 53 58.0 2: ----public class mod { public static void main(String ar[]) { int i,j,k,sum=0; i=Integer.parseInt(ar[0]); j=i; while(i>0) { k=i%10; sum=sum+(k*k*k); i=i/10; } if(sum==j) System.out.println(j+" is an armstrong number"); else System.out.println(j+" is not an armstrong number"); } }
Page 9 of 12
File: /home/dilesh/Desktop/javaaug/q.java
output ************ java mod 153 153 is an armstrong number
Page 10 of 12
converting datatype to string ***************************** public class xg { public static void main(String args[] ) { String s=Boolean.toString(true); System.out.println(s+" value"); s=Byte.toString((byte)1); System.out.println(s); s=Integer.toString(23); System.out.println(s); s=Double.toString(56); System.out.println(s); } } output ****** true value 1 23 56.0 taking input using scanner class ******************************** import java.util.Scanner; public class xh { public static void main(String ar[]) { Scanner s1=new Scanner(System.in); System.out.println("enter ur name"); String name=s1.nextLine(); System.out.println("enter ur roll"); int roll=s1.nextInt(); System.out.println("enter ur fees"); double d=s1.nextDouble(); System.out.println("\nStudent information"+"\n-----------------------"); System.out.println("name:"+name+"\n"+"Roll no.:"+roll+"\nfees:Rs."+d);
} } output ****** enter ur name Dilesh kumar sahoo enter ur roll 0801106227 enter ur fees
File: /home/dilesh/Desktop/javaaug/q.java
56908.45 Student information ----------------------name:Dilesh kumar sahoo Roll no.:801106227 fees:Rs.56908.45
Page 11 of 12
checking armstrong number *************************************** import java.util.Scanner; public class scan{ public static void main(String args[]) { int j,k,sum=0; Scanner s1=new Scanner(System.in); System.out.println("Enter the no. to be checked"); int i=s1.nextInt(); j=i; while(i>0) { k=i%10; sum =sum+(k*k*k); i=i/10; } if(j==sum) System.out.println(j+"is an armstrong number"); else System.out.println(j+"is not an armstrong number"); } output ************ Enter the no. to be checked 34 34is not an armstrong number
converting unicode to ascii code other mode of taking input from keyboard ********************************************************** import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.IOException; public class uni { public static void main(String args[]) { try { InputStreamReader is=new InputStreamReader(System.in); BufferedReader input=new BufferedReader(is); System.out.println("Enter ur name"); String name=input.readLine(); System.out.println("enter ur roll"); int roll= Integer.parseInt(input.readLine()); System.out.println("Enter ur fees"); double fee= Double.parseDouble(input.readLine()); System.out.println("Student information"+"\n"+"******************"); System.out.println("\nName:"+name+"\nRoll:"+roll+"\nfees:Rs."+fee); } catch(IOException ie) {
File: /home/dilesh/Desktop/javaaug/q.java
} } } output *************** Enter ur name Dilesh kumar enter ur roll 0801106227 Enter ur fees 49506.57 Student information ****************** Name:Dilesh kumar Roll:801106227 fees:Rs49506.57
Page 12 of 12