Lab 01 is probably due. 

Lab 02 and Homework 01 have been posted. 

There is a textbook please read chapters 1, 2, 3. 

Java programs are made out of classes. 

Let's agree to make all classes public and put one class per file. 

Names classes with nouns. Inside classes we have variables and procedures. 

We call these procedures: methods. They are verbs because that's where things happen. 

Remember the design recipe from C211:

  1. choose data representation 
  2. give some examples 
  3. name your function
  4. write the contract, purpose statement 
  5. give me some examples of I/O 
  6. choose template (we're in the class of intellectually manageable programs)
  7. write code
  8. test, debug, fix 

Now let's write some code: 

public class Toyota {
   public int kevin;
   public static int jerry; 
   public int brandon(int a, int b) {
     return a + b;  
   }
   public static void keqin() {
     System.out.println("Many people are afraid of heights, not me, I'm afraid of widths."); 
   }
}

I can't run this but I can play in DrJava: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> 3
3
> 2 + 4 * 5
22
> 12 / 7
1
> 12 % 7
5
> 4 / 7
0
> 4 % 7
4


Now let's see if I can use the variables and procedures that I defined. 

I remind you that I have two variables (kevin, jerry) and two functions (brandon, keqin). 

We call procedures and variables defined inside a class (static or not): members. 

Access static members through the class. There's only one of each per class. 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Toyota.jerry
0
> Toyota.jerry = 6;
> Toyota.jerry
6
> Toyota.jerry + 3
9
> Toyota.keqin()
Many people are afraid of heights, not me, I'm afraid of widths.


Static members are one per class, located in the class. 

Non-static members (also called instance members) are one per instance. 

So to have and use them you need to instantiate the class. 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Toyota a = new Toyota(); 
> a
Toyota@5bac72f0
> a.kevin
0
> a.kevin = 3
3
> Toyota b = new Toyota();
> b.kevin
0
> b.kevin = -2
-2
> a.kevin
3
> b.kevin
-2
> a.brandon(4, 5)
9
> b.brandon(-1, -2)
-3


So this is the big difference between static and non-static members. 

Now a problem: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> int red;
> red = 7;
> red
7
> red - 3
4
> red = 4
4
> red
4
> int blue = 9;
> blue + red
13
> blue
9
> red
4


Now please find a way to swap the numbers between variables red and blue. 

> [...]
> red = 9
9
> blue = 4
4
> red
9
> blue
4


That's good, now can you devise another way, that does not depend on the actual number. 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> int red = 5
> int blue = 13

> [...]
>
> int green = blue;
> blue = red; 
> red = green; 
> red
13
> blue
5


Good, if you have extra variables. What if you don't? 

Please devise a way to swap red and blue (two intger variables) without using a third variable. 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> int red = 7;
> int blue = -2; 
> int green = blue; 
> green 
-2
> blue
-2
> green = blue + 19; 
> green
17
> green = green + 1
18
> green == green + 1
false
> 2 == 1 + 1
true
> 2 != 3
true


Now we have an idea from Mr. Brandon: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> int red = 1
> int blue = 2
> red = blue + 0
2


Aargh. 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> int red = -9
> int blue = 4
> red = red + blue
-5
> blue = red - blue
-9
> red = red - blue
4
> red
4
> blue
-9


This by way of Zach and Andrew. 

We need a proof. 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> 1
1
> 1 + 2
3
> 1 > 2
false


Booleans. 

Booleans are a type. Integers are a type. 

A type is a set of values with associated legal operations. 

The set of integers is huge, the set of booleans is small. 

Operations on booleans: not, and, or.

 a    !a
----------
true  false
false true 

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

a     b      a || b
-----------------
true  true   true
true  false  true 
false true   true 
false false  false 

Nowhere did we say anything about if statements. 

In Lab 01 this is how you get the right answers:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> boolean b = false;
> int x = 0;
> b && x == 0
false


How do you simplify b == true ?

   
   b       b == true      false       b
-----------------------------------------------
  true     true           false      true 
  false    false          false      false 


If x is an integer x + 0 is x

Likewise x * 1 is x and x * 0 is 0 and so on. 

How do we simplify this:

if (n == 0) { 
  b = true; 
} else { 
  b = false; 
}

Jared says this is equivalent to     

  b = (n == 0);

That sounds right, is it legal in Java? 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> int n = 6
> boolean b
> b = (n == 0); 
> b
false
> n = 0;
> b = (n == 0);
> b
true