vavedenie v java-fmi

119
1 Д И П Л О М Н А Р А Б О Т А На тема: Учебно помагало „Въведение в програмирането с Java“ Дипломант :Христо Александров Хаджиценев Фак.номер : 0701181049 Научен ръководител : доц. Д-р Коста Гъров

Upload: yordanka-stoyanova

Post on 28-Sep-2015

114 views

Category:

Documents


1 download

DESCRIPTION

какво представлява java

TRANSCRIPT

  • 1

    :

    Java

    :

    .: 0701181049

    : . -

  • 2

    :

    1. .

    2. .

    3. .

    4. .

    5. .

    6. .

    7. .

    8. .

    9. .

    10. .

    11. .

    12.

  • 3

    1.

    Java.

    , , .

    Java (

    C C++). ,

    .

    , , -

    .

    Java - .

    , .

    - ,

    ( , ,

    ). ( , ..)

    ( , .).

    Java

    .

    , , Java -

    ( C#, PHP

    C++).

    , , ,

    . ,

    . Java,

    !

    ,

    Java, ,

    .

  • 4

    2. .

    . ()

    . ,

    .

    .

    , .

    , .

    .

    .

    , ,

    , C.

    , . Java, C++,

    C#, VisualBasic, Python, Ruby, PHP .

    Java. -

    .

    Java

    Java Java

    ,

    , Java.

  • 5

    classHelloJava{ publicstaticvoidmain(String[]arguments){ System.out.println("Hello, Java"); } }

    , ,

    "Hello, Java" . ,

    . -

    ,

    .

    Java ?

    :

    - ;

    - main()

    -

    ;

    main()

    .

    HelloJava. -

    class,

    . HelloJava

    main()

    .

    () main(),

    . Java

    main()

    publicstaticvoidmain(String[]arguments)

    :

    , public,

    static void, main

  • 6

    String.

    public static .

    arguments, ,

    . args argv

    ,

    , , ,

    .

    .

    main()

    ,

    .

    System.out println(),

    "Hello, Java".

    Java .

    main()

    , .

    Java !

    , class, public, static

    void , System.out.

    , !

    , Java

    . Class class

    System.out SYSTEM.OUT.

    ,

    , , ..

  • 7

    , -

    , , ,

    .

    .

    classHelloJava{ publicstaticvoidmain(String[]arguments){ System.out.println("Hello, Java"); } } -

    .

    :

    classHelloJava{ publicstaticvoidmain(String[]arguments){ System.out.println("Hello, Java"); } } :

    classHelloJava{publicstaticvoid main(String[]arguments){ System.out.println("Hello, Java");}}

    :

    class HelloJava {public staticvoidmain( String[ ]arguments){ System. out.println("Hello, Java");}

  • 8

    }

    , - .

    Java .

    ,

    .java

    ,

    .

    .

    HelloJava.java

    .

    Java

    Java e -

    . C C++,

    ,

    . Java

    .java.

    Java javac

    , .class

    .

    Java bytecode , .

    Java :

  • 9

    abstract continue for new switch

    assert default goto package synchronized

    boolean do if private this

    break double implements protected throw

    byte else import public throws

    case enum instanceof return transient

    catch extends int short try

    char final interface static void

    class finally long strictfp volatile

    const float native super while

    . const goto. ,

    .

    . -

    . 1.2 strictfp, 1.4

    assert, 1.5 enum

    Java , , , ,

    , , .

    .

    - Java

    .

    .

    , Java.

    , " " "

  • 10

    " (Garbage Collector).

    ,

    .

  • 11

    3. .

    ? ,

    . , ,

    . , ,

    , . ,

    , ,

    .

    , .

    , .

    , ,

    () .

    .

    .

    ,

    .

    :

    - ;

    - ( );

    - (defaultvalue).

    :

    - byte, short, int, long;

  • 12

    - float double

    -

    ;

    boolean

    -

    ;

    char

    -

    ;

    Object

    -

    ;

    String

    - (

    .

    byte,

    short, int, long, float, double, boolean, char, Object String),

    :

    byte 0 -128 +127 short 0 -32768 +32767 int 0 -2147483648 +2147483647

    long 0L -9223372036854775808

    +9223372036854775807

    float 0.0f -3.4+38 +3.4+38 double 0.0d -1.7+308 +1.7+308

    boolean false true false char '\u0000' 0 +65535 Object null String null

    byte, short, int, long, float, double, boolean char

    , Java -

    .

    Object String

    ,

    ( ). ,

    Java, ,

    Java.

  • 13

    byte, short, int long

    .

    , .

    byte

    . 8- ,

    , 2 8, .. 256

    .

    0. , , -128,

    +127.

    short. 16-

    . byte

    ,

    -

    .

    0. , -32768,

    - +32767.

    int

    , ,

    . 32-

    . ,

    , .

    0. , -2147483648,

    +2147483647.

    long.

    64- 0L. L

    , long ( int).

    , long

    , -9223372036854775808,

    +9223372036854775807.

    ,

    ,

    .

  • 14

    byte centuries = 20;

    short years = 2000;

    int days = 730480;

    long hours = 17531520;

    System.out.println(centuries + " centuries is " + years +

    " years, or " + days + " days, or " + hours + " hours.");

    -

    . byte, -

    long

    .

    .

    20 centuries is 2000 years, or 730480 days, or 17531520 hours.

    ,

    , float double

    32-

    .

    ,

    .

    float.

    0.0f 0.0F ( ). "f"

    , float (

    double). 6 9

    ( ). ,

    , -3.4+38, +3.4+38

    , ,

    .

    double.

    64- 0.0d 0.0D.

    15 17 . ,

    , -1.7+308, +1.7+308.

  • 15

    :

    float floatPI = 3.14f; double doublePI = 3.14;

    boolean. ,

    true false. false

    .

    - .

    ,

    , ,

    . ,

    -:

    int a = 1; int b = 2; boolean greaterAB = (a > b); boolean equalA1 = (a == 1); if (greaterAB) { System.out.println("A > B"); } else { System.out.println("A

  • 16

    equalA1. greaterAB true,

    >B, B>A

    .

    .

    char

    . .

    , -.

    ,

    char, 'a', 'b' 'A'

    char symbol = 'a';

    :

    System.out.println( "The code of '" + symbol + "' is: " + (int) symbol); symbol = 'b'; System.out.println( "The code of '" + symbol + "' is: " + (int) symbol); symbol = 'A'; System.out.println( "The code of '" + symbol + "' is: " + (int) symbol); :

    The code of 'a' is: 97 The code of 'b' is: 98 The code of 'A' is: 65

    ()

    .

    String. null.

    , ( ),

    . 12 " ",

    ,

    .

  • 17

    ,

    , :

    String firstName = "Ivan"; String lastName = "Ivanov"; String fullName = firstName + " " + lastName; System.out.println("Hello, " + firstName + "!"); System.out.println("Your full name is " + fullName + "."); :

    Hello, Ivan! Your full name is Ivan Ivanov.

    , .

    Object

    .

    ,

    , :

    Object container = 5; Object container2 = "Five"; System.out.println("The value of container is: " + container); System.out.println("The value of container2 is: " + container2); :

    The value of container is: 5 The value of container2 is: Five.

  • 18

    4. .

    ? ,

    . , ,

    . , ,

    , . ,

    , ,

    .

    , .

    , .

    , ,

    () .

    .

    .

    ,

    . :

    - ;

    - ;

    - .

    :

    - ;

    - ( );

  • 19

    - ( ).

    , :

    - ;

    - ( );

    - , .

    :

    [= ]

    :

    byte centuries = 20; short years = 2000; int days = 730480; long hours = 17531520; float floatPI = 3.141592653589793238f; double doublePI = 3.141592653589793238; boolean isEmpty = true; char symbol = 'a'; String firstName = "Ivan";

    . "=

    ".

    ,

    .

    :

    int firstValue = 5; int secondValue;

  • 20

    int thirdValue; secondValue = firstValue; thirdValue = firstValue = 3;

    Java 2 : .

    (valuetypes)

    .

    , : byte, int, short, long, float,

    double, char, boolean

    (reference types)

    (. . heap),

    . (

    ),

    .

    , .. . ()

    . 1, 2, 4 8 .

    .

    null

    , ,

    , :

    . ,

    , .

    (garbage collector), ,

    .

    , , -

    .

    Object, String, Integer, byte[]. ,

    ,

    . , ,

    , .

  • 21

    1. , -

    byte, short, int long

    2.

    ,

    : 52130; -115; 4825932; 97; -10000.

    float

    double

    3.

    : 34.567839023; 12.345; 8923.1234857;

    3456.091?

    int

    4.

    256

    (256 100 16).

    isMale boolean

    5.

    .

    String "Hello" "World".

    Object.

    ,

    ( , ).

    Object. String

    Object

    .

  • 22

    5. .

    ,

    . Java

    .

    ?

    ,

    .

    . .

    , ,

    .

    , , (+, - , /,

    *

    Java

    ) , , .

    Java :

    - ,

    .

    -

    .

    -

    / .

    - .

    -

    .

  • 23

    -

    .

    , :

    -, +, *, /, %, ++, -- &&, ||, !, ^ &, |, ^, ~, , >>> ==, !=, >, =, >>= +

    (type), instanceof ., new, (), [], ?:

    , , :

    () (unary)

    (binary)

    (ternary) -, , ,

    ,

    .

    (:?)

    Java ,

    . +.

    (

    - ( ).

    int, long, float .),

    .

  • 24

    , /

    .

    :

    int z = 4 + 8;

    System.out.println(z); // 12

    String firstName = "Lachezar";

    String lastName = "Bozhkov";

    String fullName = firstName + " " + lastName;

    System.out.println(fullName);

    +

    , .

    +, -, * .

    , .

    / (integer),

    ( ).

    %. (increment)

    ++ , --

    (decrement) .

    ++ -- (

    ) ,

    ,

    (

  • 25

    )

    , .

    :

    int squarePerimeter = 17;

    double squareSide = squarePerimeter / 4.0;

    double squareArea = squareSide * squareSide;

    System.out.println(squareSide); // 4.25

    System.out.println(squareArea); // 18.0625

    int a = 5;

    int b = 4;

    System.out.println(a + b); // 9

    System.out.println(a + b++); // 9

    System.out.println(a + b); // 10

    System.out.println(a + (++b)); // 11

    System.out.println(a + b); // 11

    System.out.println(14 / a); // 2

    System.out.println(14 % a); // 4

    (true

    false). (&&), (||),

    (^) (!

    Java ,

    :

    ).

    x y !x x && y x || y x ^ y

  • 26

    true true false true true false true false false false true true false true true false true true false false true false false false , , ""

    , , .

    "" , .

    . ,

    true ,

    false. .

    true,

    true.

    true, false

    .

    .

    :

    boolean a = true;

    boolean b = false;

    System.out.println(a && b); // false

    System.out.println(a || b); // true

    System.out.println(!b); // true

    System.out.println(b || true); // true

    System.out.println((5>7) ^ (a==b)); // false

    + (String). ,

  • 27

    . String,

    , String,

    String

    .

    , :

    String first = "Star";

    String second = "Craft";

    System.out.println(first + second); // StarCraft

    String output = first + second + " ";

    int number = 2;

    System.out.println(output + number);

    // StarCraft 2

    String

    .

    println(), .

    .

    output.

    output 2 ( number)

    .

    String, .

    ( ) . StringBuilder StringBuffer ( ) .

    ,

    .

  • 28

    .

    . 55

    00110111

    ,

    ,

    " " -5V,

    " " +5V.

    .

    "

    ", ,

    .

    .

    ,

    , .

    true false ( ),

    , 0 1 ().

    , "" (&),

    "" (|), (~) "" (^

    ).

    .

    :

    short a = 3; // 0000 0011 = 3

    short b = 5; // 0000 0101 = 5

    System.out.println( a | b); // 0000 0111 = 7

    System.out.println( a & b); // 0000 0001 = 1

    System.out.println( a ^ b); // 0000 0110 = 6

  • 29

    System.out.println(~a & b); // 0000 0100 = 4

    System.out.println(a 1); // 0000 0001 = 1

    a

    b. ,

    . , .

    , , 1 a

    b, 1 . ".

    1 - , 1 -

    . " , a b

    . -

    .

    Java

    . Java :

    - - (>)

    - - (=)

    - - (

  • 30

    - , -

    .

    ,

    Java:

    publicclass RelationalOperatorsDemo {

    publicstaticvoid main(String args[]) {

    int x = 10, y = 5;

    System.out.println("x > y : " + (x > y)); // true

    System.out.println("x < y : " + (x < y)); // false

    System.out.println("x >= y : " + (x >= y)); // true

    System.out.println("x

  • 31

    1 = 2;

    , :

    int x = 6;

    String helloString = " .";

    int y = x;

    6 x.

    helloString,

    x y.

    Java "=", - "==". . .

    ?:

    .

    3 . "?" ,

    ":

    :

    " . ( )

    .

    1 ? 2 : 3

    1 true, 2.

    1 false, 3.

  • 32

    .

    true, ()

    . , false

    ,

    () .

    "?:":

    int a = 6;

    int b = 4;

    System.out.println(a > b ? "a>b" : "b

  • 33

    .

    , ,

    (, ,

    ). :

    int r = (150-20) / 2 + 5;

    double surface = Math.PI * r * r;

    double perimeter = 2 * Math.PI * r;

    System.out.println(r);

    System.out.println(surface);

    System.out.println(perimeter);

    .

    . ,

    .

    :

    70

    15393.804002589986

    439.822971502571

    ,

    ,

    (increment, decrement) .

    :

    int a = 5;

    int b = ++a;

    System.out.println(a); // 6

    System.out.println(b); // 6

  • 34

    : 1. ,

    .

    2. ,

    5 7 .

    3. , 7

    ( ).

    6. .

    ? ,

    .

    (- )

    (- ).

    , -. ,

    .

    . ,

    .

    .

    ,

    . ,

    - , .

  • 35

    ,

    , - . ,

    -

    .

    ?

    , .

    , -

    , ,

    .

    , -

    .

    ?

    .

    Windows :

    , ,

    :

    Start -> (All) Programs -> Accessories -> Command Prompt

    ,

    ,

    .

  • 36

    "Command Prompt" "shell

    "

    " "

    " ,

    , ,

    .

    shell

    "", ,

    .

    " () ""

    ().

    - (CLI Command Line Interface)

    ( cmd.exe

    - (GUI Graphical User Interface)

    ( Windows Explorer).

    ).

    , ,

    ,

    ,

    .

    -

    , .

    Windows . .

    Windows (cmd.exe),

    . dir,

    :

  • 37

    ,

    .

    Windows

    () "Command Prompt" "MS-

    DOS Prompt" ( - Windows).

    :

    . dir

    . cd

    . mkdir

    . rmdir

  • 38

    . type

    . copy

    Windows. :

    - - , "Standard I/O

    , ,

    .

    " e -

    Unix .

    ,

    .

    - Java ,

    .

  • 39

    ,

    " " " ".

    -

    .

    ,

    () ,

    -.

    , ,

    .

    ( Java System.in),

    ( Java System.out)

    ( Java

    System.err

    -.

    ,

    Java.

    ).

    print() println()

    ,

    (, ):

    :

    System.out.println("Hello, this is Java");

    System.out.println(5);

    System.out.println(3.14159265358979);

  • 40

    :

    , System.out.println

    ,

    println() PrintStream (

    PrintStream

    API- Java).

    print() println(), print()

    , ,

    . println() "print line",

    " ". , print()

    ,

    ,

    .

    , "" ,

    .

    print println

    System.out.println("I love");

    :

    System.out.print("this ");

    System.out.print("Book!");

    :

    I love

    this Book!

    , ,

    . , println(),

    "I love" .

  • 41

    print, ,

    "this" "Book!

    " .

    printf()

    printf()

    . "Print Formatted".

    "C",

    ( BCPL).

    printf()

    Java. printf()

    ,

    , "

    ". printf()

    Java

    printf(, , , , )

    :

    printf()

    ,

    :

    String str = "Hello, Java!";

    System.out.print(str);

    System.out.printf("%s", str);

    :

  • 42

    , "Hello, Java!

    ,

    .

    " . ,

    .

    printf().

    printf() . %s ,

    str, %s. printf() System.out, .

    . PrintStream

    :

    .

    String name = "Boris";

    int age = 18;

    String town = "Plovdiv";

    System.out.printf(

    "%s is %d years old from %s!\n", name, age, town);

    :

    printf() ,

    . , ,

    , (%s %d). %s

    , ,

    name. %d, ,

    . %s,

    (town). \n,

    , .

  • 43

    , -

    , - .

    ,

    . "

    " "" .

    ,

    .

    .

    , Java, System.in

    :

    .

    - ;

    - , .

    System.in

    , System - in.

    InputStream. ()

    , .

    - System.in.read()

    .

    System.in.read()

    Java Java ,, JVMJVM

    System.in.readSystem.in.read()()

    System.out.printlnSystem.out.println()()

    STDIN

    STDERR

    STDOUT

    ,

    , . Java

    ,

    .

  • 44

    (STDIN) (STDOUT, STDERR) Java. STDERR

    System.err.println("This is error!");

    . :

    .

    .

    PrintingLetter.java

    publicclass PrintingLetter {

    publicstaticvoid main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.printf("Enter person name: ");

    String person = input.nextLine();

    System.out.printf("Enter book name: ");

    String book = input.nextLine();

    String from = "Authors Team";

    System.out.printf(" Dear %s,%n", person);

    System.out.printf("We are pleased to inform " +

  • 45

    "you that \"%2$s\" is the best Bulgarian book. \n" +

    "The authors of the book wishes you good luck %s!%n",

    person, book);

    System.out.println(" Yours,");

    System.out.printf(" %s", from);

    }

    }

    :

    . ""

    , ,

    .

    .

    1. , int

    2. , "

    .

    r"

    .

  • 46

    3. , , , ,

    . , .

    ,

    .

    4. , (integer

    )

    , , ,

    5 0.

    7. .

    if if-else if if

    if

    -else ,

    ,

    .

    if

    if ( ) {

    , :

  • 47

    }

    : if

    ,

    . .

    -, .

    , : {}

    .

    . ,

    .

    .

    true false.

    true,

    . ,

    false

    if

    , .

    if

    publicstaticvoid main(String[] args) {

    :

    Scanner input = new Scanner(System.in);

    System.out.println("Enter two numbers.");

    int firstNumber = input.nextInt();

    int secondNumber = input.nextInt();

    int biggerNumber = firstNumber;

    if (secondNumber > firstNumber) {

    biggerNumber = secondNumber;

    }

    System.out.printf("The bigger number is: %d%n", biggerNumber);

    }

  • 48

    if

    if

    int a = 6;

    -,

    , ,

    -. , ,

    . -.

    if (a > 5)

    System.out.println(" - 5.");

    System.out.println(" !");

    ,

    if,

    .

    {} if -

    !

    if-else

    if-else

    if ( ) {

    , :

    } else {

    else-

    }

    : if, ,

    , else, else-. else

    ( ).

    -

    .

    true false.

  • 49

    , .

    true, , else-

    .

    , false, else

    if-else

    -

    ,

    .

    :

    x = 3;

    if (x > 3) {

    System.out.println("x - 3");

    } else {

    System.out.println("x - 3");

    }

    : x>3,

    : "x - 3", (else) : "x -

    3". , x=3,

    else-. :

    x - 3

    if

    if-, .

    if if

    -else .

    if if-else if else

    . else -

    if . else if

    .

  • 50

    ,

    .

    ,

    .

    if

    if

    Scanner input = new Scanner(System.in);

    :

    System.out.println(

    "Please enter two numbers (on separate lines).");

    int first = input.nextInt();

    int second = input.nextInt();

    if (first == second) {

    System.out.println("These two numbers are equal.");

    } else {

    if (first > second) {

    System.out.println("The first number is greater.");

    } else {

    System.out.println("The second number is greater.");

    }

    }

    :

    , ,

    -. :

    Please enter two numbers (on separate lines).

    2

  • 51

    4

    The second number is greater.

    switch-case switch

    .

    switch-case ?

    switch

    ( ).

    :

    switch ( ) {

    case --1: ; break;

    case --2: ; break;

    case --3: ; break;

    case --4: ; break;

    default: ;}

    , .

    switch

    (). ,

    ( ). ,

    default .

    switch .

    , case break,

    switch.

    switch , break . ,

  • 52

    case ,

    ,

    case- ,

    .

    break case .

    ,

    switch.

    switch ,

    default break. ,

    default case ,

    switch. default

    break,

    . , default

    ,

    switch .

    switch

    switch

    (,

    ). , int,

    byte, char enum. , ,

    , switch .

    if

    .

    ,

    .

    :

    int number = 6;

  • 53

    switch (number) {

    case 1:

    case 4:

    case 6:

    case 8:

    case 10: System.out.println(" !"); break;

    case 2:

    case 3:

    case 5:

    case 7: System.out.println(" !"); break;

    default: System.out.println(" !");

    }

    case break ,

    6,

    case

    . ,

    case ,

    break.

    :

    1. if-,

    ,

    - .

  • 54

    2. , (+ -)

    , .

    3. , - ,

    .

    4. , -

    5 .

    5. . ,

    , 0. :

    - {-2, -1, 1}, -1 1 0.

    - {3, 1, -7}, 0.

    6. ,

    [1..9] :

    - 1 3, 10.

    - 4 6, 100.

    - 7 9, 1000.

    - 0 9, .

    8. .

    ""?

    . (loop) ,

    .

    , :

  • 55

    - ;

    - .

    , , (infinite).

    while - while.

    while (condition) {

    statements;

    }

    Condition , true fasle.

    loop condition.

    Statements , .

    .

    while ,

    true, ,

    (pre-test loop). , while

    :

    true

    Expression

    Statement(s)

    false

    while

    While .

  • 56

    while ,

    0 9

    :

    int counter = 0;

    while (counter < 10) {

    System.out.printf("Number : %d%n", counter);

    counter++;

    }

    :

    ,

    .

    1 N

    while

    1 N. N .

    num sum 1. num ,

    .

    1, ,

    ,

  • 57

    1 N. Sum .

    num.

    num ( 1 N) "+"

    .

    Scanner input = new Scanner(System.in);

    System.out.print("n = ");

    int n = input.nextInt();

    int num = 1;

    int sum = 1;

    System.out.print("The sum 1");

    while (num < n) {

    num++;

    sum += num;

    System.out.printf("+%d", num);

    }

    System.out.printf(" = %d%n", sum);

    :

    break

    break .

    break,

    ,

    , break .

  • 58

    while break.

    .

    n, ,

    - n. n! :

    - n! = 1*2*3.......(n-1)*n, n>1;

    - 1! = 1;

    - 0! = 1.

    N! , - n:

    - n! = (n-1)!n, 1! = 1.

    , n.

    factorial 1, n . While ,

    , .

    true. break, , n

    1. n,

    factorial, n 1.

    : n*(n-1)*(n-2)**3*2, n=1

    .

    Scanner input = new Scanner(System.in);

    int n = input.nextInt();

    long factorial = 1;

    while (true) {

    if (n == 1) {

    break;

    }

    factorial *= n;

  • 59

    n--;

    }

    System.out.println("n! = " + factorial);

    10 , :

    10

    n! = 3628800

    do-while Do-while while ,

    .

    (post-test loop).

    do-while:

    do {

    statements;

    }

    while (expression);

    do-while :

  • 60

    do-while

    Do-while , ,

    , - .

    n,

    while , do-while.

    .

    1, -

    0. .

    Scanner input = new Scanner(System.in);

    System.out.print("n = ");

    int n = input.nextInt();

    long factorial = 1;

    do {

    factorial *= n;

    n--;

    } while (n > 0);

    System.out.println("n! = " + factorial);

    n=7:

    n = 7

    n! = 5040

    for For- - while do-while,

    - - .

    for- :

  • 61

    for (initialization; test; update) {

    statements;

    }

    , ,

    . for

    . -

    .

    (infinite loop) for

    :

    for ( ; ; ) {

    statements;

    }

    , .

    , ,

    break, .

    for

    For- :

    for (int num = 0; ...; ...) { }

    , .

    -. ""

    .

    for

    For- :

    for (int num = 0; num < 10; ...) {

    }

  • 62

    , . true

    , false

    . loop condition (

    ).

    for- ,

    :

    for (int num = 0; num < 10; num++) {

    }

    .

    - .

    For-

    for-

    . , . 1

    , 10 :

    for (int small=1, large=10; small

  • 63

    continue

    continue - ,

    .

    .

    [1...n],

    7. for-.

    1,

    [1...n]. i

    (i

  • 64

    sum = 141

    for 5 Java

    , .. foreach, for-

    .

    , .

    "",

    .

    for-:

    for (variable : collection) {

    statements;

    }

    , - for-

    - , .

    , for- :

    int[] numbers = {2, 3, 5, 7, 11, 13, 17, 19};

    for (int i : numbers) {

    System.out.printf("%d ", i);

    }

    System.out.println();

    String[] towns = {"Sofia", "Plovdiv", "Varna", "Bourgas"};

    for (String town: towns) {

  • 65

    System.out.printf("%s ", town);

    }

    for- .

    ( ) .

    :

    2 3 5 7 11 13 17 19

    Sofia Plovdiv Varna Bourgas

    .

    - - .

    .

    for .

    , ,

    , false.

    for ,

    . 2 for

    . , ,

    .

    for (initialization; test; update) {

    for (initialization; test; update) {

    statements;

    }

    }

  • 66

    : n

    n , :

    1

    1 2

    1 2 3

    . . .

    1 2 3 . . . n

    for-. ,

    . ,

    "1" (1 , 1 ). "1 2" (2 , 2

    ). , ,

    , .

    :

    - 1 ( , )

    =>col = 1;

    - , .

    =>col

  • 67

    for (int col = 1; col

  • 68

    if ((a + b) == (c + d)) {

    System.out.printf("%d%d%d%d%n", a, b, c, d);

    }

    }

    }

    }

    }

    ( ):

    1. , 1 N.

    N .

    2. , 1 N,

    3 7. N .

    3. ,

    - - .

    4. ,

    ( 52 : 4 13 ).

    5. , N

    N : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,

    144, 233, 377, ...

  • 69

    6. , N!/K! N K (1

  • 70

    0 1 2 3 40 1 2 3 4Array of 5 elementsArray of 5

    Array of 5 elementselements

    Element index

    Element Element indexindex

    Element of an array

    Element of Element of an arrayan arrayarrayarrayarray

    0, 1, 2, ... N-1.

    .

    .

    ,

    .

    .

    , -

    .

    , .

    Java , -

    .

    .

    Java :

    int[] myArray; myArray , (int[]) ..

    . [] , ,

    , .

    , ,

    (reference), null,

    .

  • 71

    () new

    Java new,

    () :

    int[] myArray = newint[6]; 6 .

    , (heap) 6

    :

    HeapStack

    [I@42e816myArray

    0 0 0 000 0 0 0 0000 1 2 3 40 1 2 3 4 55

    , myArray

    (0x42e816) ,

    . (

    . .heap).

    .

    .

    ,

    . Java ,

    (default initial value)>

    0

    ( null false ).

    , .

    .

    (array literal expression):

  • 72

    int[] myArray = {1, 2, 3, 4, 5, 6}; .

    , .

    ,

    .

    .. .

    ,

    - for .

    :

    myArray[index] = 100;

    100 ,

    index, index .

    ,

    :

    int[] myArray = newint[6];

    myArray[1] = 1;

    myArray[5] = 5;

    ,

    :

    HeapStack

    [I@42e816myArray

    0 0 0 510 0 0 0 5100 1 2 3 40 1 2 3 4 55

  • 73

    -, ..

    0. 0, 1 ..

    N , N-1.

    Java

    . ,

    .

    java.lang.ArrayIndexOutOfBoundsException. ,

    .

    .

    for Java .

    , , n

    :

    int n = input.nextInt();

    int[] array = newint[n];

    , .

    . n

    ..

    :

    for (inti = 0; i < n; i++) {

    array[i] = input.nextInt();

    }

  • 74

    , .

    , ,

    . . ,

    .

    ,

    :

    String[] array = { "one", "two", "three", "four" };

    System.out.println(array);

    ,

    ( ).

    :

    [Ljava.lang.String;@42e816

    for :

    String[] array = { "one", "two", "three", "four" };

    for (int index = 0; index < array.length; index++) {

    System.out.printf("element[%d] = %s%n", index, array[index]);

    }

    for, array.length ,

    System.out.printf,

    . :

    element[0] = one

    element[1] = two

  • 75

    element[2] = three

    element[3] = four

    , - :

    String[] array = { "one", "two", "three", "four" };

    System.out.println(java.util.Arrays.toString(array));

    ,

    , :

    [one, two, three, four]

    for

    for

    . ,

    :

    int[] array = newint[] {1, 2, 3, 4, 5};

    for (int index = 0; index < array.length; index++) {

    array[index] = 2 * array[index];

    }

    System.out.println(Arrays.toString(array));

    for

    , .

    .. , for

    . , .

    :

  • 76

    for (int index = 0; index < array.length; index += 2) {

    array[index] = array[index] * array[index];

    }

    ,

    .

    .

    , , for

    , ,

    . :

    int[] array = newint[] {1, 2, 3, 4, 5};

    System.out.print("Reversed: ");

    for (int i = array.length - 1; i >= 0; i--) {

    System.out.print(array[i] + " ");

    }

    .

    ,

    "". , ,

    .

    8 8 (8 8

    ).

    " "? ""?

    Java

    . .

    .

  • 77

    int[].

    int[], int[], ..

    :

    int[][] twoDimentionalArray;

    ,

    ( ).

    .

    :

    int[][][] threeDimentionalArray;

    ,

    ,

    - .

    .

    :

    int[][] intMatrix;

    float[][] floatMatrix;

    String[][][] strCube;

    .

    [].

    new

    , :

    int[][] intMatrix = newint[3][4];

    float[][] floatMatrix = newfloat[8][2];

  • 78

    String[][][] stringCube = new String[5][5][5];

    intMatrix 3 int[]

    3 4. ,

    .

    , :

    458217093621

    458217093621

    0 1 2 3013

    0

    . m n,

    m*n .

    ,

    .

    .

    :

    int[][] matrix = {

    {1, 2, 3, 4}, // row 0 values

    {5, 6, 7, 8}, // row 1 values

    }

    2 4

    .

    , .. .

    , .

  • 79

    () ,

    .

    ( )

    , . ,

    , .

    , :

    int[][] myInts = { {1, 2, 3}, {91, 92, 93, 94}, {2001, 2002} };

    , .

    3 , . Java

    ,

    :

  • 80

    ,

    .

    :

    int[][] matrix = {

    { 1, 2, 3, 4 },

    { 5, 6, 7, 8 },

    };

    matrix.length.

    ,

    ( ). i-

    matrix[i].length.

    :

    int[][] matrix = {

    { 1, 2, 3, 4 }, // row 0 values

    { 5, 6, 7, 8 }, // row 1 values

    };

    for (introw = 0; row < matrix.length; row++) {

    for (int col = 0; col < matrix[0].length; col++) {

    System.out.printf("%d ", matrix[row][col]);

    }

    System.out.println();

    }

  • 81

    ,

    . ,

    , ,

    .

    .

    , ,

    :

    1 2 3 4

    5 6 7 8

    1. , 20

    5. .

    2. ,

    .

    3. , char

    ( ) -

    .

    4. ,

    . : {2, 1, 1, 2, 3, 3, 2, 2, 2, 1} {2, 2, 2}.

    5. ,

    . : {3, 2, 3, 4, 2, 2, 4} {2, 3, 4}.

    6. , N K,

    N . K ,

    .

  • 82

    7. , ,

    , ( ).

    : {4, 3, 1, 4, 2, 5, 8}, S=11 {4, 2, 5}.

    8. ,

    .

    . (4,4):

    1010663311131399552215151212884416161414111177

    1010663311131399552215151212884416161414111177

    77665544881515141433991616131322

    10101111121211

    77665544881515141433991616131322

    10101111121211

    1313121255441414111166331515101077221616998811

    1313121255441414111166331515101077221616998811

    1616121288441515111177331414101066221313995511

    1616121288441515111177331414101066221313995511

    b)b)b)

    d)*d)*d)*c)c)c)

    a)a)a)

    9. , (n, m).

    .

    (3,3), .

    10. .

    ?

    () ,

    .

    . ,

    ,

    .

  • 83

    ,

    . , -

    . 0,

    1, 2, 3, 4, 5, 6, 7, 8 9, . ( ,

    -

    , , ..

    ).

    , . ,

    , .

    , 10 .

    ,

    0 1.

    .

    , : -

    , ? ,

    ,

    (.. ) ,

    .. . , ,

    -

    , (e =

    2,718281828), .

    , ,

    .

    , 2 3. , 3

    - , .

    , ,

    .

  • 84

    , , ()

    . ,

    . 351 1

    1, 1024 1000.

    ,

    . A(p) = (a(n)a(n-1)...a(0),a(-

    1)a(-2)...a(-k)

    =

    =k

    nmmmp TaA )(

    ) :

    Tm m- .

    Tm=Pm

    =

    =k

    nm

    mmp PaA )(

    , ,

    , A(p)

    (a(n)a(n-1)...a(0)) (a(-1)a(-2)...a(-k)), a

    M={0,1,2,..., p-1}. , ,

    -

    ( , -)

    , . ,

    (-) ,

    , - .

    2, 8, 10 16 -

    ,

    0 15 :

    0000 0 0 0 0001 1 1 1

  • 85

    0010 2 2 2 0011 3 3 3 0100 4 4 4 0101 5 5 5 0110 6 6 6 0111 7 7 7 1000 10 8 8 1001 11 9 9 1010 12 10 A 1011 13 11 B 1100 14 12 C 1101 15 13 D 1110 16 14 E 1111 17 15 F

    , ,

    .

    , , . ,

    ,

    . -

    .

    .

    .

    :

    I 1 V 5 X 10

  • 86

    L 50 C 100 D 500 1000

    ,

    ,

    :

    1. , ,

    - ,

    . :

    III=3, MMD=2500.

    2. ,

    , . :

    IX=9, XML=1040, MXXIV=1024.

    , ..

    .

    10. 10.

    ( )

    (100=1), (101=10),

    (102

    95031 = (9x10

    =100) .. ,

    - .

    . 95031,

    :

    4) + (5x103) + (0x102) + (3x101) + (1x100

    , 95031 ,

    )

  • 87

    . ,

    . ,

    . ,

    ,

    .

    , ..

    . -

    . ,

    , 2.

    . ,

    0 1.

    , ,

    , ,

    . 1110(2)

    , ,

    2 .

    (2

    . ,

    , . ,

    , (..

    "").

    0=1),

    (21=2),

    (22=4) .. 8-, (27=128).

    16-, (215=32768). 8

    (0 1) 256 , 28=256.

    16 65536 ,

    216=65536.

  • 88

    .

    148. : 1, 4 8,

    :

    10010100

    148 = (1x2

    (2)

    7) + (1x24) + (1x22)

    0, , 1, .

    ,

    .

    ,

    , .

    ,

    , .

    11001(2).

    11001

    :

    (2) = 1x24+ 1x23+0x22 +0x21+1x20

    = 16

    =

    (10)+ 8(10)+ 1(10) = 25

    , 11001

    (10)

    (2)= 25

    , 2 , ,

    ( ). ,

    , ,

    .

    (10)

    ,

    . -

    .

  • 89

    ( ).

    ,

    . :

    1001(2)

    = ((1.2+0).2+0).2+1 = 2.2.2+1 = 9

    ,

    .

    . ,

    .

    148. ,

    ( 2). ,

    ( ),

    . , . :

    148:2=74 0;

    74:2=37 0;

    37:2=18 1;

    18:2=9 0;

    9:2=4 1;

    4:2=2 0;

    2:2=1 0;

    1:2=0 1;

    ,

    , , :

    10010100

  • 90

    .. 148(10) = 10010100

    (2)

    16,

    16 ()

    0 15 .

    ,

    0 9 A F.

    :

    A=10, B=11, C=12, D=13, E=14, F=15

    , D2,

    1F2 F1, D1E .

    160

    - , 161 , 162

    D1E

    ..

    . :

    (16) = *160 + 1*161 + D*162 = 14*1 + 1*16 + 13*256 = 3358(10)

    16 . :

    .

    3358 / 16 = 209 + 14 ()

    209 / 16 = 13 + 1 (1)

    13 / 16 = 0 + 13 (D)

    D1E(16)

    .

    ,

    , (

    ). ,

  • 91

    .

    ,

    . :

    : 1110011110(2)

    1.

    .

    : 0011 1001 1110.

    2.

    39E(16) 1110011110

    .

    (2) = 39E(16)

    .

    , .

    (, )

    .

    .

    ,

    . ,

    , ,

    .

    . - , .

    , 0 1.

    8 ,

    .

    ,

    (2, 4 8),

  • 92

    . ,

    .

    , .

    ,

    : .

    . - 1

    0 .

    () .

    .

    1, 2, 4 8 .

    , ,

    . n

    [0, 2n-1].

    , a :

    1 0 28-1 0 255 2 0 216-1 0 65 535 4 0 232-1 0 4 294 967 295 8 0 264-1 0 9 223 372 036 854 775 807

    158,

    10011110 (2)

    1. 1 :

    :

    1 0 0 1 1 1 1 0 2. 2 :

  • 93

    0 0 0 0 0 0 0 0 1 0 0 1 1 1 1 0

    ,

    , -

    . ,

    1 , .

    , :

    1 -27 27 -128 127 -1 2 -215 215 -32 768 32 767 -1 4 -231 231 -2 147 483 648 2 147 483 647 -1

    8 -263 263-9 223 372 036 854 775 808

    -1 9 223 372 036 854 775 807

    , ,

    .

    : [-2n-1, 2n-1

    : - .

    , .

    :

    -1].

    , .

    3 00000011.

    -3 10000011.

    : ,

    ( ).

    ,

    , .

  • 94

    .

    , .

    .. 2n

    -127 1 1111111,

    1 0000000.

    . :

    3 0 0000011, 0

    1111100.

    : ,

    ( ) . :

    -127 1 0000001.

    - : BCD (Binary Coded Decimal).

    . ,

    .

    .. .

    .

    , -

    .

    .

    , ,

    .

  • 95

    , strictfp.

    , .

    ,

    decimal C#, money SQL Server number(10,2)

    Oracle. Java .

    (

    ), BigDecimal,

    , - float

    double. ,

    :

    import java.math.BigDecimal;

    publicclass Precision {

    publicstaticvoid main(String[] args) {

    double sum = 0.0d;

    BigDecimal bdValue = new BigDecimal("0.1");

    BigDecimal bdSum = new BigDecimal("0.0");

    for(int i=1; i

  • 96

    ,

    sum double

    BigDecimal. :

    Double sum is: 0.9999999999999999

    BigDecimal sum is: 1.0

    1. 151, 35, 43, 251 -0,41 .

    2. 1111010110011110(2)

    3. 2A3E, FA, FFFF, 5A0E9

    .

    .

    4. , .

    5. , .

    6. ,

    .

    11. .

  • 97

    ""? (method) , .

    ,

    , . ,

    .

    . ,

    , ,

    .

    :

    publicstaticdouble getRectagnleArea(

    double width, double height) {

    double area = width * height;

    return area;

    }

    ? , .

    , , ,

    .

    - -

    , ,

    - - ,

    .

    , , , 20%

    , , .

    . , ,

  • 98

    , .

    .

    , ,

    .

    .

    ,

    Java-.

    () , ,

    , .

    .

    ,

    , ,

    , .

    ,

    .

    , ,

    , , ,

    "{" "}",

    . , "

    ", ,

    "{" "}". ,

    ( -).

  • 99

    "{" "}" .

    - main()

    , ?

    HelloJava.java

    publicclass HelloJava {

    publicstaticvoid main(String[] args) {

    System.out.println("Hello Java!");

    }

    }

    ,

    . :

    [public] [static] ([])

    :

    - .

    - .

    -

    .

    , main()

    HelloJava :

    publicstaticvoid main(String[] args)

  • 100

    , void (.. ),

    main, , ,

    String[] args.

    ,

    .

    ,

    ,

    "(" ")".

    , , : , .

    ( "()"

    ). ,

    .

    "(" ")", , .

    , -

    , void, ,

    . - ,

    .

    , ,

    .

    , , () ,

    .

    -, printLogo:

  • 101

    publicstaticvoid printLogo() {

    System.out.println("Sun Microsystems");

    System.out.println("www.sun.com");

    }

    , ,

    , Sun:

    - .

    - camelCase, .. ,

    , .

    -

    .

    , , .

    , Java-

    Sun.

    :

    print

    getName

    playMusic

    setUserName

    , . , ,

    , ,

    , .

    :

    - .

  • 102

    - . - camelCase. -

    - .

    ,

    "{" "}", .

    publicstatic() {

    }

    , , .

    ,

    .

    , ,

    :

    publicstaticvoid printLogo() {

    System.out.println("Sun Microsystems");

    System.out.println("www.sun.com");

    }

    ,

    , :

    .

    , ,

    , .

  • 103

    , , , ,

    ,

    .

    , , .

    , ,

    . ,

    , .

    , - ,

    :

    publicstaticvoid printMax(float number1, float number2) {

    float max = number1;

    if (number2 > number1) {

    max = number2;

    }

    System.out.println("Maximal number: " + max);

    }

    , ,

    ,

    :

    float var1, var2;

    ,

    .

  • 104

    , :

    publicstaticvoid printMax(float var1, var2)

    , :

    publicstaticvoid printMax(float var1, float var2)

    ,

    . , ,

    , .

    ,

    .

    :

    printSign(-5);

    printSign(balance);

    printMax(100, 200);

    ,

    , .

    - , ,

    , (

    " ").

  • 105

    , , ,

    ( "

    ").

    , var1 var2,

    :

    publicstaticvoid printMax(float var1, float var2)

    , -23.5 100,

    :

    printMax(100, -23.5);

    - , Java

    , .

    , . , ,

    .

    ,

    .

    , :

    publicstaticvoid printNumber(int numberParam) {

    numberParam = 5;

    System.out.println("in printNumber() method, after the "

    + "modification, numberParam is: " + numberParam);

    }

    main():

    publicstaticvoid main(String[] args) {

  • 106

    int numberArg = 3;

    printNumber(numberArg);

    System.out.println("in the main() method number is: " +

    numberArg);

    }

    3 numberArg,

    numberParam. printNumber(),

    numberParam 5.

    numberArg, ,

    numberParam . ,

    printNumber() 5. ,

    printNumber(), main()

    numberArg , .

    :

    in printNumber() method, after the modification numberParam is:5

    in the main() method number is: 3

    ( ) ,

    ( ), .

    , "". ,

    , ()

    ( ).

    .

    ( ) :

  • 107

    arrArg: int[]1 2 31 2 3[I@e48e1b

    variable object

    arrArg: int[]1 2 31 2 31 2 31 2 3

    , ,

    . . ,

    , , -

    , ,

    . ( )?

    ?

    - , :

    modifyArr(),

    , 5

    , :

    publicstaticvoid modifyArr(int[] arrParam) {

    arrParam[0] = 5;

    System.out.print("In modifyArr() the param is: ");

    System.out.println(Arrays.toString(arrParam));

    }

    , main(),

    modifyArr():

    publicstaticvoid main(String[] args) {

    int[] arrArg = newint[] { 1, 2, 3 };

    System.out.print("Before modifyArr() the argument is: ");

    System.out.println(Arrays.toString(arrArg));

    modifyArr(arrArg);

  • 108

    System.out.print("After modifyArr() the argument is: ");

    System.out.println(Arrays.toString(arrArg));

    }

    ? :

    Before modifyArr() the argument is: [1, 2, 3]

    In modifyArr() the param is: [5, 2, 3]

    After modifyArr() the argument is: [5, 2, 3]

    , modifyArr(),

    arrArg , [1,2,3], [5,2,3].

    ?

    , ,

    ,

    , .

    , , .

    (var-args)

    , ,

    , , ,

    , ,

    .

    ,

    , , ,

    , .

    , -, ,

    .

  • 109

    , double[],

    :

    publicstaticvoid printTotalAmountForBooks(double[] prices) {

    double totalAmount = 0;

    for (double singleBookPrice : prices) {

    totalAmount += singleBookPrice;

    }

    System.out.println("The total amount of all books is: " +

    totalAmount);

    }

    , , ,

    double

    .

    Java 5.0, ,

    ,

    , ,

    , , .

    , , ,

    :

    double[] prices = newdouble[] { 3, 2.5 };

    printTotalAmountForBooks(prices);

    ,

    :

    printTotalAmountForBooks(3, 2.5);

    printTotalAmountForBooks(3, 5.1, 10, 4.5);

  • 110

    , , (var-args).

    :

    publicstatic()

    , , ,

    void. , ,

    void, (int,

    float, double, ) ( String ),

    , .

    , , ,

    , .

    :

    publicstaticdouble calcSquareSurface(double sideLength)

    , double.

    , ,

    Java ,

    . , ,

    .

    ,

    :

    String companyLogo = getCompanyLogo();

  • 111

    , ,

    .

    ,

    :

    float totalPrice = getSinglePrice() * quantity;

    ,

    :

    System.out.println(getCompanyLogo());

    , getCompanyLogo(),

    println(). getCompanyLogo()

    , , "Sun Microsystems". Java

    "" , ,

    , :

    System.out.println("Sun Microsystems");

    -, ,

    int, String, .. ,

    void, ,

    .

    return

    , ,

    return, :

    public static () {

  • 112

    return ;

    }

    , . :

    publicstaticint multiply(int number1, int number2) {

    int result = number1 * number2;

    return result;

    }

    , , return,

    result.

    , ,

    ( "What time is it?"). ,

    , .

    , , "The

    time is HH:mm now.", , mm

    . ,

    "Incorrect time!".

    , ,

    :

    - .

    - .

    - .

    ,

    , ,

    .. . , 0

    23 , 0 59 .

    ( )

  • 113

    , ,

    .

    :

    DataValidation.java

    import java.util.Scanner;

    publicclass DataValidation {

    publicstaticvoid main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.println("What time is it?");

    System.out.print("Hours: ");

    int hours = input.nextInt();

    System.out.print("Minutes: ");

    int minutes = input.nextInt();

    boolean isValidTime =

    validateHours(hours) &&validateMinutes(minutes);

    if (isValidTime) {

    System.out.printf(

    "The time is %d:%d now.%n", hours, minutes);

    } else {

    System.out.println("Incorrect time!");

  • 114

    }

    }

    publicstaticboolean validateHours(int hours) {

    boolean result = (hours >= 0) && (hours < 24);

    return result;

    }

    publicstaticboolean validateMinutes(int minutes) {

    boolean result = (minutes >= 0) && (minutes = 0) && (hours < 24);

    return result;

    }

    , ,

    . validateMinutes(),

    , boolean.

    , -,

    0 59 , true, false:

    publicstaticboolean validateMinutes(int minutes) {

  • 115

    boolean result = (minutes >= 0) && (minutes

  • 116

    time is HH:mm now.", HH

    hours, mm minutes. else

    , "Incorrect

    time!".

    :

    What time is it?

    Hours: 17

    Minutes: 33

    The time is 17:33 now.

    :

    What time is it?

    Hours: 33

    Minutes: -2

    Incorrect time!

    - , .

    strong cohesion. ,

    -

    -.

    !

    - , . ,

    , sortNumbers(), number()

    processing() method2().

    , -

    .

  • 117

    -

    ( ,

    ), findSmallestElement() sort(int[] arr)

    readInputData().

    - Java .

    camelCase, .. ,

    , .

    - , ,

    .

    .

    , , .

    .

    -

    (- , ). ,

    , , ,

    (

    ). loose

    coupling.

    - , - " ".

    , ,

    -

    "" .

    - , ,

    , , .

    , ,

    , , ,

    . ,

  • 118

    , .

    1. , "Hello,

    !" ( "Hello, Peter!"). ,

    .

    2. getMax() (int) ,

    - . ,

    - ,

    getMax().

    3. ,

    . : 512 "two";

    1024 "four".

    4. ,

    . ,

    .

    5. , ,

    , -, - .

  • 119

    12.

    , .

    Java.

    Java

    .

    1. 2. . Java Java ? main() main()

    Java ! Java

    3. . ?

    ()

    4. . ?

    5. . ? Java

    :

    6. . ? ? ? Windows

    - print() println() printf() printf()

    System.in

    7. . if if-else if if if

    if-else if-else

    if if

    switch-case switch-case ? switch

    8. . ""? while while 1 N break do-while do-while for for for For- continue for

    9. . ""? () new . for " "? ""?

    10. . ?

    11. . ""? ?- - (var-args) return

    12.