Osnove JAVA Programiranja Zbirka

Embed Size (px)

Text of Osnove JAVA Programiranja Zbirka

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    1/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    2/318

    500

    978-86-7912-190-5

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    3/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    4/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    5/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    6/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    7/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    8/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    9/318

    H

    K

    [email protected]

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    10/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    11/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    12/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    13/318

    1

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    14/318

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    15/318

    H

    H

    Test

    public class Test {

    . . .

    public static void main(String[] args) {

    . . .

    }}

    H Test.txt

    H Test.class

    H Test.java

    H Main.java

    H Main.txt

    H .java

    H .obj

    H .class

    H .exe

    Test.java

    H javac Test.javaH compile Test.java

    H prevedi Test.java

    H javap Test.java

    Test.class

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    16/318

    H javac Test

    H izvrsi Test

    H java Test

    H java Test.class

    K

    K

    K

    K

    K

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    17/318

    2

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    18/318

    H

    H

    H

    H

    H

    H

    H

    H

    H import

    H export

    H moduleH package

    K

    K package

    K

    import

    K java.lang

    K /** tekst komentara */

    K // tekst komentara

    K -- tekst komentara

    K /* tekst komentara */

    K ** tekst komentara **

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    19/318

    K

    K

    K

    K

    K

    H

    H

    H

    H

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    20/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    21/318

    3

    K public

    Kstatic

    K void

    K class

    K tacno

    K 9x

    K ekran

    K brojStudenataK znak+ili-

    K 3praseta

    K tri praseta

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    22/318

    K precnik

    K bzvz

    H

    H

    H

    H

    H long

    H int

    H short

    H byte

    K float

    K int

    K long

    K double

    K boolean

    char

    H

    H

    H

    H

    boolean

    K tacno

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    23/318

    K true

    K false

    K 0

    K 1

    K netacno

    Kkredit

    K Kredit

    K KREDIT

    K kamatnaStopa

    K KamatnaStopa

    K kamatna_stopa

    K int duina; int irina;K int duina, irina;

    K int duina; irina;

    K int duina, int irina;

    Java je kul!

    K System.out.println(Java je kul!);

    K System.println("Java je kul!");

    K System.out.writeln("Java je kul!");K System.out.println("Java je kul!");

    K System.out.print("Java je kul!");

    K System.out.printf("Java je kul!");

    x

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    24/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    25/318

    a

    b

    int

    K a / b * b

    K a / (b * b)

    K 1.0 * a / b * b

    K 1.0 * a / (b * b)

    K (double) a / (b * b)

    K float f = -34;

    K int t = 23;

    K short s = 10;

    K int t = (int)false;

    K int t = 4.5;

    double x = 5.5;

    int y = (int)x;

    System.out.println("x je " + x + " i y je " + y);

    H x j e 5 i y je 6

    H x je 6.0 i y je 6.0

    H x j e 6 i y je 6

    H x je 5.5 i y je 5

    H x je 5.5 i y je 5.0

    i

    int

    K i = i + (2 - 1);

    K i = i + 1 ;

    K i += 1;

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    26/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    27/318

    K string s = A;

    K String s = 23;

    K String s = "A";

    K String s = "23";

    c

    String s = "Java";char c = s.charAt(4);

    H a

    H v

    H

    s1 s2 String

    K String s = "neki string";

    K String s3 = s1 + s2;

    K s1 >= s2

    K int i = s1.length;

    K s1.charAt(0) = ?;

    s1 s2 String

    K String s3 = s1 - s2;

    K s1 == s2K boolean b = s1.compareTo(s2);

    K char c = s1[0];

    K char c = s1.charAt(s1.length());

    K char c = s1.charAt(s1.length() - 1);

    s1

    s2

    String

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    28/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    29/318

    K s = (new Double(d)).toString();

    K s = (Double.valueOf(d)).toString();

    H 0)

    K (x > 0) || (x < 0)

    K (x != 0) || (x = 0)

    K (-10 < x < 0)

    true

    x

    x

    H 1 < x < 1 0 0 & & x < 0

    H ((x < 100) && (x > 1)) || (x < 0)

    H ((x < 100) && (x > 1)) && (x < 0)

    H (x != 0) || (x = 0)

    H (1 > x > 100) || (x < 0)

    H (1 < x < 100) || (x < 0)

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    30/318

    x

    (y > 10) && (x++ > 10)

    H

    H

    H

    H

    x

    (y > 10) || (x++ > 10)

    H

    H

    H

    H

    y

    x = 0 ;

    y = (x > 0) ? 10 : -10;

    H

    H

    H

    H

    H

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    31/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    32/318

    int

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    33/318

    4

    r

    K if (r != 0) System.out.println(r*

    r*

    Math.PI);

    K if (r >= 0) System.out.println(r * r * Math.PI);

    K if (r > 0) System.out.println(r * r * Math.PI);

    K if (r > 0) { System.out.println(r * r * Math.PI); }

    K if {r > 0} System.out.println(r * r * PI);

    K if (r 0) System.out.println(Math.pow(r, 2) * Math.PI);

    if

    else

    if (x > 0)

    if (y > 0)

    System.out.println("x > 0 i y > 0");

    else if (z > 0)

    System.out.println("x < 0 i z > 0");

    H x > 0 i y > 0

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    34/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    35/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    36/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    37/318

    public class Test {

    public static void main(String[] args) {

    String s1 = "Java";

    String s2 = "Java";

    if (s1.equals(s2))

    System.out.println("s1 i s2 imaju isti sadraj");

    else

    System.out.println("s1 i s2 imaju razlicit sadraj");

    }

    }

    H

    H s1 i s2 imaju isti sadraj

    H s1 i s2 imaju razlicit sadraj

    public class Test {

    public static void main(String[] args) {

    String s1 = "Java";

    String s2 = s1.toUpperCase();

    if (s1 == s2)

    System.out.println("s1 i s2 ukazuju na isti string");

    else if (s1.equals(s2))

    System.out.println("s1 i s2 imaju isti sadraj");

    else

    System.out.println("s1 i s2 imaju razlicit sadraj");

    }

    }

    H

    H s1 i s2 ukazuju na isti string

    H s1 i s2 imaju isti sadrajH s1 i s2 imaju razlicit sadraj

    y

    int x = 3, y = 3 ;

    switch (x + 3) {

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    38/318

    case 6: y = 0;

    case 7: y = 1;

    default: y = y + 1 ;

    }

    H

    H

    H

    H

    char ch = a;

    switch (ch) {

    case a:

    case A:

    System.out.print(ch); break;

    case b:

    case B:

    System.out.print(ch); break;

    case c:

    case C:System.out.print(ch); break;

    case d:

    case D:

    System.out.print(ch);

    }

    H abcd

    H a

    H aA

    H A

    int ocena = 15;

    switch (ocena) {

    case 0 :

    System.out.println("ocena je 0");

    break;

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    39/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    40/318

    H

    H 20

    H

    H

    20

    Kako ste?

    int brojac = 0 ;

    while (brojac < 10) {

    System.out.println("Kako ste?");

    brojac++;

    }

    H

    H

    H

    H

    int brojac = 0 ;

    // Tacka A

    while (brojac < 10) {

    System.out.println("Kako ste?");

    brojac++;

    // Tacka B

    }

    // Tacka C

    K brojac < 1 0

    K brojac < 1 0

    K brojac < 1 0

    K brojac < 1 0

    K brojac < 1 0

    K

    brojac < 1 0

    Kako ste?

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    41/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    42/318

    int suma = 0;

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

    suma = suma + i;

    }

    System.out.println(suma);

    H 10

    H 11

    H 12

    H 13

    H 45

    for

    suma

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

    suma = suma + i;

    }

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

    suma = suma + i;

    }

    H

    H

    for

    for ( ; ; ) ;

    H

    H

    public class Test {

    public static void main (String args[]) {

    int i = 0;

    for (i = 0; i < 10; i++);

    System.out.println(i + 4);

    }

    }

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    43/318

    K

    for

    K

    4

    K

    14

    K

    for

    for

    for (i = 0; i < 10; i++) { };

    int stanje = 10;

    while (true) {

    if (stanje < 9) break;

    stanje = stanje - 9;

    }

    H

    H

    suma

    int suma = 0;int i = 0 ;

    do {

    i++;

    suma = suma + i;

    if (suma > 4) break;

    } while (i < 5);

    H 5

    H 6

    H 7

    H8

    int stanje = 10;

    while (true) {

    if (stanje < 9) continue;

    stanje = stanje - 9;

    }

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    44/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    45/318

    spetlja:

    for (int i = 1; i < 10; i++) {

    upetlja:

    for (int j = 1; j < 10; j++) {

    if (i * j > 50)

    continue spetlja;

    System.out.println(i * j);

    // Tacka A

    }

    // Tacka B

    }

    nastavak:

    . . .

    H

    spetlja

    H

    upetlja

    H nastavak

    H

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    46/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    47/318

    *

    ***

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

    *********

    ***********

    *********

    *******

    *****

    ***

    *

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    48/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    49/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    50/318

    K

    protected

    K

    nadi

    K int

    K boolean

    K double

    K

    K

    H public static Main(String[] args)

    H public static Main(String args[])

    H public void main(String[] args)

    H public static void main(String[] args)

    H public static main(String[] args)

    H

    H

    H

    H

    H

    H

    H

    H

    H

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    51/318

    H void

    H return

    H public

    H static

    return

    public static void main(String[] args) {

    int max = 0;

    if (max != 0)

    System.out.println(max);else

    return;

    }

    H

    H

    Math.pow()

    public static void main(String[] args) {

    Math.pow(2, 4);}

    H

    H

    nPrint("a", 4)

    nPrint()

    void nPrint(String poruka, int n) {

    while (n > 0) {

    System.out.print(poruka);

    n--;

    }

    }

    H aaaaa

    H aaaa

    H aaa

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    52/318

    H

    aa

    H a

    nPrint()

    void nPrint(String poruka, int n) {

    while (n > 0) {

    System.out.print(poruka);

    n--;

    }

    }

    k

    int k = 2 ;

    nPrint("Java je kul!", k);

    H

    H

    H

    H

    public class Test {

    public static void main(String[] args) {

    System.out.println(xMetod(5));

    }

    public double xMetod(int n, double t) {

    System.out.println("double");

    return t;

    }

    public int xMetod(int n) {

    System.out.println("int");

    return n;

    }

    }

    H int 5

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    53/318

    H

    double

    5

    H double

    H

    int

    H

    xMetod()

    public class Test {

    public static void main(String[] args) {

    System.out.println(m(2));

    }

    public int m(int n) {

    return n;

    }

    public void m(int n) {

    System.out.println(n);

    }

    }

    H

    m()

    H

    m()

    H 2

    H

    2

    H

    H

    H

    H

    k

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    54/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    55/318

    public static void rMetod(int[] x, int n) {

    System.out.print(x[n - 1] + " ");

    rMetod(x, n - 1);

    }

    }

    H 1 2 3 4 5

    H

    1 2 3 4 5

    x

    H 5 4 3 2 1

    H 5 4 3 2 1

    x

    public class Test {

    public static void main(String[] args) {

    rMetod(3);

    }

    public static void rMetod(int n) {

    if (n > 1) {System.out.print((n - 1) + " ");

    rMetod(n - 1);

    }

    }

    }

    H rMetod()

    H

    1 2 3

    H 3 2 1

    H 1 2

    H

    2 1

    public class Test {

    public static void main(String[] args) {

    rMetod(2);

    }

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    56/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    57/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    58/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    59/318

    H sortiraj(niz)

    H sortiraj(niz, niz.length)

    H sortiraj(niz, niz.length - 1)

    H sortiraj(niz, niz.length + 1)

    public static int nadiBroj(int[] niz, int broj) {

    return nadiBroj(niz, broj, 0, niz.length - 1);

    }

    public static int nadiBroj(int[] niz, int broj,

    int leviKraj, int desniKraj) {

    if (leviKraj > desniKraj) // broj nije naden u nizu

    return -1;

    // Pretraivanje prve ili druge polovine niza

    int sredina = (leviKraj + desniKraj) / 2;

    if (broj < niz[sredina])

    return nadiBroj(niz, broj, leviKraj, sredina - 1);

    else if (broj > niz[sredina])

    return ;

    else

    return sredina;

    }

    H nadiBroj(niz, broj, sredina + 1, leviKraj)

    H nadiBroj(niz, broj, sredina - 1, leviKraj)

    H nadiBroj(niz, broj, desniKraj, sredina + 1)

    H nadiBroj(niz, broj, sredina + 1, desniKraj)

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    60/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    61/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    62/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    63/318

    6

    H

    H

    H

    H

    H

    H

    H

    H

    H method

    H class

    H main

    H object

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    64/318

    H

    H

    H

    H

    K

    K

    K void

    K

    K new

    public class Test {

    public static void main(String[] args) {

    A a = new A();

    a.prikai();

    }

    }

    class A {

    String s;

    public A(String s) {

    this.s = s;}

    public void prikai() {

    System.out.println(s);

    }

    }

    K A

    K A

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    65/318

    K

    K A a =

    new A(); main A a = new A("poruka");

    public class Test {

    public static void main(String[] args) {

    B c = new B(2);

    }

    }

    class B {

    int i;

    public void b(int j) {

    i = j;

    }

    }

    H B

    H

    B

    H B

    int

    H

    Krug k = new Krug()

    H k

    H

    k

    H k Krug

    H k Krug

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    66/318

    public class Test {

    int x;

    public Test(String s) {

    System.out.println("Test");

    }

    public static void main(String[] args) {

    Test t = null;

    System.out.println(t.x);

    }

    }

    H t

    H x

    H

    Test

    H

    t

    H t null

    t.x

    H

    H true null

    H false

    null

    H true

    null

    H false

    null

    H false

    void

    K

    K

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    67/318

    K

    K

    K

    public class Test {

    public static void main(String[] args) {

    double precnik;

    final double PI= 3.15169;

    double povrina = precnik * precnik * PI;

    System.out.println("Povrina je " + povrina);

    }

    }

    H precnik

    H PI

    H PI

    H PI

    H

    public class Test {

    int x;

    public Test(String s) {System.out.println("Test");

    }

    public static void main(String[] args) {

    Test t = new Test();

    System.out.println(t.x);

    }

    }

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    68/318

    H

    System.out.println()

    H x

    H

    Test

    H

    H 0

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    ? static

    public class Test {

    private int broj;

    public ? int kvadrat(int n) {

    return n * n;

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    69/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    70/318

    n++;

    }

    }

    H xMetod()

    H xMetod()

    H n j e 1

    H n j e 2

    H n j e 3

    println main

    public class Test {

    int i;

    static int s;

    public static void main(String[] args) {

    Test t1 = new Test();

    System.out.println("t1.i je " + t1.i + ", t1.s is " + t1.s);Test t2 = new Test();

    System.out.println("t2.i je " + t2.i + ", t2.s je " + t2.s);

    Test t3 = new Test();

    System.out.println("t3.i je " + t3.i + ", t3.s je " + t3.s);

    }

    public Test() {

    i++;

    s++;

    }

    }

    H t2.i je 1, t2.s je 1H t2.i je 1, t2.s je 2

    H t2.i je 2, t2.s je 2

    H t2.i je 2, t2.s je 1

    println

    main

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    71/318

    public class Test {

    int i;

    static int s;

    public static void main(String[] args) {

    Test t1 = new Test();

    System.out.println("t1.i je " + t1.i + ", t1.s is " + t1.s);

    Test t2 = new Test();

    System.out.println("t2.i je " + t2.i + ", t2.s je " + t2.s);

    Test t3 = new Test();

    System.out.println("t3.i je " + t3.i + ", t3.s je " + t3.s);

    }

    public Test() {

    i++;

    s++;

    }

    }

    H t3.i je 1, t3.s je 1

    H t3.i je 1, t3.s je 2

    H t3.i je 1, t3.s je 3

    H t3.i je 3, t3.s je 1

    H t3.i je 3, t3.s je 3

    public class Test {

    public static void main(String[] args) {

    A a = new A();

    a.n++;

    }

    }

    class A {

    int n;

    private A() {

    }

    }

    H

    A

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    72/318

    H

    A

    H n

    H

    b.n println

    public class Test {

    public static void main(String[] args) {

    int k = 0;

    Brojac b = new Brojac();

    for (int i = 0; i < 100; i++)

    uvecaj(b, k);

    System.out.println("b.n = " + b.n);

    System.out.println("k = " + k);

    }

    public static void uvecaj(Brojac b, int k) {

    b.n++;

    k++;

    }

    }

    class Brojac {

    int n;

    public Brojac(int n) {

    this.n = n;

    }

    public Brojac() {

    this.n = 1;

    }

    }

    H b.n = 101

    H b.n = 100

    H b.n = 99

    H b.n = 98

    H b.n = 0

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    73/318

    k

    println

    public class Test {

    public static void main(String[] args) {

    int k = 0;

    Brojac b = new Brojac();

    for (int i = 0; i < 100; i++)

    uvecaj(b, k);

    System.out.println("b.n = " + b.n);

    System.out.println("k = " + k);}

    public static void uvecaj(Brojac b, int k) {

    b.n++;

    k++;

    }

    }

    class Brojac {

    int n;

    public Brojac(int n) {

    this.n = n;}

    public Brojac() {

    this.n = 1;

    }

    }

    H k = 101

    H k = 100

    H k = 99

    H k = 98H k = 0

    Krug

    public class Krug {

    private double precnik;

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    74/318

    public Krug(double precnik) {

    precnik = precnik;

    }

    }

    H Krug main()

    H Krug

    Krug k = new Krug(2.35) k

    H Krug precnik

    = precnik;

    H Krug

    public class Test {

    private double x;

    public Test(double x) {

    this.t();

    this.x = x;}

    public Test() {

    System.out.println("Podrazumevani konstruktor");

    this(23);

    }

    public void t() {

    System.out.println("Poziv metoda t()");

    }

    }

    K this.t() Test(double x)

    t()

    K this.x Test(double x)

    x

    K this(23) Test()

    System.out.println("Podrazumevani konstruktor");

    K this(23) Test() this(23.0)

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    75/318

    Calendar

    GregorianCalendar java.util

    Tacka Kvadrat Krug

    Krug

    KompleksanBroj

    KompleksanBroj

    main()

    RimskiBroj

    RimskiBroj

    main()

    KockaZaIgru

    Brojac

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    76/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    77/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    78/318

    K Math.random() * 100

    K i + 10

    K i + 6.5

    public class Test {

    public static void main(String[] args) {

    int[ ] a = new int[3];

    System.out.println("a[0] je " + a[0]);}

    }

    H a

    H a

    H

    a[0]

    H a[0] je

    0

    K int i = new int(30);

    K double[] d = new double[30];

    K int[] i = {3, 4, 3, 2};

    K char[] c = new char();

    K char[] c = new char{a, b, c, d};

    K char[] c = {a, b};

    int[] a = {1, 2, 3, 4}

    a.length

    H

    H

    H

    H

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    79/318

    public class Test {

    public static void main(String[] args) {

    int[ ] a = new int[5];

    int i;

    for (i = 0; i < a.length; i++)

    a[i] = i;

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

    }

    }

    H 0 1 2 3 4

    H

    4

    H a[5]

    print

    main

    H i print

    main

    public class Test {

    public static void main(String[] args) {

    int[] a = {120, 200, 016};

    for (int i = 0; i < a.length; i++)

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

    }

    }

    H 120 200 16

    H 120 200 14

    H

    120 200 22

    H 016 16

    lista2

    public class Test {

    public static void main(String[] args) {

    int[] lista1 = {1, 2, 3};

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    80/318

    int[] lista2 = {1, 2, 3};

    lista2 = lista1;

    lista1[0] = 0; lista1[1] = 1; lista2[2] = 2;

    for (int i = 0; i < lista2.length; i++)

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

    }

    }

    H 1 2 3

    H 1 1 1

    H 0 1 2H 0 1 3

    lista1

    public class Test {

    public static void main(String[] args) {

    int[] lista1 = {1, 2, 3};

    int[] lista2 = {1, 2, 3};

    lista2 = lista1;lista1[0] = 0; lista1[1] = 1; lista2[2] = 2;

    for (int i = 0; i < lista1.length; i++)

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

    }

    }

    H 1 2 3

    H 1 1 1

    H 0 1 2

    H 0 1 3

    public class Test {

    public static void main(String[] args) {

    int[] x = {1, 2, 3, 4};

    int[] y = x;

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    81/318

    x = new int[2];

    for (int i = 0; i < y.length; i++)

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

    }

    }

    H 1 2 3 4

    H 0 0

    H 0 0 3 4

    H 0 0 0 0

    public class Test {

    public static void main(String[] args) {

    int[] x = {1, 2, 3, 4};

    int[] y = x;

    x = new int[2];

    for (int i = 0; i < x.length; i++)

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

    }

    }

    H 1 2 3 4

    H 0 0

    H 0 0 3 4

    H 0 0 0 0

    public class Test {

    public static void main(String[] args) {

    final int[] x = {1, 2, 3, 4};

    int[] y = x;

    x = new int[2];

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    82/318

    for (int i = 0; i < y.length; i++)

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

    }

    }

    H 1 2 3 4

    H 0 0

    H x = new int[2]

    x

    final

    H

    x

    x

    final

    int[] lista = new int[5];

    lista = new int[10];

    H

    lista

    H

    lista

    H lista

    H lista

    public class Test {

    public static void main(String[] args) {int[ ] a = new int[4];

    a[1] = 1;

    a = new int[2];

    System.out.println("a[1] je " + a[1]);

    }

    }

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    83/318

    H

    a = new int[2]

    a

    H println a[1]

    H a[1] je 0

    H a[1] je 1

    a

    b

    Hb = Arrays.copyOf(a, a.length);

    H b = Arrays.copyOf(a);

    H Arrays.copyOf(b, a, a.length);

    H Arrays.copyOf(a, b);

    H

    H

    H

    H

    public class Test {

    public static void main(String[] args) {

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

    uvecaj(x);

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

    uvecaj(y[0]);

    System.out.println(x[0] + " " + y[0]);

    }

    public static void uvecaj(int[] a) {

    for (int i = 0; i < a.length; i++)

    a[i]++;

    }

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    84/318

    public static void uvecaj(int n) {

    n++;

    }

    }

    H

    H 1 1

    H 2 2

    H 2 1

    H 1 2

    lista

    public class Test {

    public static void main(String[] args) {

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

    obrniNiz(lista);

    for (int i = 0; i < lista.length; i++)System.out.print(lista[i] + " ");

    }

    public void obrniNiz(int[] a) {

    int[ ] b = new int[a.length];

    for (int i = 0; i < a.length; i++)

    b[i] = a[a.length - 1 - i];

    a = b;

    }

    }

    H 1 2 3 4 5

    H 5 4 3 2 1

    H 5 4 1 2 3

    H 1 2 5 4 3

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    85/318

    public class Test {

    public static void main(String[] args) {

    xMetod(new double[]{3, 3});

    xMetod(new double[5]);

    xMetod(new double[3]{1, 2, 3});

    }

    public void xMetod(double[] a) {

    System.out.println(a.length);

    }

    }

    H new double[]{3,

    3} xMetod()

    H

    new double[5]

    xMetod()

    H

    new double[3]{1,

    2, 3}

    xMetod()

    H a null

    xMetod()

    H

    H

    H

    H

    H

    H

    H

    H

    public static int[] xMetod()

    return

    xMetod()

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    86/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    87/318

    int[] lista2 = obrniNiz(lista1);

    for (int i = 0; i < lista2.length; i++)

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

    }

    public int[] obrniNiz(int[] a) {

    int[ ] b = new int[a.length];

    for (int i = 0; i < a.length; i++)

    b[i] = a[a.length - 1 - i];

    return b;}

    }

    H 1 2 3 4 5

    H 5 4 3 2 1

    H 5 4 1 2 3

    H 1 2 5 4 3

    Krug[] k = new Krug[10]

    H k

    H k Krug

    H

    k

    Krug

    H k Krug

    Test

    public static void main(String[] args)

    args

    "abc"

    Test

    java Test "+" 3 "abc" 2

    H args[0]

    H args[1]

    H args[2]

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    88/318

    H args[3]

    prikai()

    K public void prikai(String... niska, double... broj)

    K public void prikai(double... broj, String ime)

    K public void double... prikai(double d1, double d2)

    K public void prikai(double... broj)

    K public void prikai(int n, double... broj)

    public class Test {

    public static void main(String[] args) {

    double[] d = {1.0, 2.0, 3.0};

    System.out.println(prosek(d));

    System.out.println(prosek(1, 2, 2, 1, 4));

    System.out.println(prosek( new double[]{1, 2, 3}));System.out.println(prosek(1.0, 2.0, 2.0, 1.0));

    }

    public static double prosek (double... brojevi) {

    double zbir = 0;

    for (double e : brojevi)

    zbir = zbir + e;

    return zbir / brojevi.length;

    }

    }

    H prosek(d)

    println

    H

    prosek(1, 2, 2, 1, 4)

    println

    H prosek(new double[]{1,

    2, 3}) println

    H

    prosek(1.0, 2.0, 2.0,

    1.0)

    println

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    89/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    90/318

    public class Test {

    public static void main(String[] args) {

    boolean[][] x = new boolean[3][];

    x[0] = new boolean[1];

    x[1] = new boolean[2];

    x[2] = new boolean[3];

    System.out.println("x[2][2] je " + x[2][2]);

    }

    }

    H new boolean[3][]

    H

    x[2][2]

    null

    H x[2][2] je

    null

    H x[2][2] je

    false

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    91/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    92/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    93/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    94/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    95/318

    8

    H

    H

    H

    H

    K

    K

    K A B A B

    K A B A

    B

    K A B A

    B

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    96/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    97/318

    public class Test extends A {

    public static void main(String[] args) {

    Test t = new Test();

    t.prikai();

    }

    }

    class A {

    String s;

    public A(String s) {

    this.s = s;}

    public void prikai() {

    System.out.println(s);

    }

    }

    K Test

    Test()

    K

    Test

    Test()

    A

    A

    K A

    A()

    public class Test extends A {

    public static void main(String[] args) {

    Test t = new Test();

    }

    }

    class A extends B {

    public A() {

    System.out.println(

    "Pozvan podrazumevani konstruktor klase A");

    }

    }

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    98/318

    class B {

    public B() {

    System.out.println(

    "Pozvan podrazumevani konstruktor klase B");

    }

    }

    H

    H Pozvan podrazumevani konstruktor klase A

    H Pozvan podrazumevani konstruktor klase B

    H Pozvan podrazumevani konstruktor klase A

    Pozvanpodrazumevani konstruktor klase B

    H Pozvan podrazumevani konstruktor klase B Pozvan

    podrazumevani konstruktor klase A

    super

    H

    super

    H

    super

    H super

    public class Test {

    public static void main(String[] args) {

    B b = new B();

    b.m(5);

    System.out.println("b.i je " + b.i);

    }

    }

    class A {

    int i;

    public void m(int i) {

    this.i = i;

    }

    }

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    99/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    100/318

    public class Test {

    public static void main(String[] args) {

    Tim partizan = new Tim("Partizan", "Beograd");

    Tim zvezda = new Tim("Crvena zvezda", "Beograd");

    KoarkakaUtakmica finale;

    finale = new KoarkakaUtakmica(partizan, zvezda);

    finale.domacinPoentirao(3);

    finale.gostPoentirao(2);

    finale.pobednikUtakmice();

    }

    }

    class Tim {

    private String ime;

    private String mesto;

    public Tim(String ime, String mesto) {

    this.ime = ime;

    this.mesto = mesto;

    }

    public String toString() {

    return ime + " (" + mesto + ")";

    }

    }

    class KoarkakaUtakmica {

    public Tim domacin, gost;

    public int brojPoenaDomacina, brojPoenaGosta;

    public KoarkakaUtakmica(Tim d, Tim g) {

    domacin = d;

    gost = g;

    }

    public void domacinPoentirao(int brojPoena) {

    brojPoenaDomacina += brojPoena;}

    public void gostPoentirao(int brojPoena) {

    brojPoenaGosta += brojPoena;

    }

    public void pobednikUtakmice() {

    if (brojPoenaDomacina > brojPoenaGosta)

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    101/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    102/318

    K

    toString()

    A

    System.out.println(a)

    K toString() Object

    System.out.println(o)

    equals()

    Object

    String

    H public boolean equals(String s)

    H public boolean equals(Object o)

    H public static boolean equals(Object o)

    H public boolean equals(String s1, String s2)

    public class Test {

    public static void main(String[] args) {

    Object a1 = new A();

    Object a2 = new A();

    System.out.println(a1.equals(a2));

    }

    }

    class A {

    int x;

    public boolean equals(Object o) {

    A a = (A)o;

    return this.x == a.x;

    }

    }

    H a1.equals(a2)

    Object

    H true

    H false

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    103/318

    public class Test {

    public static void main(String[] args) {

    Object a1 = new A();

    Object a2 = new A();

    System.out.println(a1.equals(a2));

    }

    }

    class A {

    int x;

    public boolean equals(A a) {

    return this.x == a.x;}

    }

    H a1.equals(a2)

    Object

    H true

    H false

    public class Test {

    public static void main(String[] args) {

    A a 1 = new A();

    A a 2 = new A();

    System.out.println(a1.equals(a2));

    }

    }

    class A {

    int x;

    public boolean equals(A a) {

    return this.x == a.x;}

    }

    H a1.equals(a2)

    Object

    H true

    H false

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    104/318

    public class Test {

    public static void main(String[] args) {

    m(new MasterStudent());

    m(new Student());

    m(new Osoba());

    m(new Object());

    }

    public static void m(Student x) {

    System.out.println(x.toString());

    }}

    class MasterStudent extends Student {

    }

    class Student extends Osoba {

    public String toString() {

    return "Student";

    }

    }

    class Osoba extends Object {

    public String toString() {return "Osoba";

    }

    }

    K m(new MasterStudent())

    K m(new Student())

    K

    m(new Osoba())

    K m(new Object())

    class C 1 { }

    class C2 extends C 1 { }

    class C3 extends C 2 { }

    class C4 extends C 1 { }

    C1 c1 = new C1();

    C2 c2 = new C2();

    C3 c3 = new C3();

    C4 c4 = new C4();

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    105/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    106/318

    H ArrayList()

    x ArrayList

    [Java, C++]

    [Java]

    K x.remove("C++")

    K x.remove(0)

    K x.remove(1)

    Kx.remove(2)

    x ArrayList

    [Java, C++]

    x.add("Pascal")

    H [Java, Pascal]

    H [Java, C++, Pascal]

    H [Pascal, Java, C++]

    H [Java, Pascal, C++]

    x

    ArrayList

    H x.first()

    H x.get(0)

    H x.get(1)

    H x.get()

    x

    ArrayList

    H x.getSize()

    H x.getLength(0)

    H x.length(1)

    H x.size()

    H class A { }

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    107/318

    H class A { private A(){ } }

    H final class A { }

    H class A { protected A(){ } }

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    108/318

    PovezanaLista

    ElementListe PovezanaLista

    equals()

    PovezanaLista

    PoliLinija

    PovezanaLista

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    109/318

    9

    K

    K

    K

    K

    K

    H public enum osnovnaBoja {crvena, uta, plava}H public int[] osnovnaBoja = {1, 2, 3}

    H public enum OsnovnaBoja {CRVENA, UTA, PLAVA}

    H public String[] osnovnaBoja = {"crvena", "uta", "plava"}

    H public String boja1 = "crvena", boja2 = "uta", boja3 = "plava"

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    110/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    111/318

    H Kreni

    H Pazi

    A

    H class A { abstract void aMetod() { } }

    H class A { abstract void aMetod(); }

    H abstract class A { abstract void aMetod(); }

    H public class abstract A { abstract void aMetod(); }

    aMetod()

    H public abstract aMetod();

    H public abstract void aMetod();

    H public void abstract aMetod();

    H public void aMetod() { }

    H public abstract void aMetod() { }

    K

    K

    K

    K

    K

    K

    K

    K

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    112/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    113/318

    K A a = new A();

    K A a = new B();

    K B b = new A();

    K B b = new B();

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

    Vocka[] voc e = {new Vocka(2), new Vocka(3), new Vocka(1)};

    java.util.Arrays.sort(vo ce);

    }

    }

    class Vocka {

    private double teina;

    public Vocka(double teina) {

    this.teina = teina;

    }

    }

    H

    Vocka

    H voce

    H voce Vocka

    Comparable

    Vocka

    H

    Radnik

    public class Radnik implements Comparable {

    double plata;

    public int compareTo(Object o) {

    Radnik r = (Radnik) o;

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    114/318

    if (this.plata < r.plata) return -1;

    if (this.plata > r.plata) return +1;

    return 0;

    }

    public static Radnik max(Radnik r1, Radnik r2) {

    if (r1.compareTo(r2) >= 0)

    return r1;

    else

    return r2;

    }

    }

    H

    Radnik

    H Radnik max()

    static

    H Radnik max()

    int

    H Radnik

    K

    K

    public

    K

    K

    A

    Test

    A

    H A.class

    H Test$A.class

    H A$Test.class

    H Test&A.class

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    115/318

    H

    H

    H

    H

    object

    K

    K

    extends

    implements

    K

    K

    Test

    H A.class

    H Test$A.class

    H A$Test.class

    H TestA.class

    H Test$1.class

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    116/318

    Karta

    main()

    Konverzije

    Konvertor

    PrenosnikUtakmice

    Utakmica

    Zapisnik Semafor

    MobilniTelefon

    PrenosnikUtakmice

    GeometrijskiOblik

    Uredaj

    DaljinskiUpravljac

    TV

    DVDPlejer

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    117/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    118/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    119/318

    10

    K

    K

    K

    K

    java.awt

    K Color

    K Font

    K Component

    K JFrame

    K JComponent

    java.swing

    K Color

    K Font

    K Component

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    120/318

    K JFrame

    K JComponent

    import java.awt.*;

    import javax.swing.*;

    public class Test {

    public static void main(String[] args) {

    JButton dugmeOK = new JButton("OK");

    JFrame okvir = new JFrame("Moj okvir");

    okvir.add(dugmeOK);

    okvir.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    okvir.setVisible(true);

    }

    }

    H dugmeOK Button

    H dugmeOK

    JFrame

    H

    new JFrame("Moj okvir")

    H

    import java.awt.*;

    import javax.swing.*;

    public class Test {

    public static void main(String[] args) {

    JFrame okvir = new JFrame("Moj okvir");

    okvir.add(new JButton("OK"));okvir.add(new JButton("Cancel"));

    okvir.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    okvir.setSize(200, 200);

    okvir.setVisible(true);

    }

    }

    H OK

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    121/318

    H

    Cancel

    H OK Cancel OK

    Cancel

    H OK Cancel OK

    Cancel

    import javax.swing.*;

    public class Test {

    public static void main(String[] args) {

    JFrame okvir1 = new JFrame();

    JFrame okvir2 = okvir1;

    JFrame okvir3 = okvir2;

    okvir1.setVisible( true);

    okvir2.setVisible( true);

    okvir3.setVisible( true);

    }

    }

    H

    H

    H

    H

    import javax.swing.*;

    public class Test extends JFrame {

    public static void main(String[] args) {

    JFrame okvir1 = new Test();

    JFrame okvir2 = new Test();JFrame okvir3 = new Test();

    okvir1.setVisible( true);

    okvir2.setVisible( true);

    okvir3.setVisible( true);

    }

    }

    H

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    122/318

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    okvir.setBounds(100, 150, 300, 200);

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    123/318

    K

    K

    K

    K

    H

    H

    H

    H

    H FlowLayout

    H GridLayout

    H BorderLayout

    H Nijedan

    H FlowLayout

    H GridLayout

    H BorderLayout

    H BorderLayout

    H BorderLayout

    p JPanel BorderLayout

    K p.setLayout(new BorderLayout());

    K p.setLayout(BorderLayout());

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    124/318

    K p.setLayout(new BorderLayout(5, 10));

    K p.setLayout(new BorderLayout(BorderLayout.CENTER));

    K p.setLayout(BorderLayout(BorderLayout.CENTER));

    k

    p

    JPanel

    H k.add(p);

    H p.add(k);

    H p.insert(k);

    H p.append(k);

    H k.addContentPane(p);

    H repaint()

    H update()

    H paintComponent()

    H init()

    K Graphics new Graphics()

    K

    Graphics

    K

    paintComponent()

    K paintComponent()

    repaint()

    paintComponent()

    H JLabel

    H JButton

    H JFrame

    H JComponent

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    125/318

    import java.awt.*;

    import javax.swing.*;

    public class Test extends JFrame {

    public Test() {

    add(new MojaKomponenta("Zdravo narode!"));

    }

    public static void main(String[] args) {

    JFrame okvir = new JFrame();okvir.setSize(300, 300);

    okvir.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    okvir.setVisible(true);

    }

    }

    class MojaKomponenta extends JComponent {

    private String poruka;

    public MojaKomponenta(String poruka) {

    this.poruka = poruka;

    }

    public void paintComponent(Graphics g) {

    g.drawString(poruka, 20 ,20);

    }

    }

    K Test

    K Zdravo narode!

    K

    K Zdravo narode!

    new JFrame()

    new Test()

    K Zdravo narode!

    new JFrame() new Test("Zdravo

    narode!")

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    126/318

    import java.awt.*;

    import javax.swing.*;

    public class Test extends JFrame {

    public Test() {

    add(new MojaKomponenta());

    }

    public static void main(String[] args) {

    JFrame okvir = new Test();

    okvir.setSize(300, 300);

    okvir.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);okvir.setVisible(true);

    }

    }

    class MojaKomponenta extends JComponent {

    String poruka;

    public void setPoruka(String poruka) {

    this.poruka = poruka;

    }

    public void paintComponent(Graphics g) {g.drawString(poruka, 20 ,20);

    }

    }

    K Test

    K poruka null

    g.drawString(poruka, 20 ,20) paintComponent()

    K

    K Zdravo narode!

    new Test() new Test("Zdravo

    narode!")

    K Zdravo narode!

    new MojaKomponenta() Test

    (new MojaKomponenta()).setPoruka("Zdravo narode!")

    g

    Graphics

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    127/318

    H g.drawRect(20, 50, 20, 20);

    H g.drawRectFill(20, 20, 20, 50);

    H g.drawRect(20, 20, 20, 50);

    H g.drawRectFill(20, 50, 20, 20);

    g2

    Graphics2D

    K g2.draw(Rectangle2D(20, 20, 20, 50));

    K g2.draw(Rectangle2D.Double(20, 20, 20, 50));

    K g2.draw(new Rectangle2D.Double(20, 20, 20, 50));

    K g2.draw(new Rectangle2D.Float(20, 20, 20, 50));

    K g2.draw(new Rectangle2D.Double(20.0, 20.0, 20.0, 50.0));

    K g2.draw(new Rectangle2D.Float(20.0, 20.0, 20.0, 50.0));

    K g2.draw(new Rectangle2D.Float(20.0F, 20.0F, 20.0F, 50.0F));

    K g2.draw(new Rectangle2D(20, 20, 20, 50));

    g2 Graphics2D

    t

    K Point2D t = new Point2D(getWidth(), getHeight());

    K Point2D t = new Point2D.Double(getWidth(), getHeight());

    K Point2D.Double t = new Point2D.Double(getWidth(), getHeight());

    K Point2D t = new Point2D.Double(0, 0);

    K Point2D t = new Point2D.Double(0, getHeight());

    K Point2D t = new Point2D.Double(getWidth(), 0);

    K Point2D t = new Point2D.Double(getHeight(), getWidth());

    K new Color(0, 0, 0)

    K new Color(0, 266, 0)

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    128/318

    K new Color(255, 255, 255)

    K new Color(1, 2, 3)

    k

    K k.setBackground(Color.PINK);

    K k.setBackground(new Color(0, 128, 128));

    K k.setBackground(Color(0, 128, 128));

    K setBackground(Color.YELLOW)K k.setForeground(Color.RED);

    K new Font("SansSerif", Font.BOLD, 36)

    K new Font("SansSerif", Font.CAPS, 20)

    K new Font("SansSerif", Font.BOLD, 10.5)

    K new Font("Serif", Font.BOLD + Font.ITALIC, 12)

    K new Font("Dialog", Font.PLAIN, 10)

    K new Font("Cyrillic", Font.PLAIN, 12)

    K new Font(Serif, Font.PLAIN, 12)

    H ItemEvent

    H MouseEvent

    H MouseMotionEvent

    H ActionEvent

    H WindowEvent

    H ItemEvent

    H MouseEvent

    H MouseMotionEvent

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    129/318

    H ActionEvent

    H WindowEvent

    K

    K

    K

    EventObject

    K

    java.events

    K

    Event

    Listener

    H getSource()

    H getActionCommand()

    H getTimeStamp()

    H getWhen()

    actionPerformed()

    H

    H

    H

    H

    Object

    H

    EventObject

    K

    K

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    130/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    131/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    132/318

    okvir.setSize(300, 300);

    okvir.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    okvir.setVisible(true);

    }

    }

    H import java.awt.event.*

    import java.awt.*

    H

    Test

    JFrame

    ActionListener

    H

    Dugme OK je pritisnuto

    H

    Dugme OK je pritisnuto

    import java.awt.*;

    import java.awt.event.*;

    import javax.swing.*;

    public class Test extends JFrame {

    public Test() {

    JButton dugmeOK = new JButton("OK");

    JButton dugmeNOK = new JButton("Nije OK");

    add(dugmeOK);

    add(dugmeNOK);

    dugmeOK.addActionListener( this);

    dugmeNOK.addActionListener( this);

    }

    public void actionPerformed(ActionEvent e) {

    System.out.println("Jedno od dva dugmeta je pritisnuto");

    }

    public static void main(String[] args) {

    JFrame okvir = new Test();

    okvir.setSize(300, 300);

    okvir.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    okvir.setVisible(true);

    }

    }

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    133/318

    H

    H

    Test

    ActionListener

    H actionPerformed()

    H Test

    new Test() okvir JFrame

    H

    Jedno od dva dugmeta je pritisnuto

    import java.awt.*;

    import java.awt.event.*;

    import javax.swing.*;

    public class Test extends OkvirRukovalac {

    JButton dugmeOK = new JButton("OK");

    public Test() {

    add(dugmeOK);

    dugmeOK.addActionListener( this);

    }

    public void actionPerformed(ActionEvent e) {

    super.actionPerformed(e);

    if (e.getSource() == dugmeOK)

    System.out.println("Pritisnuto dugme OK");

    }

    public static void main(String[] args) {

    OkvirRukovalac okvir = new Test();

    okvir.setSize(200, 100);

    okvir.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    okvir.setVisible(true);

    }

    }

    class OkvirRukovalac extends JFrame implements ActionListener {

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    134/318

    JButton dugmeNOK = new JButton("Nije OK");

    public OkvirRukovalac() {

    setLayout(new FlowLayout());

    add(dugmeNOK);

    dugmeNOK.addActionListener( this);

    }

    public void actionPerformed(ActionEvent e) {

    if (e.getSource() == dugmeNOK)

    System.out.println("Pritisnuto dugme NOK");

    }

    }

    K

    K

    Pritisnuto dugme OK

    K Pritisnuto dugme NOK

    K

    super.actionPerformed(e)

    actionPerformed()

    Test

    Pritisnuto

    dugme NOK

    H ActionAdapter

    H MouseAdapter

    H WindowAdapter

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    135/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    136/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    137/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    138/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    139/318

    1

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    140/318

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H Test.txt

    H Test.class

    H Test.java

    H Main.java

    H Main.txt

    H .java

    H .obj

    H .class

    H .exe

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    141/318

    H javac Test.java

    H compile Test.java

    H prevedi Test.java

    H javap Test.java

    H javac Test

    H izvrsi Test

    H java Test

    Hjava Test.class

    K

    K

    K

    K

    K

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    142/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    143/318

    2

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

    H

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    144/318

    H

    H

    H

    H

    H

    H import

    H export

    H module

    H package

    K

    K

    package

    K import

    K java.lang

    K /** tekst komentara */

    K // tekst komentara

    K -- tekst komentara

    K /* tekst komentara */

    K ** tekst komentara **

    K

    K

    K

    K

    K

    H

    H

    H

    H

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    145/318

    3

    K public

    K static

    K void

    K class

    K tacno

    K 9x

    K ekran

    K brojStudenata

    K znak+ili-

    K 3prasetaK tri praseta

    K precnik

    K bzvz

    H

    H

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    146/318

    H

    H

    H long

    H int

    H short

    H byte

    K floatK int

    K long

    K double

    K boolean

    H

    H

    H

    H

    K tacno

    K true

    K false

    K 0

    K 1

    K netacno

    K kredit

    K Kredit

    K KREDIT

    K kamatnaStopa

    K KamatnaStopa

    K kamatna_stopa

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    147/318

    K int duina; int irina;

    K int duina, irina;

    K int duina; irina;

    K int duina, int irina;

    K System.out.println(Java je kul!);

    K System.println("Java je kul!");

    K System.out.writeln("Java je kul!");

    K System.out.println("Java je kul!");

    K System.out.print("Java je kul!");

    K System.out.printf("Java je kul!");

    H 17 = x;

    H x = 17;

    H x := 17;

    H x == 17;

    H int x = 17;H int x == 17;

    H int x = 17;

    H int x = 17.0;

    H

    H

    H

    H

    K 1 / 2

    K 1.0/2

    K 1.0/2.0

    K (double) (1/ 2)

    K (double) 1 / 2

    K 1/2.0

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    148/318

    H 2 % 1

    H 15%4

    H 25%5

    H 37%6

    K a / b * b

    K a / (b * b)

    K 1.0 * a / b * b

    K 1.0*

    a / (b*

    b)

    K (double) a / (b * b)

    K float f = -34;

    K int t = 23;

    K short s = 10;

    K int t = (int)false;

    K int t = 4.5;

    H x j e 5 i y je 6H x je 6.0 i y je 6.0

    H x j e 6 i y je 6

    H x je 5.5 i y je 5

    H x je 5.5 i y je 5.0

    K i = i + (2 - 1);

    K i = i + 1 ;

    K i += 1;

    K i = 1 + i ;K i++;

    H Mesec je 09

    H

    Mesec je 9

    H Mesec je 9.0

    H 09

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    149/318

    H System.terminate(0)

    H System.halt(0)

    H System.exit(0)

    H System.stop(0)

    H Math.power(x, y)

    H Math.exp(x, y)

    H Math.pow(x, y)

    H Math.pow(y, x)

    H char c = A;

    H char c = 23;

    H char c = "A";

    H char c = "23";

    K string s = A;

    K String s = 23;

    KString s = "A";

    K String s = "23";

    H a

    H v

    H

    K String s = "neki string";

    K String s3 = s1 + s2;

    K s1 >= s2K int i = s1.length;

    K s1.charAt(0) = ?;

    K String s3 = s1 - s2;

    K s1 == s2

    K boolean b = s1.compareTo(s2);

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    150/318

    K char c = s1[0];

    K char c = s1.charAt(s1.length());

    K char c = s1.charAt(s1.length() - 1);

    H

    H

    H true

    H false

    H javaprogram

    H Java program

    H Java_program

    H Javaprogram

    H

    K i = Integer.parseInt(s);

    K i = (new Integer(s)).intValue();

    Ki = Integer.valueOf(s).intValue();

    K i = Integer.valueOf(s);

    K i = (int)(Double.parseDouble(s));

    K d = Double.parseDouble(s);

    K d = (new Double(s)).doubleValue();

    K d = Double.valueOf(s).doubleValue();

    K d = (double)(Integer.parseInt(s));

    K s = d;K s = d.toString();

    K s = (new Double(d)).toString();

    K s = (Double.valueOf(d)).toString();

    H =

    H 0) && (x > 0)

    K (x > 0) || (x < 0)

    K (x != 0) || (x = 0)

    K (-10 < x < 0)

    H 1 < x < 1 0 0 & & x < 0

    H ((x < 100) && (x > 1)) || (x < 0)

    H ((x < 100) && (x > 1)) && (x < 0)

    H (x != 0) || (x = 0)

    H (1 > x > 100) || (x < 0)

    H (1 < x < 100) || (x < 0)

    H

    H

    H

    H

    H

    H

    H

    H

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    152/318

    H

    H

    H

    H

    H

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    153/318

    public class ImePrezime {

    public static void main(String[] args) {

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

    System.out.println(" | |");

    System.out.println(" | Dejan ivkovic |");

    System.out.println(" | |");System.out.println(" +----------------+");

    }

    }

    public class Inicijali {

    public static void main(String[] args) {

    System.out.println(" ** **");

    System.out.println(" **");System.out.println(" ******* ************");

    System.out.println(" ** ** **");

    System.out.println(" ** ** **");

    System.out.println(" ** ** **");

    System.out.println(" ** ** **");

    System.out.println(" ** ** **");

    System.out.println(" ** ** **");

    System.out.println(" ** ** **");

    System.out.println(" ******* ************");

    }

    }

    public class A4 {

    public static void main(String[] args) {

    final double CM_PO_INCU = 2.54;

    double irinaPapira = 21.0;

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    154/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    155/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    156/318

    System.out.println();

    System.out.print("Hipotenuza pravouglog trougla sa ");

    System.out.print("katetama " + kateta1);

    System.out.print(" i " + kateta2 + " je: ");

    System.out.printf("%8.2f\n", hipotenuza);

    zavrnoVreme = System.currentTimeMillis();

    protekloVreme = (zavrnoVreme - pocetnoVreme)/1000.0;

    System.out.println();

    System.out.print("Vreme izvravanja u sekundama je: ");

    System.out.println(protekloVreme);}

    }

    import java.util.*;

    public class Tren {

    public static void main(String[] args) {

    int pakovanoVreme; // pakovani podaci vremenskog trenutka

    int godina, mesec, dan, sat, minut;

    // Ulaz programa se dobija preko tastature

    Scanner tastatura = new Scanner(System.in);

    // U citavanje podataka vremenskog trenutka

    System.out.print("Unesite dan, mesec, godinu: ");

    dan = tastatura.nextInt();

    mesec = tastatura.nextInt();

    godina = tastatura.nextInt();

    System.out.print("Unesite sat, minut: ");

    sat = tastatura.nextInt();

    minut = tastatura.nextInt();

    // Pomeranje ulevo bitova vremenskih podataka

    godina = godina

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    157/318

    // Pakovanje podataka vremenskog trenutka

    pakovanoVreme = godina | mesec | dan | sat | minut;

    // Prikazivanje pakovanog sadraja na ekranu

    System.out.println("Pakovano vreme: " + pakovanoVreme);

    // Raspakivanje podataka vremenskog trenutka

    godina = pakovanoVreme >>> 20;

    mesec = (pakovanoVreme >>> 16) & 0x0000000f;

    dan = (pakovanoVreme >>> 11) & 0x0000001f;

    sat = (pakovanoVreme >>> 6) & 0x0000001f;

    minut = pakovanoVreme & 0x0000003f;

    // Prikazivanje raspakovanog sadraja na ekranu

    System.out.print("Raspakovano vreme: ");

    System.out.print(dan + "." + mesec + "." + godina);

    System.out.println(" " + sat + ":" + minut);

    }

    }

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    158/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    159/318

    4

    K if (r != 0) System.out.println(r * r * Math.PI);

    K if (r >= 0) System.out.println(r * r * Math.PI);

    Kif (r > 0) System.out.println(r * r * Math.PI);

    K if (r > 0) { System.out.println(r * r * Math.PI); }

    K if {r > 0} System.out.println(r * r * PI);

    K if (r 0) System.out.println(Math.pow(r, 2) * Math.PI);

    H x > 0 i y > 0

    H x < 0 i z > 0

    H x < 0 i z < 0

    H

    H

    H

    H

    To je tacno!

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    160/318

    H

    H

    H

    H

    H

    H Plata je veca od 3000

    H Plata je veca od 4000

    H Plata je veca od 3000 Plata je veca od 4000

    H cena = 0.0

    H

    H

    H cena = 15.0

    H

    H s1 i s2 ukazuju na isti string

    H s1 i s2 ukazuju na razlicite stringove

    H

    H s1 i s2 ukazuju na isti string

    H s1 i s2 ukazuju na razlicite stringove

    H

    H s1 i s2 imaju isti sadraj

    H s1 i s2 imaju razlicit sadraj

    H

    H s1 i s2 imaju isti sadraj

    H s1 i s2 imaju razlicit sadraj

    H

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    161/318

    H s1 i s2 ukazuju na isti string

    H s1 i s2 imaju isti sadraj

    H s1 i s2 imaju razlicit sadraj

    H

    H

    H

    H

    H abcd

    H a

    H aA

    H A

    H ocena je 15

    H ocena je 15 ocena je 15 ili 30

    H

    H Pograna ocena

    H

    break

    H

    default

    switch

    H d

    switch double

    H

    H

    H 20

    H

    H 20

    H

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    162/318

    H

    H

    H

    K brojac < 1 0

    K brojac < 1 0

    K brojac < 1 0

    K

    brojac < 1 0

    K brojac < 1 0

    K

    brojac < 1 0

    H

    H

    H

    H

    H

    H i = 5H

    H i = 1

    H

    for

    H d

    for

    double

    H

    d < 10

    for

    H

    H 10

    H 11

    H 12

    H 13

    H 45

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    163/318

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    164/318

    H

    spetlja

    H

    upetlja

    H

    nastavak

    H

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    165/318

    import java.util.*;

    public class Uskrs1 {

    public static void main(String[] args) {

    Scanner tastatura = new Scanner(System.in);

    System.out.print("Unesite godinu za datum Uskrsa: ");

    int g = tastatura.nextInt();

    int a = g % 1 9 ;

    int b = g % 4;

    int c = g % 7;

    int d = (19 * a + 24) % 30;

    int e = ( 2 * b + 4 * c + 6 * d + 5 ) % 7 ;

    System.out.print("Katoli cki Uskrs je u nedelju, ");

    int f = 2 2 + d + e ;

    if (f > 31)

    System.out.printf("%d. aprila %d.\n", f - 31, g);else

    System.out.printf("%d. marta %d.\n", f, g);

    }

    }

    import java.util.*;

    public class Uskrs2 {

    public static void main(String[] args) {

    Scanner tastatura = new Scanner(System.in);

    System.out.print("Unesite godinu za datum Uskrsa: ");

    int g = tastatura.nextInt();

    int a = g % 1 9 ;

    int b = g % 4;

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    166/318

    int c = g % 7;

    int d = (19 * a + 24) % 30;

    int e = ( 2 * b + 4 * c + 6 * d + 5 ) % 7 ;

    int f = 2 2 + d + e ;

    int m = 3 ;

    if (f > 31) {

    f = f - 3 1 ;

    m = 4;

    }

    if (g == 1954 || g == 1981 || g == 2049 || g == 2076)

    f = f - 7 ;

    System.out.print("Katoli cki Uskrs je u nedelju, ");System.out.printf("%d. %d. %d.\n", f, m, g);

    }

    }

    import java.util.*;

    public class PismoGlava1 {

    public static void main(String[] args) {

    final int PISMO = 0;

    int brojBacanja, ishodBacanja;

    int brojPisma, brojGlava;

    Scanner tastatura = new Scanner(System.in);

    while(true) {

    System.out.print("Unesite broj bacanja novcica: ");

    brojBacanja = tastatura.nextInt();

    if (brojBacanja == 0) break;

    brojPisma = 0;

    brojGlava = 0;

    for (int i = 0; i < brojBacanja; i++) {

    ishodBacanja = (int)(Math.random() + 0.5);

    if (ishodBacanja == PISMO)

    brojPisma++;

    else

    brojGlava++;

    }

    System.out.print("Broj pisma Broj glava ");

    System.out.print("Broj pisma/Broj bacanja ");

    System.out.println("Broj glava/Broj bacanja ");

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    167/318

    System.out.printf("%8d %12d %17.2f %25.2f\n",

    brojPisma, brojGlava,

    (double)brojPisma/brojBacanja,

    (double)brojGlava/brojBacanja);

    }

    }

    }

    public class ZmijskeOci {

    public static void main(String[] args) {

    int brojBacanja = 0; // brojac bacanja dve kocke

    int kocka1; // broj koji je pao na prvoj kocki

    int kocka2; // broj koji je pao na drugoj kocki

    do {

    kocka1 = (int)(Math.random()*6) + 1; // baci prvu kocku

    kocka2 = (int)(Math.random()*6) + 1; // baci drugu kocku

    brojBacanja++; // uracunati bacanje

    System.out.printf("%4d. bacanje: kocka1 = %d, kocka2 = %d\n",

    brojBacanja, kocka1, kocka2);

    } while ((kocka1 != 1) || (kocka2 != 1));}

    }

    import java.util.*;

    public class Niz3n1 {

    public static void main(String[] args) {

    int n; // elementi niza

    Scanner tastatura = new Scanner(System.in);

    System.out.print("Unesite pocetni broj niza: ");

    n = tastatura.nextInt();

    while(n

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    168/318

    System.out.println("mora biti pozitivan!");

    System.out.print("Unesite pocetni broj niza: ");

    n = tastatura.nextInt();

    }

    System.out.println();

    System.out.println(n);

    while(n != 1) {

    if (n % 2 == 0)

    n = n / 2;

    else

    n = 3 * n + 1 ;

    System.out.println(n);}

    }

    }

    import java.util.*;

    public class NZD1 {

    public static void main(String[] args) {

    int x, y; // dati brojevi

    int d; // (najveci) zajednicki delilac

    Scanner tastatura = new Scanner(System.in);

    System.out.print("Unesite dva pozitivna cela broja: ");

    x = tastatura.nextInt();

    y = tastatura.nextInt();

    d = x < y ? x : y; // po cetni delilac

    while((x % d != 0) || (y % d != 0))

    d = d - 1 ;

    System.out.println("NZD(" + x + "," + y + ") = " + d);

    }

    }

  • 7/28/2019 Osnove JAVA Programiranja Zbirka

    169/318

    import java.util.*;

    /** Program prikazuje broj sa najvecim brojem delioca u

    intervalu od 1 do n */

    public class NBD {

    public static void main(String[] args) {

    int brojSaNBD = 1; // broj sa najvecim brojem delioca

    int maxBrojDelioca = 1; // njegov najveci broj delioca

    int brojDelioca; // broj delioca aktuelnog broja

    Scanner tastatura = new Scanner(System.in);

    System.out.println(

    "Unesite gornju granicu intervala brojeva (vecu od 1)");

    System.out.print(

    "u kojem se trai broj sa najvecim brojem delioca: ");

    int n = tastatura.n