There is an exam on Wednesday.

Questions are from the My Programming Lab sets for this week.

Homework Two,       Lab Three

Chapter 1, 2, 3, 4  Chapters 5, 6

300                 169

Friday              Monday

Procrastination and being late: different things. 

No penalty for being late. 

We can only accept on late assignment per day.

The exam is made out of exercises on Homework Two and Lab Three. 

1

'a'

true 

Literals, operators, expression, value, type, variable, 
assignment statement. Syntax. Java programs are made out
of classes. Classes: containers of static members, they
also contain blueprints. 

Member: variable, procedure (method). 

Members in a class: static or non-static (instance). 

A blueprint is just a vision. It is comprised of all the
instance members in the class. Using the new operator you
instantiate the blueprint and you can create 0, 1, >1 objects.

Modeling with classes: instance variables, constructors and instance methods. 

Instance variables and instance methods are instance members.

Constructors are initialization procedure (convenience). 

The inside of methods might use:

  (a) decisions (if statements)

  (b) iterations (loops: while, for)

1 + 2

1 < 2

if ( ... ) {
  // then branch
  // things to do when condition is true 
} else {
  // else branch 
  // mutually exclusive with the then branch
  // statements to execute when condition is false 
}

leap year: 

if ( ... ) {
  if ( ... ) {

  } else {

  }
} else {
  if ( ... ) {
    if ( ... ) {
      if ( ... ) {

      } else {

      } 
    } else {
      
    }
  } else {

  }
}

Don't use shortcuts unless completely sure. 

else if 

elsif 

Premature optimization is the root of all evil. 

... [1] ...  

if ( ... [2] ... ) {
  ... [3] ... 


... [4] ... 

So [3] happens if [2] is true (only). 

Back to basic syntax: 

while ( ... ) {
  // body
}

The body is executed until the condition becomes false. 

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

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

while loops and for loops are completely equivalent. 

[s1]
for ([init] ; [cond] ; [step]) {
  [body]
}
[s2]

Here's the code above as a while loop:

[s1]
[init]
while ([cond]) {
  [body]
  [step] 
}
[s2]


Kinds of exercises:

model a Circle 
a Circle is a Point with a radius 
a Circle should be able to report their area
a Circle should be able to determine if it overlaps or not another Circle

Other things you could model:

  Point
  Line
  Triangle 
  Rectangle (overlap!)

These are all modeling exercises, they could be part of Homework Three.

Lab Four is going to be related to your Early Eval Exam. 

Here is an exercise that demonstrates for loops:

I want to write a program that produces a scalable uppercase E.

import java.util.*; 

class Seth {
  public static void main(String[] args) {
    Scanner in; 
    in = new Scanner( System.in );
    System.out.print("Size: "); 
    int size = in.nextInt(); 
    System.out.println( size ); 
    for (int line = 0; line < size; line++) {
      for (int column = 0; column < size; column++) {
        if (line == 0 || 
            column == 0 || 
            line == size-1 || 
            line==size/2 && column < size/2) {
          System.out.print("* ");  
        } else {
          System.out.print( "  " );  
        }
      } // line ends here
      System.out.println(); 
    }
  }
}

Here's how it works:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Seth
Size:  [DrJava Input Box]
13
* * * * * * * * * * * * * 
*                         
*                         
*                         
*                         
*                         
* * * * * *               
*                         
*                         
*                         
*                         
*                         
* * * * * * * * * * * * * 
> run Seth
Size:  [DrJava Input Box]
9
* * * * * * * * * 
*                 
*                 
*                 
* * * *           
*                 
*                 
*                 
* * * * * * * * * 
> run Seth
Size:  [DrJava Input Box]
5
* * * * * 
*         
* *       
*         
* * * * * 
> run Seth
Size:  [DrJava Input Box]
7
* * * * * * * 
*             
*             
* * *         
*             
*             
* * * * * * * 
> run Seth
Size:  [DrJava Input Box]
21
* * * * * * * * * * * * * * * * * * * * * 
*                                         
*                                         
*                                         
*                                         
*                                         
*                                         
*                                         
*                                         
*                                         
* * * * * * * * * *                       
*                                         
*                                         
*                                         
*                                         
*                                         
*                                         
*                                         
*                                         
*                                         
* * * * * * * * * * * * * * * * * * * * * 
>

Primitive types in Java: 
  int        long  byte  short 
  double     float 
  char
  booleans

Everything else: reference type, user-defined type, classes.

Kinds of variables in Java:
  local 
  parameters
  instance
  class (static) 

Minute paper exercise:

import java.util.*; 

class Seth {
  public static void main(String[] args) {
    Scanner in; 
    in = new Scanner( System.in );
    System.out.print("Size: "); 
    int size = in.nextInt(); 
    System.out.println( size ); 
    for (int line = 0; line < size; line++) {
      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( "  " );  
        }
      } // line ends here
      System.out.println(); 
    }
  }
}

Here's how this program works:

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