1 / 21

JAVA PROGRAMMING written by sujan prodhan

JAVA PROGRAMMING written by sujan prodhan JAVA PROGRAMMING written by sujan prodhan JAVA PROGRAMMING written by sujan prodhan JAVA PROGRAMMING written by sujan prodhan JAVA PROGRAMMING written by sujan prodhan JAVA PROGRAMMING written by sujan prodhan JAVA PROGRAMMING written by sujan prodhan JAVA PROGRAMMING written by sujan prodhan JAVA PROGRAMMING written by sujan prodhan JAVA PROGRAMMING written by sujan prodhan bangla java programming

Sujan3
Download Presentation

JAVA PROGRAMMING written by sujan prodhan

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. FIRST Edition JAVA PROGRAMMING-360 BY SUJAN PRODHAN prodhans diary 2022 JAVA PROGRAMMING Written by sujan prodhan Written by sujan prodhan Cse,Rajshahi university Cse,Rajshahi university PRODHANS DIARY JAVA JAVA PROGRAMMING PROGRAMMING EXAMPLE EXAMPLE++ FOR B FOR BE EGGINERS GGINERS ++ Page 1 of 21

  2. JAVA PROGRAMMING-360 BY SUJAN PRODHAN prodhans diary JAVA Programming BASIC JAVA ..................................................................... 3 29. Program to use Boolean Logical Operators ( &, |, ^, ! ) .................................................................................. 12 30.Conditional Operator in different style .................. 12 1.Hello world EXAmples: ................................................ 3 31.find the Largest of three given numbers using Conditional Operator ................................................... 12 2. Program to find area of circle .................................... 3 32.Logical And ( & ) and Logical Short-circuit And ( && ) Operator ....................................................................... 12 3. Program to find area of triangle ................................ 3 4. Program to convert ruppes to paisa .......................... 3 33. Logical Or ( | ) and Logical Short-circuit Or ( || ) Operator. ...................................................................... 13 5. Simple example to calculate Interest ........................ 4 6. Simple example of comments .................................... 4 34.Bitwise Short-hand assignment operator .............. 13 7. Character example ..................................................... 4 35.Short-Hand Assignment Operators ( +=, -=, *=, /=, %= ) ............................................................................... 13 8. Boolean variable Example .......................................... 4 9.Program to show dynamic variable initialization of variable ........................................................................... 5 36.Type Casting ( Explicit Conversion .......................... 14 37. Implicit Conversion ( Automatic Type Conversion ) ...................................................................................... 14 10.Constant example ..................................................... 5 11. public constant example .......................................... 5 IF ELSE & SWITHCH CASE ............................................. 14 12. final variable to Create constant. ............................ 6 38. Simple if example ................................................... 14 13.to create local constant............................................. 6 39.Simple if example .................................................... 14 14. declare variable at end ............................................. 6 40.Simple if else example ............................................ 15 15.Swapping example using third variable ................... 6 40.Simple if else example ............................................ 15 16.Swapping example without third variable ............... 7 41. Simple if else to find even odd number ................ 15 17.convert Total Number of Days into Years, Months, Weeks and remaing days. .............................................. 7 42.find Maximum of Two numbers using If-Else. ....... 15 43. Nested if example .................................................. 16 18.Input Using Consule .................................................. 8 44.find Maximum of Three numbers using Nested-If. 16 19.Simple sum exp. Using command line ...................... 8 45.calculate commission of a salesman according to the given rate ............................................................... 16 20.Scanner Example ....................................................... 8 21.Return Statement exmple ......................................... 9 46.Simple else if ladder ................................................ 17 22.Exit Function .............................................................. 9 47.find given year is leap year or not .......................... 17 OPERATOR ...................................................................... 9 48.else if ladder example ............................................. 17 23. Arithmetic Operator Example .................................. 9 49.calculate commission of a salesman according to the given rate ............................................................... 18 24.Program to explain the working of Relational Operators ...................................................................... 10 50.switch case example ............................................... 18 25.Program to use Logical Bitwise Operators ............. 10 51.Find day using switch case ...................................... 19 26. Increment Operator example ................................ 11 52. Even odd using switch case ................................... 19 27. Decrement Operator Example ............................... 11 53. Switch case example .............................................. 20 28.Program to use Modulus operator ( % ) ................ 11 54.String literal in case of switch-case statement ...... 21 Page 2 of 21

  3. JAVA PROGRAMMING-360 BY SUJAN PRODHAN prodhans diary BASIC JAVA BASIC JAVA Area of Circle is = 314.159 1.Hello world 1.Hello world EXAmples: class FirstProgram EXAmples: 3. Program to find area of triangle 3. Program to find area of triangle class AreaTriangle { { public static void main( String args[ ] ) args[ ] ) public static void main( String { double area, a, b, c, s; a = 3; b = 4; { c = 5; World..!" ); System.out.println("Hello Java s = (a + b + c) / 2; area = Math.sqrt( s * (s-a) * (s-b) * (s-c) ); } System.out.println("Area of Triangle is = " + area ); } } Output: } Hello Java World..! Output: 2. 2. Program to find area of circle Program to find area of circle class AreaCircle Area of Triangle is = 6.0 4. Program to convert ruppes to paisa 4. Program to convert ruppes to paisa public class SimpleConversion { public static void main( String args[ ] ) { double rad; { public static void main(String args[]) { double n=56.50; final double PI = 3.14159; int a=(int) n; rad = 10.0; double p=(n-a)*100; double area = PI * rad *rad; System.out.println("Rs : "+a); System.out.println("paise :" +(int)p); of Circle is = " + area ); System.out.print( "\n Area } } Output: Rs : 56 } } paisa : 50 Output: Page 3 of 21

  4. JAVA PROGRAMMING-360 BY SUJAN PRODHAN prodhans diary 5. Simple example to calculate 5. Simple example to calculate Interest Interest class CompInterest with multiple lines */ } } { public static void main( String args[ ] ) Output: { double a, p, r ,n ,ci; Java Programming Examples p = 1000; 7. 7.Character example Character example class CharDemo r = 10; n = 3; { public static void main(String args[ ]) { a = p * Math.pow(( 1 + ( r / 100.0 ) ), n ); char aChar, bChar; ci = a - p; aChar = 88; // code for X System.out.println( " Amount = " + a); System.out.println( " Compound interest = " + ci ); } bChar = 'Y'; System.out.print("aChar and bChar : "); } System.out.println(aChar + " " + aChar); Output: aChar++; // increment aChar Amount = 1331.0000000000005 System.out.println("aChar : " + aChar); Compound interest = 331.000000000000045 } } 6 6. Si . Simple example of comments mple example of comments class Comments { Output: aChar and bChar : X Y // Your program begins with a call to main(). aChar : Y 8. Boolean variable Example 8. Boolean variable Example class BoolTest // this is single line comment public static void main( String args[ ] ) { { System.out.println("Java Programming Examples"); public static void main(String args[ ]) { /* boolean b; This is multiline comment Page 4 of 21

  5. JAVA PROGRAMMING-360 BY SUJAN PRODHAN prodhans diary b = false; 10.Constant example 10.Constant example class Constant1 System.out.println("b is " + b); { b = true; static final int NUMBER_OF_MONTHS = 12; System.out.println("b is " + b); static final double PI = (double) 22 / 7; if(b) public static void main( String args[] ) { System.out.println("This is executed."); { } } System.out.println("NUMBER_OF_MONTHS : " + NUMBER_OF_MONTHS ); System.out.println("PI : " + PI ); } Output: b is false This is executed. } 9.Program to s 9.Program to show dynamic variable initialization of variable variable initialization of variable class DynInit how dynamic } Output: NUMBER_OF_MONTHS : 12 { PI : 3.142857142857143 public static void main( String args[ ] ) 11. public constant example 11. public constant example class MyConstant { double a = 3.0, b = 4.0; { // c is dynamically initialized public static final int NUMBER_OF_MONTHS = 12; double c = Math.sqrt(a * a + b * b); System.out.println("Hypotenuse is " + c); public static final double PI = (double) 22 / 7; } } } class Constant2 Output: { Hypotenuse is 5.0 public static void main( String args[ ] ) { Page 5 of 21

  6. JAVA PROGRAMMING-360 BY SUJAN PRODHAN prodhans diary System.out.println("NUMBER_OF_MONTHS : " + MyConstant.NUMBER_OF_MONTHS ); a = PI * r * r; System.out.println("Area of Circle : " + a); System.out.println("PI : " + MyConstant.PI ); } } } } Output: NUMBER_OF_MONTHS : 12 Output: PI : 3.142857142857143 Area of Circle : 314.159 12. final variable to Create final variable to Create constant. constant. class FinalVar 1 14 4. declare variable at end . declare variable at end class DeclareAtEnd { { public static void main( String args[ ] ) final static double PI = 3.14159; { public static void main( String args[ ] ) msg = "Hello Java Developer..!"; { System.out.println("Message : " + msg); double r = 10.0, a; } a = PI * r * r; static String msg; System.out.println("Area of Circle : " + a); } } Output: Message : Hello Java Developer..! } 15. 15.Swapping example using third Swapping example using third variable variable class Swap Output: Area of Circle : 314.159 1 13 3.to create local constant .to create local constant. . class FinalVar1 { public static void main( String args[ ] ) { public static void main( String args[ ] ) { { int a, b, t; double r = 10.0, a; a = 10; final double PI = 3.14159; Page 6 of 21

  7. JAVA PROGRAMMING-360 BY SUJAN PRODHAN prodhans diary b = 20; a = a - b; System.out.print( "\nBefore Swapping :" ); System.out.print( "\nAfter Swapping : " ); System.out.print( a + " " + b ); System.out.print( a + " " + b ); t = a; } a = b; } b = t; System.out.print( "\nAfter Swapping : " ); Output: Before Swapping : 10 20 System.out.print( a + " " + b ); After Swapping : 20 10 } } 17. 17.convert Total Number of Days convert Total Number of Days into Years, Months, Weeks and into Years, Months, Weeks and remaing days remaing days. . Output: Before Swapping : 10 20 After Swapping : 20 10 1 16. 6.Swapping example without Swapping example without third variable third variable class Days { public static void main( String args[ ] ) { int days, years, months, weeks; class SwapWithoutThirdVariable days = 1050; { public static void main( String args[ ] ) years = days / 365; { days = days % 365; int a, b; months = days / 30; a = 10; days = days % 30; b = 20; weeks = days / 7; System.out.print("\nBefore Swapping : " ); days = days % 7; System.out.print( a + " " + b ); System.out.print("\n Years = " + years ); a = a + b; System.out.print("\n Months=" + months ); b = a - b; Page 7 of 21

  8. JAVA PROGRAMMING-360 BY SUJAN PRODHAN prodhans diary System.out.print "\n Weeks = " + weeks ); 19.Simple sum exp. Using 19.Simple sum exp. Using command line command line class Sum System.out.print( "\n Days = " + days ); } { public static void main( String args[ ] ) } { int num1, num2, sum; Output: Years = 2 num1 = Integer.parseInt( args[0] ); Months = 10 num2 = Integer.parseInt( args[1] ); Weeks = 2 Days = 6 sum = num1 + num2; 18.Input Using 18.Input Using Consule import java.io.*; Consule sum ); System.out.println( " Sum = " + class ConsoleExample } { } public static void main( String args[ ] ) Output: (In command Prompt) { D:\Java Examples>javac sum.java Console cn = System.console(); D:\Java Examples>java sum 150 250 int n; Sum = 400 Number : "); System.out.print( "Enter a 20.Scanner Example 20.Scanner Example import java.util.Scanner; cn.readLine() ); n = Integer.parseInt( class ScannerExample number : " + n ); System.out.println( "The given { public static void main( String args[ ] ) } { } ); Scanner sc = new Scanner( System.in Output: Enter a Number : 100 int n; The given number : 100 System.out.print( "Enter a Number : "); Page 8 of 21

  9. JAVA PROGRAMMING-360 BY SUJAN PRODHAN prodhans diary n = sc.nextInt(); System.exit(0); System.out.println( "The given number : " + n ); System.out.println( "BBB" ); } } } Output: Enter a Number : 100 } The given number : 100 Output: AAA 21.Return Statement exmple 21.Return Statement exmple class ReturnTest { public static void main( String args[] ) OPERATOR OPERATOR { int a = 10; 23. Arithmetic Operator Example 23. Arithmetic Operator Example class ArithOpr System.out.println( "Before return." ); { public static void main( String args[ ] ) if( a == 10 ) { int a = 25, b = 10; return; float c = 25.5f, d = 4.0f; System.out.println( "After return." ); System.out.println( " a = " + a ); } System.out.println( " b = " + b ); } System.out.println( " c = " + c ); System.out.println( " d = " + d ); Output: Before return. System.out.println( " a + b = " + ( a + b ) ); 22. 22.Exit Function Exit Function class ExitTest System.out.println( " a - b = " + ( a - b ) ); System.out.println( " a * b = " + ( a * b ) ); { public static void main( String args[] ) System.out.println( " a / b = " + ( a / b ) ); { System.out.println( " a % b = " + ( a % b ) ); System.out.println( " a + c = " + ( a + c ) ); int a = 10; System.out.println( " c + d = " + ( d + d ) ); System.out.println( "AAA" ); System.out.println( " c % d = " + ( c % d ) ); if( a == 10 ) System.out.println( " -a % b = " + ( -a % b ) ); Page 9 of 21

  10. JAVA PROGRAMMING-360 BY SUJAN PRODHAN prodhans diary System.out.println( " -a % -b = " + ( -a % -b ) ); System.out.println( " a > b = " + ( a > b ) ); System.out.println( " a % b = " + ( a % b ) ); System.out.println( " a <= b = " + ( a <= b ) ); System.out.println( " a % -b = " + ( a % -b ) ); System.out.println( " a >= b = " + ( a >= b ) ); } System.out.println( " a == b = " + ( a == b ) ); } System.out.println( " a != b = " + ( a != b ) ); Output: a = 25 } b = 10 } c = 25.5 Output: d = 4.0 a = 20 a + b = 35 b = 10 a - b = 15 a < b = false a * b = 250 a > b = true a / b = 2 a <= b = false a % b = 5 a >= b = true a + c = 50.5 a == b = false c + d = 8.0 a != b = true c % d = 1.5 25.Program to use Logical Bitwise 25.Program to use Logical Bitwise Operators Operators -a % b = -5 -a % -b = -5 a % b = 5 class BitwiseOperatorExample a % -b = 5 { public static void main(String args[ ]) { 24.Program to explain the working 24.Program to explain the working of Relational Operators of Relational Operators int a = 3, b = 6, c; System.out.println("a = " + a); System.out.println("b = " + b); class RelationalOpr c = a & b; { public static void main( String args[ ] ) System.out.println("a & b = " + c); { int a = 20, b = 10; c = a | b; System.out.println( " a = " + a ); System.out.println("a | b = " + c); System.out.println( " b = " + b ); c = a ^ b; System.out.println( " a < b = " + ( a < b ) ); Page 10 of 21

  11. JAVA PROGRAMMING-360 BY SUJAN PRODHAN prodhans diary System.out.println("a ^ b = " + c); } c = ~b; } System.out.println("~ b = " + c); Output: c = a << 3; Value of a = 11 System.out.println("a << 3 = " + c); Value of b = 21 c = b >> 2; Value of c = 32 System.out.println("b >> 2 = " + c); 27. Decrement Operator Example 27. Decrement Operator Example class DecrementOperatorExample a = -1 c = a >>> 24; { public static void main( String args[ ] ) System.out.println("a >>> 24 = " + c); { int a = 10, b = 20 ,c; } c = ( --a ) + ( --b ); //c = ( a-- ) + ( b-- ); } System.out.println( " Value of a = " + a ); Output: a = 3 System.out.println( " Value of b = " + b ); b = 6 System.out.println( " Value of c = " + c ); a & b = 2 } a | b = 7 } a ^ b = 5 ~ b = -7 a << 3 = 24 Output:Value of a = 9 b >> 2 = 1 Value of b = 19 a >>> 24 = 255 Value of c = 28 26. Increment Operator exampl 26. Increment Operator example e class IncrementOperatoeExample 28.Program to use Modulus operator ( 28.Program to use Modulus operator ( % ) % ) class Modulus { public static void main( String args[ ] ) { int a = 10, b = 20 ,c; { public static void main(String args[ ]) c = ( ++a ) + ( ++b ); { int x = 42; // a++ is Post fix increment // ++b is pre fix increment double y = 42.25; System.out.println( " Value of a = " + a ); System.out.println("x mod 10 = " + x % 10); System.out.println( " Value of b = " + b ); System.out.println("y mod 10 = " + y % 10); System.out.println( " Value of c = " + c ); } Page 11 of 21

  12. JAVA PROGRAMMING-360 BY SUJAN PRODHAN prodhans diary } 30.Conditional Operator in 30.Conditional Operator in different style different style class ConditionalOperator Output: x mod 10 = 2 y mod 10 = 2.25 { public static void main( String args[ ] ) 29. Program to use Boolean Logical 29. Program to use Boolean Logical Operators ( &, |, ^, ! ) Operators ( &, |, ^, ! ) class BoolLogic { int a, b, max; a = 25; b = 14; { public static void main(String args[ ]) max = ( ( a > b ) ? a : b ); { boolean a = true; System.out.print("\nMaximum Value is= " + max ); boolean b = false; } boolean c = a | b; } boolean d = a & b; Output: Maximum Value is= 25 boolean e = a ^ b; 31.find the Largest of three given 31.find the Largest of three given numbers using Conditional numbers using Conditional Operator Operator class MaxOf3ConditionalOperator boolean f = !a; boolean g = (!a & b) | (a & !b); System.out.println(" a = " + a); System.out.println(" b = " + b); {public static void main( String args[ ] ) System.out.println(" a|b = " + c); {int a, b, c, max = 0; a = 20; b = 30; c = 45; System.out.println(" a&b = " + d); max = ((a>b) ? ((a>c) ? a : c ) : ((b>c) ? b : c )); System.out.println(" a^b = " + e); System.out.println( " The LARGEST Number is = " + max ); System.out.println(" !a = " + f); System.out.println(" !a&b|a&!b = " + g); } } } } Output: The LARGEST Number is = 45 Output: a = true 32.Logical And ( & ) and Logical 32.Logical And ( & ) and Logical Short Short- -circuit And ( && ) Operator circuit And ( && ) Operator class ShortCircuitAnd b = false a|b = true a&b = false { public static void main(String args[ ]) a^b = true { int a = 10, b = 20; !a = false if( (a < 5) && (++b < 10) ) !a&b|a&!b = true { System.out.println(" True "); Page 12 of 21

  13. JAVA PROGRAMMING-360 BY SUJAN PRODHAN prodhans diary } System.out.println(" b = " + b); System.out.println("a : " + a); if( (a < 5) & (++b < 10) ) a &= 4; { System.out.println(" True "); System.out.println("a &= 4 : " + a); } a |= 3; System.out.println(" b = " + b); System.out.println("a |= 3 : " + a); } a ^= 8; } System.out.println("a ^= 8 : " + a); Output: b = 20 b = 21 a <<= 3; System.out.println("a <<= 3 : " + a); 33. Logical Or ( | ) and 33. Logical Or ( | ) and Logical Short circuit Or ( || ) Operator circuit Or ( || ) Operator. class ShortCircuitOr Logical Short- - a >>= 2; System.out.println("a >>= 2 : " + a); a >>>= 3; { public static void main(String args[ ]) System.out.println("a >>>= 3 : " + a); { int a = 10, b = 20; } if( (a > 5) || (++b < 10) ) } { System.out.println(" True "); } Output: System.out.println(" b = " + b); a : 14 if( (a > 5) | (++b < 10) ) a &= 4 : 4 { System.out.println(" True ");} a |= 3 : 7 System.out.println(" b = " + b); a ^= 8 : 15 } a <<= 3 : 120 } a >>= 2 : 30 Output: b = 20 a >>>= 3 : 3 True b = 21 35.Sho 35.Short rt- -Hand Assignment Hand Assignment Operators ( +=, Operators ( +=, - -=, *=, /=, %= ) class ShortHandAssign 34.Bitwise Short 34.Bitwise Short- -hand assignment hand assignment operator operator class BitwiseShortHandAssignmentOperator =, *=, /=, %= ) { public static void main(String args[ ]) { int a = 1; { public static void main( String args[ ] ) int b = 2; { int a = 13; Page 13 of 21

  14. JAVA PROGRAMMING-360 BY SUJAN PRODHAN prodhans diary int c = 3; { public static void main( String args[ ] ) a += 5; { Int i = 2; b *= 4; float f = 12.33f; c += a * b; byte b = 1; c %= 6; double d = 124.097; System.out.println("a = " + a); double res = ( i - b ) * ( d / f ); System.out.println("b = " + b); System.out.println( " Answer is = " + res ); System.out.println("c = " + c); } } } } Output: Answer is = 10.064639153923036 Output: a = 6 IF ELSE & SWITHCH CASE IF ELSE & SWITHCH CASE b = 8 c = 3 38. Simple if example 38. Simple if example class SimpleIf 36.Type Casting ( Explicit 36.Type Casting ( Explicit Conversion Conversion) { public static void main(String args[ ] ) class TypeCasting { int n; { public static void main( String args[ ] ) n = 230; { int a; if( n > 100 ) float b = 123.333f; { System.out.println( " Before Casting = " + b ); System.out.println( " The number is greater than 100." ); a = (int) b; } System.out.println( " After Casting = " + a ); } } } } Output: The number is greater than 100. Output: Before Casting = 123.333 39.Simple if example 39.Simple if example public class SimpleIf{ After Casting = 123 37. Implicit Conversion ( 37. Implicit Conversion ( Automatic Type Conversio Automatic Type Conversion ) class ImplicitConversion public static void main(String[] args) { n ) int a = 10; int b = 20; Page 14 of 21

  15. JAVA PROGRAMMING-360 BY SUJAN PRODHAN prodhans diary if(a>b){ { System.out.println("a is greater than b"); } System.out.println("a is greater than b"); else } { System.out.println("a is less than or equal to b"); } if(a<b){ } System.out.println("a is less than b"); } } Output: a is less than or equal to b if(a==b){ 41. Simple if else to find even odd 41. Simple if else to find even odd number number class EvenOddTest System.out.println("a is equal to b"); } } { public static void main( String args[] ) } { int n; n = 23; Output:a is less than b if( n % 2 == 0 ) 40.Simple if else example 40.Simple if else example class IfElse System.out.println("Number " + n + " is Even Number." ); Else System.out.println("Number " + n + " is Odd Number." ); { public static void main( String args[ ] ) } { int n; } n = 23; Output:Number 23 is Odd Number. if( n > 100 ) {System.out.println( " The number is greater than 100." ); 42.find Maximum of 42.find Maximum of Two numbers using If using If- -Else. Else. class Max2IfElse Two numbers } else { System.out.println( " The number is smaller than 100." ); } { public static void main( String args[] ) } { int a, b, max; } a = 23; b = 14; Output: The number is smaller than 100. if( a > b ) 40.Simple if else example 40.Simple if else example public class IfElse{ max = a; else public static void main(String[] args) max = b; { int a = 23, b = 47, c; System.out.println("\nMaximum : " + max ); if(a>b) } Page 15 of 21

  16. JAVA PROGRAMMING-360 BY SUJAN PRODHAN prodhans diary } max = a; Output:Maximum : 23 else max = c; } 43. Nested if 43. Nested if example public class NestedIf{ example else { public static void main(String[] args) { if( b > c ) int a = 10; max = b; if(a>0){ else if(a<=10){ max = c; System.out.println("a --> 0 to 10"); } } System.out.println("\nMaximum : " + max ); if(a>10 && a<=20) } { System.out.println("a --> 11 to 20"); } } Output: Maximum : 93 if(a>20) 45.calculate commission 45.calculate commission of a salesman according to the given rate salesman according to the given rate of a {System.out.println("a --> 21 to ((2^31 -1)= 2,14,74,83,647"); } Sales Value Comm. Rate (%) } 0 to 5000 0 % } 5000 to 10000 5 % } 10000 to 20000 10 % Output:a --> 0 to 10 20000 to 30000 12 % 44.find Maximum of Three numbers 44.find Maximum of Three numbers using Nested using Nested- -If. If. class Max3IfElse Above 30000 15 % class CommissionNested { public static void main( String args[ ] ) { public static void main( String args[] ) { double sales, comm; { int a, b, c, max; a = 23; sales = 12000; b = 93; if( sales < 5000 ) c = 10; {comm = 0; } if( a > b ) { if( a > c ) else Page 16 of 21

  17. JAVA PROGRAMMING-360 BY SUJAN PRODHAN prodhans diary { if( sales < 10000 ) 47.find given year is leap year or 47.find given year is leap year or not not class Leap2 { comm = sales * 0.05; } else { if( sales < 20000 ) { public static void main( String args[ ] ) { comm = sales * 0.1; } { int y; y = 2016; else { if( sales < 30000 ) if( y % 100 == 0 ) {comm = sales * 0.12; } { if( y % 400 == 0 ) else { comm = sales * 0.15; System.out.println( " It is a LEAP Year." ); } else } System.out.println( " It is NOT a LEAP Year." ); } } } else if( y % 4 == 0 ) System.out.println( " The Commission is = " + comm ); System.out.println("It is a LEAP Year." ); } else } System.out.println( " It is NOT a LEAP Year." ); Output: The Commission is = 1200.0 } } 46.Simple else if ladder 46.Simple else if ladder class ElseIfDemo Output:It is a LEAP Year. { public static void main( String args[ ] ) 48.else if ladder example 48.else if ladder example public class ElseIfLadder{ { int a = 2; if (a == 1) public static void main(String[] args) { {System.out.println("One"); } int a = 13; else if (a == 2) if (a < 0) { {System.out.println("Two"); } System.out.println("a is negative"); else if (a == 3) } else if (a == 0) { { System.out.println("Three"); } System.out.println("a is zero"); else } else if (a > 0) { { System.out.println("invalid Value."); } System.out.println("a is positive"); } } } Output:Two } Page 17 of 21

  18. JAVA PROGRAMMING-360 BY SUJAN PRODHAN prodhans diary } Sales Value Comm. Rate (%) Output:a is positive 0 to 5000 0 % 49. show Day Name according to Day Number ( 1 - 7 ) 5000 to 10000 5 % class ElseIfDays 10000 to 20000 10 % { public static void main(String args[ ]) 20000 to 30000 12 % { int day; day = 7; Above 30000 15 % if( day == 1 ) class CommissionElseIf System.out.println( " Monday." ); { public static void main( String args[ ] ) else if( day == 2 ) { double sales, comm; System.out.println( " Tuesday." ); sales = 12000; else if( day == 3 ) if( sales < 5000 ) System.out.println( " Wednesday." ); { comm = 0; else if( day == 4 ) } System.out.println( " Thursday." ); else if( sales < 10000 ) else if( day == 5 ) { comm = sales * 0.05; } System.out.println( " Friday." ); else if(sales < 20000) else if( day == 6 ) {comm = sales * 0.1; System.out.println( " Satarday." ); } else if( day == 7 ) else if( sales < 30000 ) System.out.println( " Sunday." ); { comm = sales * 0.12; else } System.out.println( " Wrong Day Number." ); else } { comm = sales * 0.15; } } Output:Sunday. System.out.println( " The Commission is = " + comm ); } } Output:The Commission is = 1200.0 50.switch case example 50.switch case example class SwitchDemo 49.calculate commission of a 49.calculate commission of a salesman according to the given rate salesman according to the given rate { public static void main( String args[ ] ) Page 18 of 21

  19. JAVA PROGRAMMING-360 BY SUJAN PRODHAN prodhans diary { int a = 2; break; switch (a) case 4: { case 1 : System.out.println( " Thursday." ); System.out.println("One."); break; break; case 5: case 2 : System.out.println( " Friday." ); System.out.println("Two."); break; break; case 6: case 3 : System.out.println( " Satarday." ); System.out.println("Three."); break; break; case 7: default: System.out.println( " Sunday." ); System.out.println("Invalid value."); break; } default: } System.out.println( " Wrong Day Number." ); } } Output:Two } } 51.Find day using switch case 51.Find day using switch case class SwitchCaseDays Output: Satarday. { public static void main(String args[ ]) 52. Even odd using switch case 52. Even odd using switch case class EvenOdd { int day; day = 6; switch( day ) { public static void main(String args[ ]) { case 1: { for( int i=1 ; i<=10 ; i++ ) System.out.println( " Monday." ); { switch( i ) break; { case 2: case 1: System.out.println( " Tuesday." ); case 3: break; case 5: case 3: case 7: System.out.println( " Wednesday." ); case 9: Page 19 of 21

  20. JAVA PROGRAMMING-360 BY SUJAN PRODHAN prodhans diary System.out.println( i + " - Odd Number." ); System.out.println("byte"); System.out.println("8 bit"); break; break; case 2: case 1: case 4: System.out.println("short"); case 6: System.out.println("16 bit"); case 8: break; case 10: case 2: System.out.println( i + " - Even Number." ); System.out.println("int"); break; System.out.println("32 bit"); } break; } case 3: System.out.println( " The End. " ); System.out.println("long"); } System.out.println("64 bit"); }Output:1 - Odd Number. break; 2 - Even Number. case 4: 3 - Odd Number. System.out.println("float"); 4 - Even Number. System.out.println("32 bit"); 5 - Odd Number. break; 6 - Even Number. case 5: 7 - Odd Number. System.out.println("double"); 8 - Even Number. System.out.println("64 bit"); 9 - Odd Number. break; 10 - Even Number. The End case 6: 53. Switch case example 53. Switch case example public class SwitchCase{ System.out.println("boolean"); System.out.println("1 bit : true/false"); public static void main(String[] args) { break; int choice = 1; case 7: switch (choice) { System.out.println("char"); case 0: System.out.println("16 bit"); Page 20 of 21

  21. JAVA PROGRAMMING-360 BY SUJAN PRODHAN prodhans diary break; break; default: case "THURSDAY": System.out.println("Other entry"); System.out.println( "It is day number-4." ); break; break; } case "FRIDAY": } System.out.println( "It is day number-5." ); break; } case "SATARDAY": Output: short System.out.println( "It is day number-6." ); 16 bit break; case "SUNDAY": 54.String literal in case of switch 54.String literal in case of switch- - case statement case statement import java.util.*; System.out.println( "It is day number-7." ); break; default: class SwitchString System.out.println("It is Wrong day." ); { public static void main(String args[ ]) } { String dname; } Scanner sc = new Scanner( System.in ); } dname = sc.next(); System.out.print( " Enter Day Name = " ); Output: Enter Day Name = WEDNESDAY dname = dname.toUpperCase(); It is day number-3. switch( dname ) { case "MONDAY": System.out.println( "It is day number-1." ); break; case "TUESDAY": System.out.println( "It is day number-2." ); break; case "WEDNESDAY": System.out.println( "It is day number-3." ); Page 21 of 21

More Related