0 .. 2 0 0 12!03 *2!) #1 % Java - /0 . ! 1 * 1 12 Java 1.1211. Java 5 public class Point {...

download 0 .. 2 0 0 12!03 *2!) #1 % Java -    /0 . ! 1 * 1 12 Java 1.1211. Java 5 public class Point { private int x,y; private Color myColor; public int currentX() {

of 13

  • date post

    27-Apr-2018
  • Category

    Documents

  • view

    217
  • download

    3

Embed Size (px)

Transcript of 0 .. 2 0 0 12!03 *2!) #1 % Java - /0 . ! 1 * 1 12 Java 1.1211. Java 5 public class Point {...

  • Java

    Franz Marc, Rehe im Walde (II), 1913-14

    ;

    ;

    Java 2

    M y x: 5

    M y y : 20

    M y co lo r: b lack

    Th in g s I can d o :

    mo v e

    rep o rt x

    rep o rt y

    M y x: 2 0

    M y y : 10

    M y co lo r: d ar k g rey

    Th in g s I can d o :

    mo v e

    rep o rt x

    rep o rt y

    M y x: 1 7

    M y y : 25

    M y co lo r: l ig h t g rey

    Th in g s I can d o :

    mo v e

    rep o rt x

    rep o rt y

    Java

    (object)

    (fields)

    (methods)

    (instance)

    (class)

    Java 3

    M y x: 1 0

    M y y : 50

    M y co lo r: b lack

    Th in g s I can d o :

    mo v e

    rep o rt x

    rep o rt y

    :

    ..

    ,

    Java 4

  • Java

    Java 5

    public class Point {

    private int x,y;

    private Color myColor;

    public int currentX() {

    return x;

    }

    public int currentY() {

    return y;

    }

    public void move(int newX, int newY) {

    x = newX;

    y = newY;

    }

    }

    f ield def in it ions

    m ethod def init ions

    Java

    char: 0..216-1, 'a', '\n', ,

    Unicode

    byte: -27..27-1

    short: -215..215-1

    int: -231..231-1,

    long: -263..263-1, L

    float: IEEE 32-bit standard, F

    double: IEEE 64-bit standard,

    (.., 1.2, 1.2e-5, 1e3)

    boolean: true false

    : void null

    Java 6

    Java

    (reference types)

    , .. Point

    (interface)

    , .. Point[] int[]

    Java 7

    (strings)

    : String

    String,

    Java 8

    M y d ata : Hello there

    M y len g th : 11

    T h in g s I can d o :

    rep o rt my len g th

    rep o rt my ith ch a r

    m ake an u p p ercas e

    v ers io n o f

    my s e lf

    etc .

    "Hello there"

  • int: +, -, *, /, %,

    double: +, -, *, /,

    Java 9

    Java

    1+2*3

    7

    15/7

    2

    15%7

    1

    -(6*7)

    -42

    Java

    6.0*7.0

    42.0

    15.0/7.0

    2.142857142857143

    +

    String

    Java 10

    Java

    "123" + "456"

    "123456"

    "The answer is " + 42

    "The answer is 42"

    "" + (1.0/3.0)

    "0.3333333333333333"

    1 + "2"

    "12"

    "1" + 2 + 3

    "123"

    2 + 2 + "2"

    "42"

    : ,

    == !=

    , double

    ( ML)

    Java 11

    Java

    1

  • (side effect)

    ,

    ML (pure)

    Java

    Java 13

    , Rvalues Lvalues

    a = b: a b

    a = 42 , 42 = a;

    = :

    .. a, 42, a+42, f() ( void), ..

    =

    : .. a d[42], 42 a+42

    rvalue lvalue

    Java 14

    Java

    Java 15

    Java

    Java

    a = a+b

    a += b

    a = a-b

    a -= b

    a = a*b

    a *= b

    Java

    Java

    a = a+1

    a++

    a = a-1

    a--

    x = y y

    x

    Java 16

    Java

    a+(x=b)+c

    a, b c

    x, b

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

    d

    a, b c, d

    a=b=c

    c

    a b, c

  • Java 17

    Java

    a++

    a

    a

    ++a

    a

    a

    a--

    a

    a

    --a

    a

    a

    (instance method)

    Java 18

    Java

    s.length()

    String s

    s.equals(r)

    true s r false

    r.equals(s)

    s.toUpperCase()

    String String s

    s.charAt(3)

    3 String s (,

    )

    s.toUpperCase().charAt(3)

    3 String s

    (class method calls)

    (class methods)

    Java 19

    Java

    String.valueOf(1==2)

    "false"

    String.valueOf(6*7)

    "42"

    String.valueOf(1.0/3.0)

    "0.3333333333333333"

    :

    :

    ,

    :

    Java 20

    ::= . ()

    ::= . ()

    ::= ()

  • (constructor)

    Java 21

    Java

    new String() String

    new String(s)

    String String s

    new String(chars)

    String chars

    ::= new ()

    new

    (garbage collection)

    Java 22

    : , --;

    Java

    ,

    15

    : .. * +

    : .. < !=

    null

    String

    ( )

    Java 23

    char int

    ( )

    int double

    Java 24

    Java

    'a'+'b'

    195

    1/3

    0

    1/3.0

    0.3333333333333333

    1/2+0.0

    0.0

    1/(2+0.0)

    0.5

  • (expression statements)

    ,

    , ,

    Java

    , .. x == y

    Java 25

    ::= ;

    Java

    speed = 42; 42 speed.

    a++; a 1.

    inTheRed = cost > balance;

    cost balance, inTheRed true, false.

    (compound statements)

    Java 26

    ::= { } < statement-list> ::= |

    Java

    {

    a = 0;

    b = 1;

    }

    a, b.

    {

    a++;

    b++;

    c++;

    }

    a, b, c.

    { }

    .

    Java 27

    ::= ; ::= | =

    boolean done = false;

    done boolean, false.

    Point p;

    p Point. ( .)

    {

    int temp = a;

    a = b;

    b = temp;

    }

    a b.

    if

    else

    Java 28

    ::= if () | if () else

    Java

    if (i > 0) i--;

    i,

    .

    if (a < b) b -= a;

    else a -= b;

    a b

    .

    if (reset) {

    a = b = 0;

    reset = false;

    }

    reset true, a b reset false.

  • while

    expression false

    statement

    (

    , )

    while Java do for loops

    Java 29

    ::= while ()

    Java 30

    Java

    while (a < 100) a += 5;

    a 100, 5 a.

    while (a != b)

    if (a < b) b -= a;

    else a -= b;

    a b

    , . ( .)

    while (time > 0) {

    simulate();

    time--;

    }

    time

    , simulate

    time.

    while (true) work();

    work

    , .

    return

    return

    (

    void)

    return

    Java 31

    ::= return ; | return;

    Java 32

  • : ConsCell

    Java 33

    /**

    * A ConsCell is an element in a linked list of ints.

    */

    public class ConsCell {

    private int head; // the first item in the list

    private ConsCell tail; // rest of the list, or null

    /**

    * Construct a new ConsCell given its head and tail.

    * @param h the int contents of this cell

    * @param t the next ConsCell in the list, or null

    */

    public ConsCell(int h, ConsCell t) {

    head = h;

    tail = t;

    }

    Java 34

    /**

    * Accessor for the head of this ConsCell.

    * @return the int contents of this cell

    */

    public int getHead() {

    return head;

    }

    /**

    * Accessor for the tail of this ConsCell.

    * @return the next ConsCell in the list, or null

    */

    public ConsCell getTail() {

    return tail;

    }

    }

    ConsCell

    cons ML

    Java :

    ML :: , -

    Java

    ConsCell

    ML length . Java

    , null

    Java 35 Java 36

    /**

    * An IntList is a list of ints.

    */

    public class IntList {

    private ConsCell start; // list head, or null

    /**

    * Construct a new IntList given its first ConsCell.

    * @param s the first ConsCell in the list, or null

    */

    public IntList(ConsCell s) {

    start = s;

    }

    /**

    * Cons the given element h onto us and return the

    * resulting IntList.

    * @param h the head int for the new list

    * @return the IntList with head h, and us as tail

    */

    public IntList cons (int h) {

    return new IntList(new ConsCell(h, start));

    }

  • Java 37

    /**

    * Get our length.

    * @return our int length

    */

    public int length() {

    int len = 0;

    ConsCell cell = start;

    while (cell != null) { //