This is Jan 14, 9:28am, in GY126. How have you been? 

Java programs are made out of classes. 

Classes contain members. Members can be static or non-static. 

By members we mean: variables and procedures (methods). 

In Java there are four kinds of variables:

  (a) local variables
  (b) parameters
  (c) static variables 
  (d) non-static variables (instance variables) 

One static member is truly special: the main method. 

Here's the simplest program we can write: 

public class Hello { // put this in Hello.java 
  public static void main(String[] args) {
    System.out.println("Hi there."); 
  } 
}

You compile with: javac Hello.java

You run it with: java Hello

On silo as well as Macs you're set from the beginning. 

On Windows you have two options:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\dgerman>dir
 Volume in drive C has no label.
 Volume Serial Number is BA71-C62D

 Directory of C:\Users\dgerman

01/14/2013  09:28 AM    <DIR>          .
01/14/2013  09:28 AM    <DIR>          ..
01/08/2013  10:18 PM    <DIR>          .android
11/01/2012  09:50 PM    <DIR>          .datastudio
01/14/2013  09:28 AM    <DIR>          Contacts
01/14/2013  09:28 AM    <DIR>          Desktop
01/14/2013  09:28 AM    <DIR>          Documents
01/14/2013  09:28 AM    <DIR>          Downloads
01/14/2013  09:28 AM    <DIR>          Favorites
01/14/2013  09:28 AM    <DIR>          Links
01/14/2013  09:28 AM    <DIR>          Music
01/14/2013  09:28 AM    <DIR>          Pictures
01/14/2013  09:28 AM    <DIR>          Saved Games
01/14/2013  09:28 AM    <DIR>          Searches
01/14/2013  09:28 AM    <DIR>          Videos
08/14/2012  06:13 PM    <DIR>          workspace
               0 File(s)              0 bytes
              16 Dir(s)  125,759,827,968 bytes free

C:\Users\dgerman>cd Desktop

C:\Users\dgerman\Desktop>dir
 Volume in drive C has no label.
 Volume Serial Number is BA71-C62D

 Directory of C:\Users\dgerman\Desktop

01/14/2013  09:28 AM    <DIR>          .
01/14/2013  09:28 AM    <DIR>          ..
               0 File(s)              0 bytes
               2 Dir(s)  125,759,827,968 bytes free

C:\Users\dgerman\Desktop>dir
 Volume in drive C has no label.
 Volume Serial Number is BA71-C62D

 Directory of C:\Users\dgerman\Desktop

01/14/2013  09:50 AM    <DIR>          .
01/14/2013  09:50 AM    <DIR>          ..
01/14/2013  09:50 AM               139 Hello.java
               1 File(s)            139 bytes
               2 Dir(s)  125,759,811,584 bytes free

C:\Users\dgerman\Desktop>type  Hello.java
public class Hello { // put this in Hello.java
  public static void main(String[] args) {
    System.out.println("Hi there.");
  }
}
C:\Users\dgerman\Desktop>javac Hello.java
'javac' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\dgerman\Desktop>PATH=%PATH%;"C:\Program Files\Java\jdk1.6.0_33\bin"

C:\Users\dgerman\Desktop>javac Hello.java

C:\Users\dgerman\Desktop>java Hello
Hi there.

C:\Users\dgerman\Desktop>

To make this setting available in other windows go to Control Panel. 

Search for environment variables and then edit the ones for your account. 

Any window opened knows about the environment at the time it was opened.

So windows opened after you set the PATH will all know where javac is. 

Close the other older windows. 

Now about the type of programs we can write: 

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

    int a = 1 + 2; // a is a local variable (to main)

    System.out.println( "1 + 2 evaluates to " + a ); 

    System.out.println( "14 / " + a + " evaluates to: " + ( 14 / a )); // prints 4, an integer

    System.out.println( 14.0 / a ); // prints 4.66 a floating-point number 

  } 
}

Let's think about the type of these expressions:

  "what?!" 

  1

  1 + 2

  "who" + "ever" 

  1 < 2 

Relational operators in Java: <, <=, >, >=, !=, ==

Relational operators yield boolean values. 

Boolean operators in Java: &&, ||, !.

Boolean values are: true and false. 

Assume p and q are boolean variables. 

Then the definition of &&, || and ! is as follows: 

    p         q           p && q        p || q        !p 
----------------------------------------------------------------
  true       true         true          true          false 
  true       false        false         true 
  false      true         false         true          true 
  false      false        false         false 

Now let's work out the first set of exercises on your papers. 

Assuming a is a boolean variable can you simplify:

   a           a == false            !a 
  true         false                 false 
  false        true                  true 

Read about De Morgan's laws in the book (the last two expressions). 

Now about reading user input we know this:

import java.util.*;

public class Example { 
  public static void main(String[] args) {
    boolean p; 
    p = 1 < 2 && 2 > 3; 
    System.out.println ( p ); 
    Scanner dottie; 
    dottie = new Scanner(System.in); 
    // System.out.println( dottie ); 
    String line;
    System.out.print("Type something: "); 
    line = dottie.nextLine();
    System.out.println( "You typed: " + line ); 
  } 
}

Is there any other way to get user input? 
    
Yes. You can get input from the command line. 

We went and wrote this program:

public class Example { 
  public static void main(String[] args) {
    String one = args[0]; 
    String two = args[1]; 
    System.out.println( one.equals(two) ); 
  } 
}

With this you can start working on the three exercises. 

We will post the paper with exercises. 

Read chapter 3 and 4 for Wednesday. 

--