Computer Science 15-111 (Sections A & B), Spring 2007

Class Notes:  19-Jan-2007

 

Course Web Site, Syllabus, etc:   See http://www.kosbie.net/cmu/spring-07/15-111/.

 

Reading:

On Monday and Wednesday, we covered:

Chapter 1+2:  Introduction and Program Fundamentals (Number Rep’s)

Appendix A.1:  Integer Binary Representation

Appendix B.1:  Operator Precedence Table (portions thereof)

This lecture, we are covering:

            Chapter 3:  Statements and Control Flow

            Appendix B.2:  The Standard Java Math Functions

 

Quiz:

Our first quiz, covering Chapters 1-3 and Appendices A-B, is on Wednesday 24-Jan (note: we are moving quickly as this is mostly material you should have seen already). 

 

Topic Outline:

1.      From last lecture

a.      The importance of reading the book!

b.      Scanner example (Section 2.6)

                                                   i.      import, assert and AssertionException

                                                 ii.      Problems with sample code (comments, exceptions, UI)

c.      A little more on printf
     System.out.printf(“<%6.2f>”,13.579) 
ŕ  < 13.58>    Convenient!

d.      ASCII:  Just know:  ‘ ‘ (space,32), ‘0’ (48), ‘A’ (65), and ‘a’ (97)

e.      Casting:  Covered in Section 2.10.2

                                                   i.      int x = 3.9;  // not allowed!  Will not compile!

                                                 ii.      int x = (int) 3.9;  // ok!  We are casting the double to an int.

                                                iii.      double d = (int) 3.9; // ok!  Assignment (=) widens int back to a double!

f.        Increment / Decrement operators:  Covered thoroughly in Section 2.12.

g.      Base conversions

2.       Blocks, Nested Blocks, and Scope

{

     int x = 3;

     System.out.println(x);  // this is legal!

}

System.out.println(x); // but this is not!

3.      The Null Statement

4.      Relational and Equality Operators

a.      <,  <=,  ==,  >=,  >,  !=

// good, concise, clear!

boolean flag = (x != y);

// bad

boolean flag;
if (x != y)
     flag = true;
else
     flag = false;

 

5.      Logical Operators

a.      && (and), || (or), and ! (not)

b.      Short-circuit evaluation!

Very handy use:  if ((index < array.length) && (array[index] == foo)) …

c.      Precedence:

                                                   i.      (true || true && false || false)
Is this:   ((true || true) && (false || false)) == (true && false) == false
Or:  (true || (true && false) || false) == (true || false || false) == true ?

                                                 ii.      Best answer:  avoid this with parentheses!
(true || (true && false) || false)
(a || (b && c) || d)

 

6.      Conditional Statements

a.      if ( BooleanExpr ) Statement

                                                   i.      Single statement body:
  
if (x < 10) x += 3;  // this is usually ok style
Or:
  
if (x < 10)
        x += 3;  // this is ok, too


                                                 ii.      Block (multi-statement) body:

   // correct usage

   if (x < y) {

        // use braces!!!

        x += 3;

        y = y / 2;

   }

  // incorrect usage

  if (x < y)

      // no braces!!!

      x += 3;

      y = y / 2; // always runs!

 

b.      if ( BooleanExpr ) Statement else Statement

                                                   i.      Beware the Dangling Else!  Solution:  Braces!!!

 // correct usage

 // “else” binds to outer “if”

 if (x < y) {
    if (y < z)
       y++;
 }

 else
    x++;

  // incorrect usage

  // “else” binds to inner “if”

  if (x < y)
     if (y < z)
        y++;

  else  
     x++;

 

 

                                                 ii.      Avoid redundant logic, especially in final boolean expression:

 // correct usage

if (x < y)

   y++;

 else
   x++;

  // incorrect usage

  // redundant logic in “else-if”

  if (x < y)

     y++;
  else if (x >= y)
     x++;

 

 


 

7.      Looping Statements

a.      while ( BooleanExpr) Statement

b.      do Statement while ( BooleanExpr )

c.      for ( ForInit ; BooleanExpr ; UpdateExpr ) Statement
The same as:
ForInit;
while ( BooleanExpr ) {
   Statement
   UpdateExpr
}


8.   Adding the integers from 1 to 10:
   public static void main(String[] args) {

           int sum, counter;

          

           // for loop:

           sum = 0;

           for (counter = 1; counter <= 10; counter++)

                sum += counter;

           System.out.println(sum);

 

           // while loop:

           sum = 0;

           counter = 1;

           while (counter <= 10) {

                sum += counter;

                counter++;

           }

           System.out.println(sum);

 

           // do-while loop:

           sum = 0;

           counter = 1;

           do {

                sum += counter;

                counter++;

           } while (counter < 11);

           System.out.println(sum);

     }

9.      Break and Continue statements (see examples in Section 3.8)

10.  Switch statement (see examples in Section 3.9)

11.  DeMorgan’s Laws:  !(a || b) == (!a && !b),    !(a && b && c) == (!a || !b || !c)

12.  Math functions:
abs, min, max, exp, log, sqrt, pow, ceil, floor, rint, round, round, random,
sin, cos, tan, asin, acos, atan, atan2