Early Evaluation Exam next week on Wednesday in class. 

Study guides will be posted this week. 

Exam is over the first six chapters. 

Monday office hours (both in Lindley 125)

  Griffin Tennent    Mon and Wed 1-2pm in LH125 
  Xinran Dai         Mon 4-5pm in LH125

Let's discuss if statements and while and for loops. 

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

See my flowchart it is clear y is 2 at the end. 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> int x = 18, y = 10; if (x < 10) { if (x > 5) y = 1; } else y = 2;
> y
2


Next we remove the curly braces 

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

Watch the flowchart change. Now y is 10. 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> int x = 18, y = 10; if (x < 10) if (x > 5) y = 1; else y = 2;
> y
10


Picture of dangling else and explanation. 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> if (2 < 1) { 
    System.out.println("Oops."); 
  }
> if (2 < 1) ; { 
    System.out.println("Oops."); 
  }
Oops.


int y = 6; y = --y - y--; 

Here's an experiment:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> int y = 6;
> y
6
> y--
6
> y
5
> --y
4
> y
4


So one more time from the beginning:

> int y = 6;
> y
6
> y--
6
> y
5
> --y
4
> y
4
> --y + y-- // 3 (and y is 3) + 3 (and y becomes 2)
6
> y
2


Let's calculate 1 + 2 + ... + (n-1) + n


    1    2    3     ....   (n-2)  (n-1)  n 

    n  (n-1) (n-2)  ....     3      2    1   

----------------------------------------------
  (n+1)(n+1) (n+1)  ....   (n+1)  (n+1) (n+1)


So we have n * (n+1) to get twice the answer. 

So I just proved the formula and that's it. 

Maybe the second one is n * (n + 1) * (2 * n + 1) / 6

http://www.trans4mind.com/personal_development/mathematics/series/sumNaturalSquares.htm

In these two problems as well the last one we should use a loop.

Two loops under consideration: while and for. 

if (i < 10) {
  i = i + 1; 
}

If we make a change of keyword

while (i < 10) {
  i = i + 1; 
}

This results in repeatedly incrementing i until the condition is false. 

int i = 1; // current number to add to sum 

int sum = 0; // the sum as we work on it, initialized

while (i <= n) { // condition for eligible numbers 
  sum = sum + i * i * i; // add the current number to sum 
  i = i + 1; // prepare new number
}

System.out.println( sum ); 

How does this look if we use a for loop instead? 

General form for a while loop: 

 
  INIT; // int i = 1, sum = 0; 

  while ( COND ) { // i <= n 
    
    BODY; // sum = sum + i * i * i; 

    STEP; // i = i + 1

  }


This is equivalent to: 


  for ( INIT ; COND ; STEP ) {

    BODY; 

  }

In our case: 

  int sum = 0; 

  for ( int i = 1; i <= n ; i = i + 1) {
    sum = sum + i * i * i;
  }


Here's the proof this works:

public class One {
  public static void main(String[] args) {
    int n = 10; 
    System.out.println( n * (n + 1) / 2 );     
    int sum = 0;
    for ( int i = 1; i <= n ; i = i + 1) {
      sum = sum + i;
    }
    System.out.println( sum ); 
  }
}

Question: why is sum defined before the for? 

Next: can we write a program to produce a scalable pattern? 

-bash-3.2$ java Four 16
                *
              *
            *
          *
        *
      *         *
    *           *
  *             *
* * * * * * * * * * *
                *
                *
                *
                *
                *
                *
                *
-bash-3.2$ java Four 21
                    *
                  *
                *
              *
            *
          *
        *
      *             *
    *               *
  *                 *
* * * * * * * * * * * * * * *
                    *
                    *
                    *
                    *
                    *
                    *
                    *
                    *
                    *
                    *
-bash-3.2$ java Four 9
        *
      *
    *
  *     *
* * * * * * *
        *
        *
        *
        *
-bash-3.2$

How do we accomplish this? 

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

Let's run it:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Two
* * * * * * * * * * 
*                   
*                   
*                   
*                   
* * * * *           
*                   
*                   
*                   
* * * * * * * * * * 


That looks like a good start to me!

--