Java programs are made out of classes. 

Classes have static and non-static members inside. 

One static member is special: main. 

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

  } 
}

This class is public so it needs to be placed in a file Wednesday.java

javac Wednesday.java -> Wednesday.class which you can run: java Wednesday 

For now we focus on what we write inside main. 

We have spent some time writing expressions, with constants, operators and
variables. We learned to simply expressions and we looked at if statements. 

The basic syntax of an if statement is as follows: 

  if (...) { // branch [1] 
    if (...) { // branch [2] 
      ... 
    } else { // branch [3] 
      ...
    }  
  } else { // branch [4] 
    ... 
  }

The two branches are mutually exclusive. 

The else does not need to be there. 

Nested if statements are fine if they suit your problem. 

There's a Math class in java.lang that has a method called random. 

http://docs.oracle.com/javase/8/docs/api/java/lang/Math.html

Math.random() is a number in [0, 1) uniformy distributed 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> Math.random()
0.8907330031484826
> Math.random()
0.43633905659249794
> Math.random()
0.9675812550979527
> Math.random()
0.6027152579625141
> Math.random()
0.6728914447418617


How do I get random integers in the interval [a, b)

       (int) ( (b - a) * Math.random() + a )

If I want to simulate a dice I will work with a = 1 and b = 7

       (int) ( 6 * Math.random() + 1 )

Welcome to DrJava.  Working directory is C:\Users\dgerman
> (int) ( 6 * Math.random() + 1 )
6
> (int) ( 6 * Math.random() + 1 )
5
> (int) ( 6 * Math.random() + 1 )
6
> (int) ( 6 * Math.random() + 1 )
5
> (int) ( 6 * Math.random() + 1 )
2
> (int) ( 6 * Math.random() + 1 )
3
> (int) ( 6 * Math.random() + 1 )
4
> (int) ( 6 * Math.random() + 1 )
6
> (int) ( 6 * Math.random() + 1 )
3
> (int) ( 6 * Math.random() + 1 )
4
> (int) ( 6 * Math.random() + 1 )
2
> (int) ( 6 * Math.random() + 1 )
2
> (int) ( 6 * Math.random() + 1 )
1
> (int) ( 6 * Math.random() + 1 )
2
> (int) ( 6 * Math.random() + 1 )
3
> (int) ( 6 * Math.random() + 1 )
1
>

Now we put this together

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

    int dice = (int) ( 6 * Math.random() + 1 ); // dice is in {1, 2, 3, 4, 5, 6} 

    // System.out.println( dice ); 

    if ( dice % 2 == 1 ) { // dice is odd 
      if ( dice <= 3 ) { // dice is 1 or 3 
        System.out.println( dice + " is 1 or 3 "); 
      } else { // dice is 5
        System.out.println( dice + " is 5 "); 
      }   
    } else { // dice is even 
      System.out.println( dice + " is 2 or 4 or 6 ");      
    } 
  }
}

I run this several times and it looks like this:

I can eliminate the curly braces

public class Wednesday {
  public static void main(String[] args) {
    int dice = (int) ( 6 * Math.random() + 1 ); // dice is in {1, 2, 3, 4, 5, 6} 

    if ( dice % 2 == 1 ) 
      if ( dice <= 3 ) 
        System.out.println( dice + " is 1 or 3 "); 
      else 
        System.out.println( dice + " is 5 "); 
    else 
      System.out.println( dice + " is 2 or 4 or 6 ");     
  }
}

If the block has only one statement curly braces are not necessary. 

public class Wednesday {
  public static void main(String[] args) {
    int dice = (int) ( 6 * Math.random() + 1 ); // dice is in {1, 2, 3, 4, 5, 6} 
    System.out.println( dice ); 

    if ( dice % 2 == 1 ) { // dice is odd 
      if ( dice <= 3 ) { // dice is 1 or 3 
        System.out.println( dice + " is 1 or 3 "); 
      } else { // dice is 5
        // System.out.println( dice + " is 5 "); 
      }   
    } else { // dice is even 
      System.out.println( dice + " is 2 or 4 or 6 ");      
    } 
  }
}

Summarize this program in English.

Next try simplifying it and we get this from Sara: 

public class Wednesday {
  public static void main(String[] args) {
    int dice = (int) ( 6 * Math.random() + 1 ); // dice is in {1, 2, 3, 4, 5, 6} 
    System.out.println( dice ); 

    if ( dice % 2 == 1 ) { // dice is odd 
      if ( dice <= 3 ) { // dice is 1 or 3 
        System.out.println( dice + " is 1 or 3 "); 
      }  
    } else { // dice is even 
      System.out.println( dice + " is 2 or 4 or 6 ");      
    } 
  }
}

Ke says: 

public class Wednesday {
  public static void main(String[] args) {
    int dice = (int) ( 6 * Math.random() + 1 ); // dice is in {1, 2, 3, 4, 5, 6} 
    System.out.println( dice ); 

    if ( dice % 2 == 1 ) { // dice is odd 
      if ( dice <= 3 ) 
        System.out.println( dice + " is 1 or 3 "); 
    } else { // dice is even 
      System.out.println( dice + " is 2 or 4 or 6 ");      
    } 
  }
}

Joseph says:

public class Wednesday {
  public static void main(String[] args) {
    int dice = (int) ( 6 * Math.random() + 1 ); // dice is in {1, 2, 3, 4, 5, 6} 
    System.out.println( dice ); 

    if ( dice % 2 == 1 ) { // dice is odd 
      if ( dice <= 3 ) 
        System.out.println( dice + " is 1 or 3 "); 
    } else 
      System.out.println( dice + " is 2 or 4 or 6 ");      
  }
}

Can I still remove curly braces?

Ivy says:

public class Wednesday {
  public static void main(String[] args) {
    int dice = (int) ( 6 * Math.random() + 1 ); // dice is in {1, 2, 3, 4, 5, 6} 
    System.out.println( dice ); 

    if ( dice % 2 == 1 ) 
      if ( dice <= 3 ) 
        System.out.println( dice + " is 1 or 3 "); 
      else 
        System.out.println( dice + " is 2 or 4 or 6 ");      
  }
}

Removing the last set of curly braces changes the meaning of the program. 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Wednesday
5
5 is 2 or 4 or 6 
>

So this is the dangling-else problem.

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> int i = 3, j = 5;
> i++
3
> i
4
> i++ + 1 // 4 + 1 and i is 5
5
> i
5
> i++ + i // 5 (and i becomes 6) + 6
11
> i
6
> i++ + i++ // 6 (and i becomes 7) + 7 (and i becomes 8)
13
> i
8
> i++ + ++i // 8 (and i becomes 9) + 10 (and i remains 10)
18
> i
10


If you understand this you can easily solve the two problems in the minute paper. 

--