int a = ..., b = ...; 

          (a + b)    Math.abs( a - b ) 
int max = ------- + -------------------
             2               2 

Kevin Jiaxing  Runxia  Mary-Ann Zejun  Ryan    Jashan 
Bakr  Santiago Matthew Rui      Serena Jingyun Andrew

Welcome to DrJava.  Working directory is C:\Users\dgerman
> (3 + 5) / 2
4
> (3 + 4) / 2
3

> (3 + 5) / 2 + Math.abs(3 - 5) / 2
5
> (3 + 4) / 2 + Math.abs(3 - 4) / 2
3
> (3 + 104) / 2 + Math.abs(3 - 104) / 2
103


          (a + b) +  Math.abs( a - b ) 
int max = -----------------------------
             2               2 

if (a < b) {
 System.out.println( b ); 
} else {
 System.out.println( a ); 
}

// ----------------------------------------------

if (a < b) {
 a = b; 
}

System.out.println( a ); 

The if statement contains two blocks, the second of which 
is identified by the keyword else and is optional. There is
a shortcut that says if one of the blocks contains only one
statement the curly braces are not needed. 


// ----------------------------------------------

if (a < b) 
 a = b; 

System.out.println( a ); 

Not sure you are completely clear on the meaning of blocks. 

int x = 3, y = 5;
System.out.println( x + y ); 
System.out.println( x + " " + y); 

int x = 3;
String y = "5";
System.out.println( x + y ); 

char x = '3';
String y = "5";
System.out.println( x + y ); 

char x = '3', y = '5';
System.out.println( x + y ); 

int x = 3;
char y = '5';
System.out.println( x + y ); 
System.out.println( (char) (x + y) ); 

public class Tuesday {
  public static void main(String[] args) {
    int a = 3; 
    {
      System.out.println( a ); // expected: 3  
      int b = 4; // can't redefine a
      System.out.println( b ); // expected: 4
    }
    System.out.println( a ); // expected: 3
    // System.out.println( b ); // b not available here 
    {
      System.out.println( a ); // expected: 3
      int b = 23; 
      System.out.println( b ); // expected: 23
    }
  }
}

Here's a problem with if statements: 

https://www.cs.indiana.edu/classes/c212-dgerman/fall2016/resources/rctmpx/3657.html

Problem 5.26 in the book (p. 222)

double gpa = ...; 

if (gpa >= 1.5) {
  if (gpa < 2) {
    System.out.println("Probation."); 
  } else { 

  } 
} else {
  System.out.println("Failing."); 
}

// ----------------------------------------

double gpa = ...; 

if (gpa >= 1.5) {
  if (gpa < 2) 
    System.out.println("Probation."); 
} else 
  System.out.println("Failing."); 

At this stage I'm still OK but I keep going... 

// ----------------------------------------

double gpa = ...; 

if (gpa >= 1.5)
  if (gpa < 2) 
    System.out.println("Probation."); 
else 
  System.out.println("Failing."); 

So at this stage the else dangles to the inner if.

public class Tuesday {
  public static void main(String[] args) {
    double gpa = 2.1;
    if (gpa >= 1.5)
      if (gpa < 2)
      System.out.println("Probation.");
    else
      System.out.println("Failing.");    
  }
}

Run this and you get: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Tuesday
Failing.


Nested if statements are quite common. 

Be careful and use curly braces, avoid dangling else situations... 

https://www.cs.indiana.edu/classes/c212-dgerman/fall2016/resources/rctmpx/3659.html

Sometimes people need to look at 5.16 as well. 

https://www.cs.indiana.edu/classes/c212-dgerman/fall2016/resources/rctmpx/3660.html

I suggest you try to capture the essence of P5.8 in one boolean expression:

  (a) the only input (independent) variable is: int year; 

  (b) you need to calculate a true or a false for the given year

Now look at this code and tell me what happens: 

int i = 1;

if (i <= 10) {
  i = i + 1; 
  System.out.println( i ); 


Let's make one change:

int i = 1; // INIT 

while (i <= 10) { // COND 
  i = i + 1;                  // BODY 
  System.out.println( i );    // BODY 


// https://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/AnatomyMurder2.jpg/800px-AnatomyMurder2.jpg

The first one prints 2

The second one prints 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 

Loops: while, for, do-while. 


INIT;

while ( COND ) {
  BODY; 
}

So that's the basic anatomy of a while loop. 

for (INIT; COND; ...) {
  BODY;
}

So if we don't dissect the body further this is enough. 

int i = 1; // INIT 

while (i <= 10) { // COND 
  i = i + 1;                  // BODY 
  System.out.println( i );    // BODY 


Same thing:

for (int i = 1; i <= 10;    ) {
  i = i + 1; 
  System.out.println( i );   


Here it is, run if you don't believe me:

public class Tuesday {
  public static void main(String[] args) {
    for (int i = 1; i <= 10;    ) {
      i = i + 1;
      System.out.print( " " + i );
    }
    // expected: 2 3 4 5 6 7 8 9 10 11> 
  }
}

for and while loops are entirely equivalent; while loops are more appropriate 
when you don't know ahead of time how many steps your loop will take whereas in
case where the loop's steps are more like counting a for loop seems more likely
to be preferred. 

you can embed if statements and loops just use careful nesting of blocks

public class Tuesday {
  public static void main(String[] args) {
         // INIT    COND
    for (int i = 1; i <= 10;    ) {
      System.out.print( " " + i ); // BODY

      i = i + 1; // STEP 
    }
    // expected: 1 2 3 4 5 6 7 8 9 10> 
  }
}

So now I can say:

for ( INIT ; COND; STEP ) {
  BODY
}

public class Tuesday {
  public static void main(String[] args) {
    for (int i = 1; i <= 10; i = i + 1) {
      System.out.print( " " + i );
      // i = i + 1;    
    }
    // expected: 1 2 3 4 5 6 7 8 9 10> 
  }
}

The equivalent while now looks as follows: 

INIT;
while (COND) {
  BODY;
  STEP; 
}

public class Tuesday {
  public static void main(String[] args) {
    System.out.println( args[0] ); 
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> java Tuesday
java.lang.ArrayIndexOutOfBoundsException: 0
    at Tuesday.main(Tuesday.java:3)
> java Tuesday what
what
> java Tuesday oh boy
oh


public class Tuesday {
  public static void main(String[] args) {
    String first = args[0]; 
    int size = Integer.parseInt( first ); 
    System.out.println( size ); 
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> java Tuesday
java.lang.ArrayIndexOutOfBoundsException: 0
    at Tuesday.main(Tuesday.java:3)
> java Tuesday 3
3
> java Tuesday nothing
java.lang.NumberFormatException: For input string: "nothing"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at Tuesday.main(Tuesday.java:4)


public class Tuesday {
  public static void main(String[] args) {
    String first = args[0]; 
    int size = Integer.parseInt( first ); 
    // System.out.println( size ); 
    for (int j = 0; j < size; j = j + 1) { 
      for (int i = 0; i < size; i = i + 1) {
        System.out.print(" *");  
      }
      System.out.println(); 
    }
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> java Tuesday 5
 * * * * *
 * * * * *
 * * * * *
 * * * * *
 * * * * *
> java Tuesday 7
 * * * * * * *
 * * * * * * *
 * * * * * * *
 * * * * * * *
 * * * * * * *
 * * * * * * *
 * * * * * * *
> java Tuesday 3
 * * *
 * * *
 * * *
> java Tuesday 12
 * * * * * * * * * * * *
 * * * * * * * * * * * *
 * * * * * * * * * * * *
 * * * * * * * * * * * *
 * * * * * * * * * * * *
 * * * * * * * * * * * *
 * * * * * * * * * * * *
 * * * * * * * * * * * *
 * * * * * * * * * * * *
 * * * * * * * * * * * *
 * * * * * * * * * * * *
 * * * * * * * * * * * *


public class Tuesday {
  public static void main(String[] args) {
    String first = args[0]; 
    int size = Integer.parseInt( first ); 
    // System.out.println( size ); 
    for (int j = 0; j < size; j = j + 1) { 
      for (int i = 0; i < size; i = i + 1) {
        if (i == j || i + j == size-1)
          System.out.print(" *");  
        else 
          System.out.print("  "); 
      }
      System.out.println(); 
    }
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> java Tuesday 11
 *                   *
   *               *  
     *           *    
       *       *      
         *   *        
           *          
         *   *        
       *       *      
     *           *    
   *               *  
 *                   *
> java Tuesday 17
 *                               *
   *                           *  
     *                       *    
       *                   *      
         *               *        
           *           *          
             *       *            
               *   *              
                 *                
               *   *              
             *       *            
           *           *          
         *               *        
       *                   *      
     *                       *    
   *                           *  
 *                               *
> java Tuesday 5
 *       *
   *   *  
     *    
   *   *  
 *       *


--