Computer Science 15-100 (Sections T & U), Fall 2007
Class Notes, Day 3:   Tue 4-Sep-2007


Logistics

  1. Schedule
  2. Reading

Topic Outline:

  1. Some topics from last lecture
    1. Moore's Law
      Q:  According to Moore's Law, about what would the clockspeed be of a typical $500 computer in 20 years?
       
    2. Logic Gates and Digital Circuits
      http://math.hws.edu/TMCM/java/xLogicCircuits/index.html
      A XOR B:

       
    3. "Hello World" on a Mac command line
       
    4. Number Systems
      1. What about signed numbers (that is, negative numbers)?
        1. Use the leftmost bit as a sign bit  (0 = non-negative,  1 = negative)
        2. Store number in two's complement  (store number x as  "max - x")
          Where "max" here is 2(k-1), if we are using k bits.
          So, for 4-bits, "max" is 23 = 8.
          1. So:   in 8-bit-two's-complement:
            -3 =  (sign-bit-of-1)(23 - 3 = 5)
               =         1       (4 + 1)
               =         1101
          2. So:  1101 is both -3 in signed 4-bit binary AND it is 13 in unsigned 4-bit binary!!!
          3. So:  we simply must know if the number is signed or unsigned!!!
             
      2. A shortcut to two's complement:
        1. Negate a number by flipping the bits and adding one.
        2. So:  To find -3, start with +3 = 0011.
          Flip the bits to get 1100
          And add one to get 1101.
           
  2. Data and Expressions
    1. Teasing apart HelloWorld.java
      public class HelloWorld {
            public static void main(String[] args) {
                System.out.println("Hello World!");
            }
      }

       
    2. print vs println
      class MyCode {
        public static void main(String[] args) {
          System.out.print("This is");
          System.out.print("a test");
          System.out.println("that will run, albeit");
          System.out.println();
          System.out.println("with some problems...");
        }
      }
    3. String concatenation
      class MyCode {
        public static void main(String[] args) {
          System.out.println("a" + "b");
          System.out.println("a" +  1 );
          System.out.println("a" +  1 + "2");
          System.out.println("a" +  1 +  2 );
          System.out.println( 1  +  2 + "a");
        }
      }
    4. Printing numbers (not Strings)
      class MyCode {
        public static void main(String[] args) {
          System.out.println(1);
          System.out.println(1 + 2);
        }
      }
    5. Escape Sequences (or, how do we print a double-quote?)
      class MyCode {
        public static void main(String[] args) {
          System.out.println("This is a double-quote: \"");
        }
      }
    6. More Escape Sequences (tabs, newlines, and backslashes)
      class MyCode {
        public static void main(String[] args) {
          System.out.println("These items are tab-delimited, 3-per-line");
          System.out.println("abc\tdef\tg\nhi\tj\\\tk\n---");
        }
      }
    7. "int" variables

      a)  Declare a variable and use it

      class MyCode {
        public static void main(String[] args) {
          int x = 3;
          System.out.println(x);
        }
      }

      b)  Same, but with a nicer UI

      class MyCode {
        public static void main(String[] args) {
          int x = 3;
          System.out.println("x = " + x);
        }
      }

      c)  Use a variable without declaring it

      class MyCode {
        public static void main(String[] args) {
          System.out.println("x = " + x);  // ERROR!  No such variable as x
        }
      }

      d)  Declaring and assigning on different lines

      class MyCode {
        public static void main(String[] args) {
          int x;  // declare the variable
          x = 5;  // initialize it (assign it its first value)
          System.out.println("x = " + x);
        }
      }

      e)  Assigning and re-assigning

      class MyCode {
        public static void main(String[] args) {
          int x;
          x = 1;
          System.out.println("x = " + x); // prints "x = 1"
          x = 2;
          System.out.println("x = " + x); // prints "x = 2"
        }
      }

      f)  Using before assigning a value

      class MyCode {
        public static void main(String[] args) {
          int x;  // declared, but not assigned a value
          System.out.println("x = " + x);  // ERROR!  x is not initialized
        }
      }

      g)  Using two variables

      class MyCode {
        public static void main(String[] args) {
          int x = 1;
          int y = 2;
          System.out.println("x = " + x);
          System.out.println("y = " + y);
        }
      }

      h)  Declaring two variables on one line

      class MyCode {
        public static void main(String[] args) {
          int x = 1, y = 2;
          System.out.println("x = " + x);
          System.out.println("y = " + y);
        }
      }

      i)  Using two variables in an expression

      class MyCode {
        public static void main(String[] args) {
          int x = 1, y = 2;
          System.out.println("x = " + x);
          System.out.println("y = " + y);
          System.out.println("x + y = " + (x + y));
        }
      }

      j)  Same, but with a nicer UI

      class MyCode {
        public static void main(String[] args) {
          int x = 1, y = 2;
          System.out.println("x = " + x);
          System.out.println("y = " + y);
          System.out.println("x + y = "
                             + x + " + " + y
                             + " = " + (x + y));
        }
      }
    8. More about "int" variables

      a)  Reading a "int" variable

      class MyCode {
        public static java.util.Scanner scanner = new java.util.Scanner(System.in);
        public static void main(String[] args) {
          int x;
          x = scanner.nextInt();  // reads an int from the user
          System.out.println("x = " + x);
        }
      }

      b)  Same, but with a nicer UI

      class MyCode {
        public static java.util.Scanner scanner = new java.util.Scanner(System.in);
        public static void main(String[] args) {
          int x;
          System.out.print("Enter an integer: "); // prompt the user
          x = scanner.nextInt();
          System.out.println("x = " + x);
        }
      }

      c)  Overflow

      class MyCode {
        public static void main(String[] args) {
          int x = 1234567890;  // about 1.2 billion
          int y = 1355779246;  // about 1.3 billion
          System.out.println("x = " + x);
          System.out.println("y = " + y);
          System.out.println("x + y = "
                             + x + " + " + y
                             + " = " + (x + y));
          // Prints:  x + y = 1234567890 + 1355779246 = -1704620160
        }
      }

      d)  Integer division

      class MyCode {
        public static void main(String[] args) {
           System.out.println("20/3 = " + (20/3));
           System.out.println(" 6/3 = " + ( 6/3));
           System.out.println(" 5/3 = " + ( 5/3));
           System.out.println(" 2/3 = " + ( 2/3));
        }
      }

      e)  The modulus or remainder operator (%)

      class MyCode {
        public static void main(String[] args) {
           System.out.println("20%3 = " + (20%3));
           System.out.println(" 6%3 = " + ( 6%3));
           System.out.println(" 5%3 = " + ( 5%3));
           System.out.println(" 2%3 = " + ( 2%3));
           System.out.println(" 0%3 = " + ( 0%3));
        }
      }

Carpe diem!