Today is Thursday, June 26. 2014. 

A type is a collection of values. 

Java types:

  (a) primitive   integers    int     1 + 2                          + - % / * < >= == != 
                              long
                              byte 
                              short

                  floating-point numbers     float      1.3f
                                             double     1.3

                  truth values    boolean = {true, false}                && (and) || (or) ! (not)

      TeachScheme!  

                  characters  char   'a'  dual nature of characters as that and numbers 

  (b) user-defined types (reference types)

I would like you to write a program that reads a String (word) from a user. 

Then I want the program to determine if the String is "bye" or not. 

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

  } 
}

Save, compile, run then develop further:

import java.util.Scanner; 

class LectureFour {
  public static void main(String[] args) {
    Scanner timmy = new Scanner(System.in);     
    System.out.print("Type: ");
    String line; 
    line = timmy.nextLine(); 
    System.out.println("You have typed: " + line); 
  } 
}

Then after some more discussion we have: 

import java.util.Scanner; 

class LectureFour {
  public static void main(String[] args) {
    Scanner timmy = new Scanner(System.in);     
    System.out.print("Type: ");
    String line; 
    line = timmy.nextLine(); 
    if ( line.equals( "bye" ) ) {
      System.out.println("Thanks, come again."); 
    } else {
      System.out.println("I don't understand " + line);      
    }
  } 
}

The else branch is optional so we cut it out:

import java.util.Scanner; 

class LectureFour {
  public static void main(String[] args) {
    Scanner timmy = new Scanner(System.in);     
    System.out.print("Type: ");
    String line; 
    line = timmy.nextLine(); 
    if ( line.equals( "bye" ) ) {
      System.out.println("Thanks, come again."); 
    } 
  } 
}


What if I replace if with while:

import java.util.Scanner; 

class LectureFour {
  public static void main(String[] args) {
    Scanner timmy = new Scanner(System.in);     
    System.out.print("Type: ");
    String line; 
    line = timmy.nextLine(); 
    while ( line.equals( "bye" ) ) {
      System.out.println("Thanks, come again."); 
    } 
  } 
}

In the end after much discussion we have:

import java.util.Scanner; 

class LectureFour {
  public static void main(String[] args) {
    Scanner timmy = new Scanner(System.in);     
    System.out.print("Type: ");
    String line; 
    line = timmy.nextLine(); 
    while ( ! line.equals( "bye" ) ) {
      // our assumption is the user types "bye" or a positive number 
      System.out.println( Math.sqrt( Double.parseDouble( line ) ) ); 
      System.out.print("Type: ");
      line = timmy.nextLine();
    } 
    System.out.println( "Thank you, come again." ); 
  } 
}

So now you can start Homework One, I think. 

We also wrote this:

import java.util.Scanner; 

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

    int size = 20; 
    
    int count = 0;
    while (count < size) {
      System.out.print("*");
      count = count + 1; 
    }
    System.out.println(); 
    
  } 
}

It prints a line of size stars. 

We wanted to print size of those so we wrote this:

import java.util.Scanner; 

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

    int size = 12; 

int lines = 0; // added     
while (lines < size) { // added 
  
    int count = 0;
    while (count < size) {
      System.out.print("* "); // notice the extra space 
      count = count + 1; 
    }
    System.out.println(); 
     
  lines = lines + 1; // added 
} // added 
    
  } 
}

This is what it looks now. 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run LectureFour
* * * * * * * * * * * * 
* * * * * * * * * * * * 
* * * * * * * * * * * * 
* * * * * * * * * * * * 
* * * * * * * * * * * * 
* * * * * * * * * * * * 
* * * * * * * * * * * * 
* * * * * * * * * * * * 
* * * * * * * * * * * * 
* * * * * * * * * * * * 
* * * * * * * * * * * * 
* * * * * * * * * * * * 


Yunsheng asked about this:

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

    {
      int count = 6;
      System.out.println( count ); 
    }
        
    {
      int count = 12;
      System.out.println( count ); 
    }
    
  } 
}

Try it. 

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

    // no count known
    { // anonymous block (of the kind in if, while...)
      int count = 6;
      // count known and 6 from here 
      System.out.println( count ); 
      // to here
    }
    // no count known         
    {
      int count = 12;
      // another count defined and 12 from here 
      System.out.println( count ); 
      // to here 
    }
    // no count known 
    
  } 
}