Java Programming - Chapter 9 (in Thai)

download Java Programming - Chapter 9 (in Thai)

of 18

Transcript of Java Programming - Chapter 9 (in Thai)

  • 7/25/2019 Java Programming - Chapter 9 (in Thai)

    1/18

    113

    2553 ( 7 2/2553) ( )

    Computer Programming using JavaAdvanced Statements and Applications - 09

    CHAPTER

    9

    (Advanced Statements and Applications)

    1. (Advanced Decision Statements)

    1. 2 1) i f - el sei f - el se(Nested If-Else)

    3 22)

    i f - el se (Logical Operators) 3

    2. if-elseif-else(Nested If-Else)

    3. if-else

    1 [] (10 )1)

    n3 12 112 (2 )

    1234567891011

    121314151617

    if ( Condition1) {Stat ement 1;

    } else {if ( Condition2) {

    Stat ement 2;} else {if ( Condition3) {

    Stat ement 3;} else {

    ..

    .} else {

    Stat ement N;}

    }}

    }

    ..

    .

    if ( Condition1) {Stat ement 1;

    } else if ( Condition2) {Stat ement 2;

    } else if ( Condition3) {Stat ement 3;

    } else {Stat ement N;

    }

    12

    345678

    if ( Condition1 && Condition2 | | Condition3 &&. . . | | ConditionN) {Stat ement 1;

    Stat ement 2;...

    Stat ement M- 1;Stat ement M;

    }

    i f

    &&| |

    el se

  • 7/25/2019 Java Programming - Chapter 9 (in Thai)

    2/18

    114

    2553 ( 7 2/2553) ( )

    Computer Programming using Java 09 - Advanced Statements and Applications

    2) n3 5 100 (2 )

    3)

    m(2 )

    4) m(2 )

    5)

    s"Yes'"Y"(2 )

    2 [] getDaysOfMonth() .. .. 1 31 6 30 0

    (Leap Year) 29 .. 4 100 .. 400 (10 )

  • 7/25/2019 Java Programming - Chapter 9 (in Thai)

    3/18

    115

    2553 ( 7 2/2553) ( )

    Computer Programming using JavaAdvanced Statements and Applications - 09

    3 [] getSize() nn(10 )1) n >= 0nmm

    (1)0

  • 7/25/2019 Java Programming - Chapter 9 (in Thai)

    4/18

    116

    2553 ( 7 2/2553) ( )

    Computer Programming using Java 09 - Advanced Statements and Applications

    4 [] isEngStudent() 10 7 1 1 (10 )

    1) 1-2

    1) 3 3

    2) 9-10 21

    3)

    1-2 21 4) 5 1 1

    2. (Advanced Iteration Statements)

    1. 2

    1) f or f or (Nested For) whi l e

    whi l e(Nested While) f or whi l e (Nested Loops) 4 2

    5 4 3 0 8 1 9 5 2 1

    1 2 3 9 10

    2 1 1 0 1 9 1

    1 2 5

  • 7/25/2019 Java Programming - Chapter 9 (in Thai)

    5/18

    117

    2553 ( 7 2/2553) ( )

    Computer Programming using JavaAdvanced Statements and Applications - 09

    2) f or whi l e( ) 3

    2.

    1)

    f or f or f or

    2)

    f or f or f or f or f or

    3)

    1-2 f or ()

    5 [] i, j, ksum

    (10 )

    1)

    i j k sum

    12345678910

    for ( Initial1; Condition1; Update1) {for ( Initial2; Condition2; Update2) {for ( Initial3; Condition3; Update3) {. . .for ( InitialN; ConditionN; UpdateN) {

    St atement s;}

    }}

    }

    f or

    1234567

    f or ( i nt i = 1; i = 5; j - - ) {

    f or ( i nt k = 3; k < 5; k++) {i nt sum = i + j + k;

    }}

    }

    2) sumi, jksum

    3) sumi, jk

    sum

    f or f or 2

    f or f or f or

  • 7/25/2019 Java Programming - Chapter 9 (in Thai)

    6/18

    118

    2553 ( 7 2/2553) ( )

    Computer Programming using Java 09 - Advanced Statements and Applications

    6 [] (8 )

    7 [] formulaAtoB() ab formulaAtoB(4, 9) 4 9 (10 )

    8 [] showBarGraph() 1xy1xy

    x y 3 (15 )

    l oopXYZ( 2, 7, 1) ;1234

    56789

    publ i c stati c voi d l oopXYZ( i nt x, i nt y, i nt z) {f or ( i nt i = x; i < 5; i ++)

    f or ( i nt j = y; j > 2 * i ; - - j )f or ( i nt k = z; k

  • 7/25/2019 Java Programming - Chapter 9 (in Thai)

    7/18

    119

    2553 ( 7 2/2553) ( )

    Computer Programming using JavaAdvanced Statements and Applications - 09

    public class BarGraph {public static void main(String[] args) {

    } //End of main

    //showBarGraph()

    } //End of class

    9 [] calEquation() 0954663772889 zxy...zy11xzy13xzy15xyz17xs +++++= s

    1) x1 a 1a

    2) y5 b 5b 3)

    z12 c2c

    x yy z main()10 (15 )

  • 7/25/2019 Java Programming - Chapter 9 (in Thai)

    8/18

    120

    2553 ( 7 2/2553) ( )

    Computer Programming using Java 09 - Advanced Statements and Applications

    import java.util.Scanner;

    public class TheEquation {

    //calEquation()

    public static void main(String[] args) {

    } //End of main} //End of class

    3. 1)

    for1234

    56

    I ni t i al 11for ( Initial1, Initial2, Initial3, . . . ;

    Condition1 && Condition2 | | Condition3 && . . . ;Update1, Update2, Update3, . . . ) {.

    ..}I ni t i al Comma, Condi t i onLogicalOperators, Update Comma

  • 7/25/2019 Java Programming - Chapter 9 (in Thai)

    9/18

    121

    2553 ( 7 2/2553) ( )

    Computer Programming using JavaAdvanced Statements and Applications - 09

    2) while

    10 [] (8 )

    11 [] (8 )

    12 [] toStringCalculation() a,bc (Loop) x,yzp, qrinputNumber() 28101647143812291010 zxy...zy7xzy8xzy9xyz10xs +++++= for3 x, yzx + y + z1000 x, y, zs(15 )

    1234

    56

    Initial1, Initial2, Initial3, . . . ;while ( Condition1 && Condition2 | | Condition3 && . . . ) {

    .

    .

    .Update1; Update2; Update3; . . . ;

    }

    I ni t i al Comma, Condi t i onLogicalOperators, Update Semicolon

    i t er ABC( 1, 15, 2) ;

    1234

    5678

    publ i c st at i c voi d i t er ABC( i nt a, i nt b, i nt c) { whi l e (a 3 | | c >= 0) {

    Syst em. out . pr i nt l n( a + "\ t " + b + "\ t " + c) ;a++;

    b- - ;i f ( ( a + b) % 2 == 0) c- - ;

    }}

    12345678

    f or ( i nt i = 0, j = 10; i < 10 && j > 0; i ++, j - - ) {i f ( i == j / 2) br eak;System. out . pr i nt l n( i ) ;i f ( j % 2 != 0) continue;System. out . pr i nt l n( j ) ;i f ( i * 4 == j )System. out . pr i nt l n( i + ", " + j ) ;

    }

    continue Loop

  • 7/25/2019 Java Programming - Chapter 9 (in Thai)

    10/18

    122

    2553 ( 7 2/2553) ( )

    Computer Programming using Java 09 - Advanced Statements and Applications

    import java.util.Scanner;

    import java.util.io.*;public class Calculation {

    //toStringCalculation()

    //toStringCalculation() main() 10, 20 30

    public static void main(String[] args) {

    } //End of main} //End of class

    public static int inputNumber(String s) {

    Scanner kb = new Scanner(System.in);System.out.print("Enter " + s + ": ");return kb.nextInt();

    }

  • 7/25/2019 Java Programming - Chapter 9 (in Thai)

    11/18

    123

    2553 ( 7 2/2553) ( )

    Computer Programming using JavaAdvanced Statements and Applications - 09

    3. (Applications of Advanced Statements)

    1) f or f or f or whi l ei f - el sei f - el se

    2)

    13 [- ] starXX() - System.out.print("*"); System.out.println("*"); System.out.print(" ");System.out.println(); (80 : 8 )

    public class Shape {

    public static void starA(int n) {

    } //End of method

    public static void starB(int n) {

    } //End of method

    1234567

    f or f or 2 f or i f - el sei f - el se

    st ar A( 11)

    ***********

    **************************************************************************************************************

    for ( I ni t i al 1; Condition1; Update1) {for ( I ni t i al 2; Condition2; Update2) {if ( Condition3) St atement ;else if ( Condition4) St atement ;else St atement ;

    }}

    []

    st ar B( 11)

    *******************************************************

    ***********

    []

  • 7/25/2019 Java Programming - Chapter 9 (in Thai)

    12/18

    124

    2553 ( 7 2/2553) ( )

    Computer Programming using Java 09 - Advanced Statements and Applications

    public static void starC(int n) {

    } //End of method

    public static void starD(int n) {

    } //End of method

    public static void starE(int n) {

    } //End of method

    st ar C( 11)

    *********************************************

    *********************

    st ar D( 11)

    *

    *****

    *********

    *************

    *****************

    *********************

    []

    st ar E( 11)

    ******************************************************************

    []

    []

  • 7/25/2019 Java Programming - Chapter 9 (in Thai)

    13/18

    125

    2553 ( 7 2/2553) ( )

    Computer Programming using JavaAdvanced Statements and Applications - 09

    public static void starF(int n) {

    } //End of method

    public static void starG(int n) {

    } //End of method

    public static void starH(int n) {

    } //End of method

    st ar F( 11)

    ************ ** ** ** *

    * ** ** ** ** ************

    []

    st ar G( 11)

    ************* *** * * ** * * ** * * ** * ** * * ** * * ** * * *** *************

    []

    st ar H( 6)

    ****

    ************

    ********************

    []

  • 7/25/2019 Java Programming - Chapter 9 (in Thai)

    14/18

    126

    2553 ( 7 2/2553) ( )

    Computer Programming using Java 09 - Advanced Statements and Applications

    public static void starI(int n) {

    } //End of method

    public static void starJ(int n) {

    } //End of method

    // 4 (0 -5 )public static void main(String[] args) {

    } //End of main

    } //End of class

    star I ( 11)

    ***********************************

    ****

    ************

    ********************

    []

    star J ( 11)

    * *** ***** ******* ********* ********************* *****

    **** ******* ***** *** *

    []

  • 7/25/2019 Java Programming - Chapter 9 (in Thai)

    15/18

    127

    2553 ( 7 2/2553) ( )

    Computer Programming using JavaAdvanced Statements and Applications - 09

    14 [] revWords() (Words) "We love you" "eW evol uoy"(15 )

    15 [] (50 )import java.util.Scanner;

    public class OperationsOfArrays {

    //removeDuplicatedMembers() (10 )

    message.txt

    I evoL avaJ gni mmargor Psi hT si yM t sri F mar gor PdnA t I osl a si ym TSAL mar gorP. . .

    I Love J ava Progr ammi ngThi s i s My Fi r st Pr ogramAnd I t al so i s my LAST Progr am. . .

  • 7/25/2019 Java Programming - Chapter 9 (in Thai)

    16/18

    128

    2553 ( 7 2/2553) ( )

    Computer Programming using Java 09 - Advanced Statements and Applications

    //unionArray() - () (10 )

    //intersectArray() ( ) -(10 )

  • 7/25/2019 Java Programming - Chapter 9 (in Thai)

    17/18

    129

    2553 ( 7 2/2553) ( )

    Computer Programming using JavaAdvanced Statements and Applications - 09

    //complementArray() ( ) (10 )

  • 7/25/2019 Java Programming - Chapter 9 (in Thai)

    18/18

    130

    Computer Programming using Java 09 - Advanced Statements and Applications

    //printArray() (Comma) - { 1, 2, 3, 4, 5 }{ 5, 12, 7, 0 } (5 )

    public static void main(String[] args) {

    int a[] = { 7, 7, 2, 4, 5, 6, 5, 3, 2, 3 , 0, 10};int b[] = { 2, 3, 4, 2, 1, 1, 4, 5, 6, 5, 3, 2, 3 };

    //(5 )

    (1)

    ab(2)ab (3)

    Union ab(4) Intersect ab(5)

    Complement ab

    } //End of main} //End of class