Since we saw each other last week

  (a) GitHub 

  (b) submit new assignments there (and old) 

  We posted Lab 03, Homework 03, 04, 05. 

  (c) we promise to grade everything this week

Chapters 1-6 for the first exam. 

--

int i = 3, j = 5;

System.out.println( i < 2 ); // false 

int m = (Math.abs(i - j) + i + j) / 2; 

int n; 

if (i < j) {
  n = j;
} else {
  n = i; 
}

System.out.println( n == m ); // true 


With conditionals you can branch in your program. 

import java.util.Scanner;

public class Leap {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in); 
    System.out.print("Year: "); 
    String line = in.nextLine(); 
    int year = Integer.parseInt( line ); 
    if (year < 1582) {
      if (year % 4 == 0) { // multiple of 4
        System.out.println( "Leap year! "); 
      } else {
        System.out.println( "Not a leap year! "); 
      }
    } else {
      if (year % 4 == 0) {
        if (year % 100 == 0) {
          // not finished yet
          if (year % 400 == 0) {
            System.out.println(" Leap year! ");  
          } else {
            System.out.println( "Not leap year! "); 
          }
        } else {
          // no worries: leap year
          System.out.println( "Leap year! "); 
        }
      } else { // not a multiple of 4
        System.out.println( "Not leap year! "); 
      }
    }
  }
}

--

if (...) {
  // if the condition is true 

} else {
  // if the condition is not true




--

public class Leap {
  public static void main(String[] args) {
    int i = 3;
    
    if (i < 10) {
      i = i + 1;
      System.out.println( i ); // 4
    }
   
    System.out.println( i ); // 4
  }
}

Let's change one keyword in the program above: 

public class Leap {
  public static void main(String[] args) {
    int i = 3;
    
    while (i < 10) {
      i = i + 1;
      // System.out.println( i );  
    }
   
    System.out.println( i ); // 10
  }


--

INIT;                  int count = 0; 
while (COND) {         while (count < size) { 
  BODY;                   print a star
  STEP;                   count++; 
}                      }

import java.util.Scanner; 

public class Leap {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in); 
    System.out.print("Size: ");     
    int size = Integer.parseInt(in.nextLine()); 
    int count = 0; // INIT; 
    // while (COND) { 
    while (count < size) {
      System.out.print("* "); // BODY
      count = count + 1; // STEP 
    }    
  }



INIT
while (COND) {
  BODY
  STEP 
}

for ( INIT ; COND ; STEP ) {
  BODY


for (int i = 0; i != 10; i += 3)
  System.out.println(i); 

int i = 0; 
if (0 != 10) print 0;
i becomes 3
if (3 != 10) print 3;
i becomes 6
if (6 != 10) print 6;
i becomes 9 
print 9
i becomes 12
and there's no end in sight 

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


         ! (b && true) = ! b 



  b      ! (b && true)    f(b) = !b 
--------------------------------
 true      false          false  
 false     true           true 



Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> 2
2
> 2 / 3
0
> 2.0 / 3
0.6666666666666666
> 2 / 3 * 3
0
> 2 / (3 * 3)
0
> 2.0 / (3 * 3)
0.2222222222222222
> 2.0 / 3 * 3
2.0
> 5 / 9 * ( 66 - 32 )
0
> ( 66 - 32 ) * 5 / 9
18
> ( 66 - 32 ) * 5 / 9.0
18.88888888888889



Last time we asked the following questions: 

Assume 

  int i = 5; 

What gets printed by

  System.out.println( i++ + ++i ); 

There were 48 correct answers to this question, 
           54 incorrect answers to this question 
       and  1 student did not provide an answer. 

I also asked: 

  what is the value of i at the end? 

There were 31 correct answers here 
           27 incorrect answers and
           45 missing answers (so please be careful on an exam). 

Then we asked: what is the value of i after the following 

  i = i++ + i++; 

// again we were assuming i starts being 5 

There were 42 correct answers here 
           49 incorrect answers and
           11 students did not provide an answer. 

Finally we asked the value of this expression: 

  i++ < i

This is a boolean expression so there were only two possibilities. 

Here the value of i does not matter but we were still assuming i starts out as 5. 

So the results are that 23 answers were correct, 
                    and 80 were wrong
                    and  0 answers were missing. 

This last problem is the most interesting since it shows you are sure you're right, though you're wrong. 

So we need to discuss these as we start today in class. 

--

import java.util.Scanner; 

public class Leap {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in); 
    System.out.print("Size: ");     
    int size = Integer.parseInt(in.nextLine()); 
    for (int i = 0; i < size; i++) {
      for (int j = 0; j < size; j++) {
        if (i == j || j == 0 || j == size - 1) {
          System.out.print("* ");  
        } else {
          System.out.print("  ");  
        }
      }
      System.out.println(); 
    }
  }


Can you change this to print an A, an M, an E, a Q, a circle?