Tue Sep  4 09:22:04 EDT 2012

Expressions

1

1 + 2

1 < 2 relational operator "less than" the outcome is "true"

Types

Type is: a classification of data 

         a set of values 

Examples of types in Java:

  String

  Integer 
 
  int, long, byte, short

  double, float 

  boolean = {true, false} 

  char 

  Double

  Boolean 

The type of this expression: 4 < 2 is a boolean. 

It follows that once we have this type we can define 
variables that could hold such values. 

public class One { // save as One.java
  public static void main(String[] args) {
    boolean a, b; 
    a = 1 < 2; 
    b = 3 > 5; 
    // how do I combine a and b 
    // if they were of type int I could use +, -, *, /, % 
    int d = ...; // read from the keyboard
    boolean c = (d % 19 == 0) ; // whether d is a multiple of 19 or not 
    // so the question is: what operators are related to boolean
    
  }
}


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

Boolean operators: && (AND), || (OR), ! (NOT).

 a     b     a && b     a || b     ! a
------------------------------------------
true  true    true       true      false
true  false   false      true 
false true    false      true      true 
false false   false      false 


Simplify these expressions: 

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

This demonstrates that the expression a == true is equivalent to a

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

By the same reasoning we demonstrated that a == false is the same as ! a

   a && true       a 

   a || true       true

   a && false      false 

   a || false      a

   a && ! a        false 

   a || ! a        true 

   ! a && ! b      ! ( a || b)     De Morgan's law

   ! a || ! b      ! ( a && b)     De Morgan's law

When you write a boolean expression the operators act
as follows 

  !    acts like unary minus in numerical expressions 
  ||             + 
  &&             *

If you don't have any parens you can transfer the order
of evaluation of operations from numbers. 

Decisions use boolean expressions. 

A boolean expression is a test. 

  int n, m; 
  n = ...;
  m = ...;
  if (n < m) { // test is the one that determines our decision 
    System.out.println( "max(" + n + ", " + m + ") = m); 
  } else {
    System.out.println( "max(" + n + ", " + m + ") = n); 
  }


An if statement has the following syntax:

  if (...) {
    if (...) {
      ... // both conditions are true  
    } else {
      ... // first true and the second one false
    }
  } else {
    ... // first condition is false 
  }

10:22 Let's work 

import java.util.Scanner;

public class LectureFive {
  public static void main(String[] args) {
    System.out.print("Year: ");
    Scanner a = new Scanner(System.in);
    int year = a.nextInt();
    if (year < 1582) {
      if (year % 4 == 0) {
        System.out.println("Leap year.");
      } else {
        System.out.println("Not a leap year.");
      }
    } else {
      if (year % 4 == 0) {
        if (year % 100 == 0) {
          if (year % 400 == 0) {
            System.out.println("Leap year.");
          } else {
            System.out.println("Not a leap year.");
          }
        } else {
          System.out.println("Leap year.");
        }
      } else {
        System.out.println("Not a leap year.");
      }
    }
  }
}