public class One {
  public static void main(String[] args) {
    String a; // local variable 
    boolean b; // local variable 
    System.out.println( a ); 
  }
}

--

1 error found:
File: C:\Users\dgerman\Desktop\One.java  [line: 5]
Error: variable a might not have been initialized

Is this confusing? 

--

In Java we have four kinds of variables: 

  (a) local variables 
  (b) formal parameters
  (c) instance variables 
  (d) static (class) variables 

Please be aware that the Interactions panel is an abstraction of its own:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> String a
> a
null


Now I write this program:

public class One {
  public static void main(String[] args) {

    System.out.println( 'ab' ); 
    
  }
}

What does it do when I try to compile: 

4 errors found:
File: C:\Users\dgerman\Desktop\One.java  [line: 4]
Error: unclosed character literal
File: C:\Users\dgerman\Desktop\One.java  [line: 4]
Error: ';' expected
File: C:\Users\dgerman\Desktop\One.java  [line: 4]
Error: unclosed character literal
File: C:\Users\dgerman\Desktop\One.java  [line: 4]
Error: ';' expected

So this seems like the following is happening: 

public class One {
  public static void main(String[] args) {

    System.out.println( 'ab' ); 
    //                    ^ unclosed character literal 
  }
}

Now let's go into the Interactions panel: 

  TIME FLIES I CANT THEYRE TOO FAST

Here's what was meant (did you anticipate that correctly?)

  TIME FLIES!

  I CAN'T, THEY'RE TOO FAST...

When there's ambiguity of purpose error reporting is not 100% accurate. 

Fix it just knowing that there is a mistake. 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> 'ab'
Lexical error at line 1, column 3.  Encountered: "b" (98), after : "\'a"


Now let's look at this:

public class One {
  public static void main(String[] args) {

    int i = 23; 
    System.out.println( i ); 
    
    int i = -7; 
    System.out.println( i ); 
    
 
  }
}

The compilation error is: 

1 error found:
File: C:\Users\dgerman\Desktop\One.java  [line: 7]
Error: variable i is already defined in method main(java.lang.String[])

A local variable can only be defined once per enclosing block (not method): 

public class One {
  public static void main(String[] args) {

    { int i = 23; 
      System.out.println( i ); 
    } 
      
    { int i = -7; 
      System.out.println( i ); 
    }   
 
  }
}

Every local variable should be defined once per block. 

public class One {
  public static void main(String[] args) {

    { int i = 23; 
      System.out.println( i ); 
    } 
      
    System.out.println( i ); // there's no i here 
    
    { int i = -7; 
      System.out.println( i ); 
    }   
 
  }
}

If I try to define an i in the enclosing block I produce clashes: 

public class One {
  public static void main(String[] args) {
    int i = 31; 
    { int i = 23; 
      System.out.println( i ); 
    } 
    System.out.println( i ); 
    { int i = -7; 
      System.out.println( i ); 
    }   
  }
}

Here's what was intended originally (maybe): 

public class One {
  public static void main(String[] args) {
    int i = 31; 
    { int j = 23; 
      System.out.println( j ); 
      System.out.println( i ); // is is visible here that's why I could not redefined it
    } 
    System.out.println( i ); // 31 as expected 
    { int j = -7; 
      System.out.println( j ); 
      System.out.println( i ); // still 31
    }   
  }
}

Params don't need to be initialized because when the method is called values 
are being passed to them. Otherwise params are just like local variables. 

Homework 04 and Lab 05 are now up. 

Homework 05 and Lab 06 will be posted tonight. 

They will be due Tue night, grace period Wed night. 

There will be no other assignments this week. 

Lab on Thu (Lab 07) will be graded (your self-assessment). 

We looked at your Homework 04 and Lab 05 work on it tonight.

Finish it or come tomorrow ready to ask questions. 

public class One {
  public static void main(String[] args) {
    double gpa;
    gpa = Double.parseDouble(args[0]); 
    
    if (gpa < 2) {
      if (gpa < 1.5) {
        System.out.println("You are failing."); 
      } else {
        System.out.println("You are on probabtion."); 
      }
    } else {
      // System.out.println("No worries."); 
    }
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> java One 2.3
> java One 1.99
You are on probabtion.
> java One 1.51
You are on probabtion.
> java One 1.50
You are on probabtion.
> java One 1.499999
You are failing.


Here's the answer from the sample exam: 

36. Here's how I code correctly in my obsessive-compulsive way (all curly 
braces, all branches): 

    if (gpa >= 1.5) {
      if (gpa < 2) {
        System.out.println("Probation."); 
      } else { 

      } 
    } else { 
      System.out.println("Failing."); 
    } 

The rules are: else is optional and blocks of one statement don't need curly braces. 

If I apply this blindly I get: 

    if (gpa >= 1.5) 
      if (gpa < 2) 
        System.out.println("Probation."); 
    else  
      System.out.println("Failing."); 
     
But this is wrong, because it runs like this: 

    if (gpa >= 1.5) 
      if (gpa < 2) 
        System.out.println("Probation."); 
      else  
        System.out.println("Failing."); 

We discussed this in class and notes. So the "moving" else they say "dangles" 
without the curly braces around the inner if. 

A few other questions at the end of the lab: 

public class One {
  public static void main(String[] args) {

    String a = "Tom", b = "tomato"; 
    
    System.out.println( a.compareTo(b) ); // negative so a < b
    
  }
}

An unrelated question about instance vs static (class) variables. 

public class One {
  
  boolean b;
  static boolean c; 
  
  public static void main(String[] args) {
    System.out.println( c ); // or even better: One.c
    One a = new One();
    System.out.println( a.b ); 
        
  }
}

--

See you tomorrow!