Example1 Day 2 : Intermediate Concepts 1 Examples public class Example1 public static void main(string[] args) int a= 5, b = 10, c = 15, d= 20; Assignment int x = a++; a is assigned to x and then increment System.out.println(x); End result: x = 5, a = 6 System.out.println(a); int x = ++b; b is incremented and then assigned to x System.out.println(x); End result: x = 5, a = 6 System.out.println(b); End result: b = 11, x = 11 int x = c--; c assigned to x and then decremented System.out.println(x); End result: x = 5, a = 6 System.out.println(c); End result: x= 15, c = 14 int x = --d ; End result d = 19, x= 19 System.out.println(x); End result: x = 5, a = 6 System.out.println(d); int x= -a; x = -5 System.out.println(x); End result: x = 5, a = 6 System.out.println(a); Example 2 Example 3 public class Example2 public static void main(string[] args) System.out.println(4.5<=10); System.out.println(4.5<-10); System.out.println(-35>=0); System.out.println(10<=4+6); System.out.println(35==35); System.out.println(22!=10); public class Example3 public static void main(string[] args) int x= 10, y = 20; boolean z ; z = x>y; System.out.println(z); Prints false
Example 4 public class Example4 public static void main(string[] args) int a = 1, b= 2; if(a>b) a=0; else b=0; System.out.println("a : "+a); System.out.println("b : "+b); Example 5 public class Example5 public static void main(string[] args) int a =8; if( a>0) if(a%2==0) System.out.println("Even Positive Number"); else System.out.println("Positive Odd Number"); else System.out.println("Negative Number"); System.out.println("Success! Exiting "); Example 6 public class Example6 public static void main(string[] args) int marks = 60; String grade; if (marks > 79) grade = "Honours"; else if(marks> 59) grade = "First Division"; else if(marks > 49) grade = "Second Division"; else if(marks > 39) grade = "Third Division"; else grade = "Fail"; System.out.println("Grade is : "+grade);
Example 7 public class Example7 public static void main(string[] args) int month=4; String season; switch(month) case 1: case 2: case 12: season = "Winter"; break; case 3: case 4: case 5: season = "Spring"; break; case 6: case 7: case 8: season = "Summer"; break; default: season = "Invalid Month"; System.out.println("April is in the "+season); Example 8 public class Example8 public static void main(string[] args) int x= 1; while(x<=5) System.out.println("The value of x is :" +x); x++; Example 9 public class Example9 public static void main(string[] args) int x= 1; do System.out.println("The value of x is :" +x); x++;
while(x<=5); Example 10 public class Example10 public static void main(string[] args) int x; for( x=1; x<=5;x++) System.out.println("Count is :" +x); System.out.println("Statement outside loop"); Example 11 public class Example11 public static void main(string[] args) boolean t = true; first: second: System.out.println(" Before the break"); if(t) break second; System.out.println("This won't execute"); System.out.println("After second block"); Example 12 public class Example12 public static void main(string[] args) int i; for(i=0;i<10;i++) if(i%2==0) continue; System.out.println(i + " "); Example 13 public class Example13 public static void main(string[] args) boolean t= true; System.out.println("Before the return"); if(t) return; System.out.println("After the return");
Example 14 public class Employee int empid; public static void main(string[] args) Employee emp1= new Employee(); emp1.empid = 001; emp1.name = "John"; emp1.dept = "Production"; System.out.println("Employee Id : " +emp1.empid); System.out.println("Name : " +emp1.name); System.out.println("Department : " +emp1.dept); Example 15 class Employee int empid; void display( ) System.out.println("Employee Id: " + empid); System.out.println("Name : " + name); System.out.println("Department : " + dept); public static void main(string[] args) Employee emp1= new Employee(); emp1.empid = 001; emp1.name = "John"; emp1.dept = "Production"; emp1.display( ); Example 16 Employee emp2= new Employee(); emp2.empid = 002; emp2.name = "Mary"; emp2.dept = "Testing"; emp2.display( );
Example 17 class Employee int empid; void accept(int id, String n, String d) Formal Parameters empid = id; name = n; dept = d; void display( ) System.out.println("Employee Id: " + empid); System.out.println("Name : " + name); System.out.println("Department : " + dept); public static void main(string[] args) Employee emp1= new Employee(); Employee emp2= new Employee(); emp1.accept(001, "John ", "Production "); emp2.accept(002, " Mary ", "Testing "); emp1.display( ); emp2.display( ); Example 18 class Employee int empid; void accept(int id, String n, String d) empid = id; name = n; dept = d; void accept(int id, String n) empid = id; name = n; void display( ) System.out.println("Employee Id: " + empid);
System.out.println("Name : " + name); System.out.println("Department : " + dept); public static void main(string[] args) Employee emp1= new Employee(); Employee emp2= new Employee(); emp1.accept(001, "John ", "Production"); emp2.accept(002, " Mary " ); emp1.display( ); emp2.display( ); Example 19 class Employee int empid; void accept(int id, String n, String d) empid = id; name = n; dept = d; void accept(int id, String n) empid = id; name = n; void change_basic (int a) a = 12000; void display( ) System.out.println("Employee Id: " + empid); System.out.println("Name : " + name); System.out.println("Department : " + dept); public static void main(string[] args) int basic_sal=15000; Employee emp1= new Employee(); emp1.accept(001, "John", "Production"); emp1.display( );
System.out.println("Basic Salary before method call :"+basic_sal); emp1.change_basic(basic_sal); System.out.println("Basic Salary after method call :"+basic_sal); Example 20 class Employee int empid; int basic_sal=10000; void accept(int id, String n, String d) empid = id; name = n; dept = d; void accept(int id, String n) empid = id; dept = d; void change_basic(employee ob) ob.basic_sal = 12000; void display( ) System.out.println("Employee Id: " + empid); System.out.println("Name : " + name); System.out.println("Department : " + dept); EXAMPLE 20.1 public static void main(string[] args) Employee emp1= new Employee(); emp1.accept(001, "John", "Production"); emp1.display( ); System.out.println("Basic Salary before method call: "+emp1.basic_sal); emp1.change_basic(emp1); System.out.println("Basic Salary after method call :"+emp1.basic_sal);
Example 22 public class Employee static int empid; static String name; static String dept; static void accept(int id, String n, String d) empid = id; name = n; dept = d; static void display() System.out.println("Employee Id : " +empid); System.out.println("Name : " +name); System.out.println("Department : " +dept); public static void main(string[] args) Employee emp1= new Employee(); emp1.accept(001, " John", "Production"); Employee.accept(001, "John", "Production"); classname.method() System.out.println("Name of the employee :"+Employee.name); System.out.println("Employee Id:" +Employee.empid); Example 23 public class Employee int empid; public Employee() Constructor empid =0; name =" "; dept =" "; public static void main(string[] args) Employee e2=new Employee();
Example 24 public class Employee int empid; private Employee() Constructor empid =0; name =" "; dept =" "; public static void main(string[] args) Employee e2=new Employee(); Invalid Example 25 public class Employee int empid; private Employee() Constructor empid =0; name =" "; dept =" "; void main() Employee e1= new Employee(); EXAMPLE 25.1 public static void main(string[] args) Employee e1=new Employee(001, "John", "Production"); Employee e2=new Employee(); Invalid e2.display(); Example 26 class Employee int empid; public Employee(int i, String n, String d)
empid = i; name = n; dept = d; void display() System.out.println("Employee Id : " +empid); System.out.println("Name : " +name); System.out.println("Department : " +dept); public static void main(string[] args) Employee e1=new Employee(001, "John", "Production"); Parameterized Employee e2=new Employee(); Non-parameterized e1.display(); e2.display(); Example 27 public class Employee int empid; public Employee() Constructor empid =0; name ="ABC"; dept ="TESTING "; void display() System.out.println("Employee Id : " +empid); System.out.println("Name : " +name); System.out.println("Department : " +dept); public static void main(string[] args) Employee e1=new Employee();
Example 28 public class Employee int empid; public Employee() empid=0; name="null"; dept="null"; public Employee(int i, String n, String d) empid=i; name=n; dept=d; void display( ) System.out.println("Employee Id: " + empid); System.out.println("Name : " + name); System.out.println("Department : " + dept); public static void main(string[] args) Employee e1=new Employee(001, "John", "Production"); Parameterized Employee e2=new Employee(); Non-parameterized e1.display(); e2.display();