Exam on Wednesday 35 questions from the 469 that you're working on right now. 

300 questions in Homework Two  Friday 

169 in Lab Three               Monday 

Late: no penalty

Late != Procrastinating 

Try to do Homework Two and Lab Three by Wedensday because of the exam.

Makeups allowed, some limitations. 

Literals, operators, expressions, values, types, variables, assignment 
statements. DrJava: Interactions Panel allows you to practice with them.

Programs in Java: made out of classes. 

Classes: containers of static members. Member: variable, procedure (method). 

Math.PI is an example of a static final (constant) variable. 

Math.sqr(...) is an example of a static method from the class java.lang.Math 

Classes are also used to model things: 

Other models: Point, Line, Triangle, Circle like Student. 

Homework Three will be simple modeling

Lab Four is grading your exam 

Typical Homework Three:

  design Circle 
  a Circle is a Point (center) with a radius
  every Circle reports their area

  every Circle can tell if it overlaps another Circle or not

Exercises for this week

Chapters 1, 2, 3
Chapter 4

Chapter 5, 6

1 + 2

1 < 2

if ( ... ) {
  // then branch 
  // execute block if condition is true 
} else {
  // else block
  // do this if condition is false 
}

What kinf of statements can you write inside those blocks? 

Leap year: 

if ( ... ) { // before 1582 
  if ( ... ) { // multiple of 4?
    // leap year 
  } else {
    // not a leap year 
  } 
} else { // after 1582 
  if ( ... ) { // multiple of 4
    if ( ... ) { // multiple of 100 
      if ( ... ) { // multiple of 400 
        // leap year
      } else {
        // not leap year 
      } 
    } else { // not multiple of 100
      // leap year 
    } 
  } else { // not multiple of 4
    // not a leap year 
  } 
}

Nested ifs are fine. 

Use curly braces religiously. 

There are shortcuts: don't take them. 

else if 

elsif 

Premature optimization is the root of all evil. 

Types in Java: 

  primitive types

    int       long short byte 
    double    float 
    char 
    boolean

  non-primitive types: reference types, user-defined types, classes 

Variables in Java:

  local 
  parameter 
  instance 
  static 

A class contains a blueprint: all the instance members. 

Using new you can instantiate the blueprint. 

For each class there's exactly one static member per class.

A class could have three static variables and five static methods. 

None of these eight static members appear more than once. 

From each class we can create 0, 1, >1 objects (instances). 

In class Math there is one method sqrt and that's it. 

In class Student there is a blueprint that contains talk().

There's an instance of talk() in each Student object we create. 

// Point.java 
public class Point {
  static Point origin = new Point(0, 0); 
  int x, y; // instance variables
  Point(int x, int y) {
    this.x = x;
    this.y = y; 
  } // constructor
  public double distanceTo(Point other) { // instance method
    int dx = this.x - other.x, 
        dy = this.y - other.y; 
    return Math.sqrt( dx * dx + dy * dy ); 
  }
  public static void main(String[] args) {
    Point a = new Point(3, 0);
    Point b = new Point(0, 4); 
    System.out.println( a.distanceTo(b) ); 
    Point c = new Point(1, 1); 
    System.out.println( c.distanceTo(Point.origin) );    
  }
}

The else branch is optional: 

if ( ... ) {



Replace the if keyword with while: 

while ( ... ) {

}

Example: 

int i = 0; 
while (i < 10) {
  System.out.println( i );
  i++; 
}

Exact same thing could be implemented like this: 

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

Usually while is used when you know when to stop. 

Usually for is used when you know how many times you need to keep going. 

The for and while loops are completely equivalent. 

[s1]
for ([i1] ; [c1] ; [i2]) {
  [b1]
}
[s2]

The structure above is equivalent to: 

[s1]
[i1]
while ([c1]) {
  [b1]
  [i2]
}
[s2]

I'd like to design an example: print an uppercase scalable M.

import java.util.Scanner; 

public class M {
  public static void main(String[] args) {
    Scanner in;
    in = new Scanner(System.in); 
    System.out.print("Size: "); 
    int size = 2 * in.nextInt() + 1; 
    for (int line = 0; line < size; line++) {
      // System.out.println( "****************************************" );  
      for (int column = 0; column < size; column++) {
        if (column == 0 || // west coast 
            column == size-1 || // east coast 
            line == column && line < size/2 || // seattle to st louis
            line + column == size-1 && line <= size/2
           ) { 
          System.out.print("* ");  
        } else {
          System.out.print("  ");  
        }
      } // end of line
      System.out.println(); 
    }
    // System.out.println("You have typed: " + size); 
    in.close(); 
  }
}

Here's how it works:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run M
Size:  [DrJava Input Box]
*                   * 
* *               * * 
*   *           *   * 
*     *       *     * 
*       *   *       * 
*         *         * 
*                   * 
*                   * 
*                   * 
*                   * 
*                   * 
> run M
Size:  [DrJava Input Box]
*                       * 
* *                   * * 
*   *               *   * 
*     *           *     * 
*       *       *       * 
*         *   *         * 
*           *           * 
*                       * 
*                       * 
*                       * 
*                       * 
*                       * 
*                       * 
> run M
Size:  [DrJava Input Box]
*                           * 
* *                       * * 
*   *                   *   * 
*     *               *     * 
*       *           *       * 
*         *       *         * 
*           *   *           * 
*             *             * 
*                           * 
*                           * 
*                           * 
*                           * 
*                           * 
*                           * 
*                           * 
> run M
Size:  [DrJava Input Box]
*                               * 
* *                           * * 
*   *                       *   * 
*     *                   *     * 
*       *               *       * 
*         *           *         * 
*           *       *           * 
*             *   *             * 
*               *               * 
*                               * 
*                               * 
*                               * 
*                               * 
*                               * 
*                               * 
*                               * 
*                               * 
>

Now exercise:

import java.util.Scanner; 

public class Four {
  public static void main(String[] args) {
    Scanner in;
    in = new Scanner(System.in); 
    System.out.print("Size: "); 
    int size = 2 * in.nextInt() + 1; 
    for (int line = 0; line < size; line++) {
      // System.out.println( "****************************************" );  
      for (int column = 0; column < size; column++) {
        if ( line + column == size/2 ||
             line == size/2 && column < 3 * size / 4 ||
             column == size/2 && line > size / 4
           ) { 
          System.out.print("* ");  
        } else {
          System.out.print("  ");  
        }
      } // end of line
      System.out.println(); 
    }
    // System.out.println("You have typed: " + size); 
    in.close(); 
  }
}

Here's how it works:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Four
Size:  [DrJava Input Box]
              *               
            *                 
          *                   
        *                     
      *       *               
    *         *               
  *           *               
* * * * * * * * * * *         
              *               
              *               
              *               
              *               
              *               
              *               
              *               
> run Four
Size:  [DrJava Input Box]
          *           
        *             
      *               
    *     *           
  *       *           
* * * * * * * *       
          *           
          *           
          *           
          *           
          *           
> run Four
Size:  [DrJava Input Box]
                  *                   
                *                     
              *                       
            *                         
          *                           
        *         *                   
      *           *                   
    *             *                   
  *               *                   
* * * * * * * * * * * * * *           
                  *                   
                  *                   
                  *                   
                  *                   
                  *                   
                  *                   
                  *                   
                  *                   
                  *