Howdy. 

There are four primitive types in Java: 

  (a) byte short int long

  (b) float double 

  (c) char

  (d) boolean

Let's create and work with some String:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> "whatever"
"whatever"
> "whatever".substring(3, 5)
"te"
> "whatever".substring(2, 5)
"ate"
> "whatever".substring(4)
"ever"
> "whatever".substring(2)
"atever"
> "whatever".substring("whatever".length()-2)
"er"


We could have done this:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> String a = "something";
> a
"something"
> a.substring(3, 6)
"eth"


How about this:

> a.substring(3, 4)
"e"
> a.charAt(3)
'e'


Now the nature of characters is revealed through these experiments: 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> 1 + 2
3
> "1" + "2"
"12"
> "what" + "ever"
"whatever"
> "12" + "345"
"12345"
> "1" + "2"
"12"
> "a" + "b"
"ab"
> "1" + 2
"12"
> 'ab'
Lexical error at line 1, column 3.  Encountered: "b" (98), after : "\'a"
> 'a' + 'b'
195
> '1' + '2'
99
> ('a' + 'a') / 2
97
> 'a' + 'b' - 'b'
97
> 'a'
'a'
> (int)'a'
97
> 'a' + 0
97
> '1' + 0
49
> '0' + 0
48
> ' ' + 0
32
> '{' + 0
123
> 'A' + 0
65
> char c = '4';
> c
'4'
> c - '0'
4


Two more expressions:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> 'a'
'a'
> (int)'a'
97
> (char)97
'a'


Give me some examples of operators:

+ - / % *         primitive operators

Math.sqrt(...)    static method from class Math implements square root operator 

&& || !           boolean operators 

< <= > >= == !=   relational operators  

Let's discuss the boolean operators:

 a       !a
-------------
true     false 
false    true 


a     b     a && b 
-------------------
true  true  true 
true  false false 
false true  false 
false false false 


a     b     a || b 
-------------------
true  true  true
true  false true 
false true  true 
false false false

public class One {
  public static void main(String[] args) {
    System.out.println( Math.random() ); // random number in [0, 1) 
  }
}

produces:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run One
0.44595196898408684


Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run One
0.9395175590446114


Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run One
0.27283054454778466


and so on...

public class One {
  public static void main(String[] args) {
    // System.out.println( Math.random() ); // random number in [0, 1) 
    if (Math.random() < 0.5) 
      System.out.println("Heads.");
    else
      System.out.println("Tails."); 
  }
}

This runs as follows:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run One
Heads.


Here's a variation of this program:

public class One {
  public static void main(String[] args) {
    // System.out.println( Math.random() ); // random number in [0, 1) 
    if (Math.random() < 0.5) {
      System.out.println("        _.-'~~`~~'-._");
      System.out.println("     .'`  B   E   R  `'.");
      System.out.println("    / I               T \\");
      System.out.println("  /`       .-'~\"-.       `\\");
      System.out.println(" ; L      / `-    \\      Y ;");
      System.out.println(";        />  `.  -.|        ;");
      System.out.println("|       /_     '-.__)       |");
      System.out.println("|        |-  _.' \\ |        |");
      System.out.println(";        `~~;     \\\\        ;");
      System.out.println(" ;  INGODWE /      \\\\)P    ;");
      System.out.println("  \\  TRUST '.___.-'`\"     /");
      System.out.println("   `\\                   /`");
      System.out.println("     '._   1 9 9 7   _.'");
      System.out.println(" jgs    `'-..,,,..-'`");
    } else
      System.out.println("Tails."); 
  }
}

Run this program three times: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run One
        _.-'~~`~~'-._
     .'`  B   E   R  `'.
    / I               T \
  /`       .-'~"-.       `\
 ; L      / `-    \      Y ;
;        />  `.  -.|        ;
|       /_     '-.__)       |
|        |-  _.' \ |        |
;        `~~;     \\        ;
 ;  INGODWE /      \\)P    ;
  \  TRUST '.___.-'`"     /
   `\                   /`
     '._   1 9 9 7   _.'
 jgs    `'-..,,,..-'`
> run One
        _.-'~~`~~'-._
     .'`  B   E   R  `'.
    / I               T \
  /`       .-'~"-.       `\
 ; L      / `-    \      Y ;
;        />  `.  -.|        ;
|       /_     '-.__)       |
|        |-  _.' \ |        |
;        `~~;     \\        ;
 ;  INGODWE /      \\)P    ;
  \  TRUST '.___.-'`"     /
   `\                   /`
     '._   1 9 9 7   _.'
 jgs    `'-..,,,..-'`
> java One
Tails.
> java One
Tails.
> java One
Tails.
> java One
        _.-'~~`~~'-._
     .'`  B   E   R  `'.
    / I               T \
  /`       .-'~"-.       `\
 ; L      / `-    \      Y ;
;        />  `.  -.|        ;
|       /_     '-.__)       |
|        |-  _.' \ |        |
;        `~~;     \\        ;
 ;  INGODWE /      \\)P    ;
  \  TRUST '.___.-'`"     /
   `\                   /`
     '._   1 9 9 7   _.'
 jgs    `'-..,,,..-'`
> java One
Tails.
> java One
Tails.
> java One
        _.-'~~`~~'-._
     .'`  B   E   R  `'.
    / I               T \
  /`       .-'~"-.       `\
 ; L      / `-    \      Y ;
;        />  `.  -.|        ;
|       /_     '-.__)       |
|        |-  _.' \ |        |
;        `~~;     \\        ;
 ;  INGODWE /      \\)P    ;
  \  TRUST '.___.-'`"     /
   `\                   /`
     '._   1 9 9 7   _.'
 jgs    `'-..,,,..-'`
> java One
        _.-'~~`~~'-._
     .'`  B   E   R  `'.
    / I               T \
  /`       .-'~"-.       `\
 ; L      / `-    \      Y ;
;        />  `.  -.|        ;
|       /_     '-.__)       |
|        |-  _.' \ |        |
;        `~~;     \\        ;
 ;  INGODWE /      \\)P    ;
  \  TRUST '.___.-'`"     /
   `\                   /`
     '._   1 9 9 7   _.'
 jgs    `'-..,,,..-'`
> java One
Tails.
> java One
Tails.
> java One
        _.-'~~`~~'-._
     .'`  B   E   R  `'.
    / I               T \
  /`       .-'~"-.       `\
 ; L      / `-    \      Y ;
;        />  `.  -.|        ;
|       /_     '-.__)       |
|        |-  _.' \ |        |
;        `~~;     \\        ;
 ;  INGODWE /      \\)P    ;
  \  TRUST '.___.-'`"     /
   `\                   /`
     '._   1 9 9 7   _.'
 jgs    `'-..,,,..-'`
> java One
        _.-'~~`~~'-._
     .'`  B   E   R  `'.
    / I               T \
  /`       .-'~"-.       `\
 ; L      / `-    \      Y ;
;        />  `.  -.|        ;
|       /_     '-.__)       |
|        |-  _.' \ |        |
;        `~~;     \\        ;
 ;  INGODWE /      \\)P    ;
  \  TRUST '.___.-'`"     /
   `\                   /`
     '._   1 9 9 7   _.'
 jgs    `'-..,,,..-'`


Now you can solve exercises 22-24 in the attendance questions. 

Also attendance question for the lab today: 25.

--

int x = 18, y = 10;

if (x < 10) 
  if (x > 5) 
    y = 1; 
  else 
    y = 2;

// so y is unchanged (10) 

----------------------(that was 23)----

int x = 18, y = 10; 

if (x < 10) { 
  if (x > 5) 
    y = 1; 
  else 
    y = 2; 
}

// same thing

----------------------(that was 24)----

int x = 18, y = 10; 

if (x < 10) { 
  if (x > 5) 
    y = 1; 
} else 
  y = 2;

// so y does get changed to 2 here 

----------------------(that was 22)----

int x = 18, y = 10; { if (x < 10)  if (x > 5) y = 1; } else y = 2;

// this is not correct, won't compile: else without if because we put the parens that way

proof: 

int x = 18, y = 10; 
{ if (x < 10)  
    if (x > 5) 
      y = 1; 
} else y = 2; // that gets reported as an error 

----------------------(that was made up with djruiz after class today)----

public class One {
  public static void main(String[] args) {
    int x = 18, y = 10; { if (x < 10) if (x > 5) y = 1; } else y = 2; 
  }
}

the report is:

1 error found:
File: C:\Users\dgerman\Desktop\One.java  [line: 3]
Error: 'else' without 'if'

--