CS201 - Spring 2014 - Class 1

  • http://www.somethingofthatilk.com/index.php?id=135

  • introduction
       - what intro class?
          - if this is your first CS class at Middlebury, come talk to me
       - why are you taking this course?
       - what do you hope to get out of this course?

  • goals of the class
       - prerequisites: one of our 100-level classes (or equivalent experience). Come talk to me if you're not sure if this is the right place for you.
       - teach you how to program in Java
       - advanced programming
          - more practice
          - Java has some additional features that allow us to investigate some programming paradigms in more detail
          - more tools
          - object oriented programming
       - data structures
          - which ones have you seen/heard of?
          - way for storing and accessing data
       - basic performance analysis and design
       - after this class, you'll be ready to take many of the upper division CS classes

  • administrative
       - textbooks
       - grade breakdown
       - basic schedule
          - Every class:
             - reading
             - problems for the data
          - Fridays: assignment part 1 will be due (think of this like an out of class lab)
          - Wednesdays: assignment part 2 will be due (this is the main part of the assignment)

  • Your first Java
       - take a look at the first four functions in SimpleFunctions code
          - what do they do?
          - what are some of the similarities with python you notice (there are lots!)?
          - what are some of the differences from python you notice (there are lots!)?

  • some high-level syntactic differences
       - all statements in Java are terminated with a semi-colon. This (not an end of line) indicates to java that the statement is done
       - Java uses braces and parenthesis to identify blocks of code (not indentation, like python)
          - For example, the following are *all* equivalent to the add function
             int add(int num1,
             int num2
             ){return num1+num2;
             }

             int add(int num1, int num2){return num1+num2;}

             int
             add
             (
             int num1,
             int num2
             )
             {
             return
             num1
             +
             num2
             ;
             }

             - Java doesn't care!
          - We will follow certain conventions to make the code more readable, though!
          - Curly braces, {}, denote a block of code
          - parentheses, (), denote parameters or arguments
       - variable and function name conventions
          - like python, all functions and variable names will start with a lowercase letter
          - unlike python, for multi-word variables and functions the convention for java is to use "camel-casing", i.e. capitalize the first letter of each new word
             - For example:
                firstAndLastSame NOT first_and_last_same
                sumUpTo NOT sum_up_to
       - comments: java has two different types of comments
          - single line comments: //, everything following this to the end of the line is a comment and is ignored
          - multi-line comments: /* */
             - Everything between /* and */ is a comment and is ignored
             - by convention, we will often put a * on each line in the comments to indicate that this is still part of the comment, but it is NOT required
             - this can be very useful during testing (or other situations) when you want to temporarily comment out a large section of code

  • statically typed vs. dynamically typed languages
       - What is a type?
          - the type of an object define the types of operations you can perform on that object
       - in strongly-typed languages anything that represents a value has a type (e.g. objects)
       - in dynamically-typed languages each object is checked on the fly when the program is done to make sure
          - For example, is there any problem with the following python statements:
             x = 10
             y = "banana"
             x/y
          
             - Yes! You can't divide a number by a string
             - When would python recognize that this was an error?
                - Not until you run the program and it gets to that particular line
                   - it would check to see what type x was
                   - and check to see what type y was
                   - and only then realize that you can't calculate x/y
       - in statically-type languages the type of objects can be determined *without* running the code
          - often when this means is that the programmer is required to declare the types of things in the languages
          - For example, the above statements would be written in Java as follows:
             int x = 10
             String y = "banana"
             x/y

             - When we declare variables we *have* to specify their type
             - Because of this, Java can easily figure out before running that x/y will cause an error
          - why statically typed?
             - static typing allows errors to be caught much earlier and, often, automatically

  • typed variables
       - every variable and parameter must have a type
       - what are some of the types Java might have?
          - int (integers)
          - float (decimal numbers)
          - double (decimal numbers with greater precision, more on this later)
          - boolean
          - String (strings)
          - many more we'll get to later
       - to declare a new variable, you first specify the type then the name then assign to it
          <type> <name> = <value>;

          - Java will enforce that for the lifetime of the variable, it can only be assigned to something of that type (there are actually a few caveats to this, but more later)
             - For example:
                int x = 10
                x = "banana"

                - Java will complain!

       - when you declare parameters in addition to giving them a name, you also have to specify their type!
          - Java checks these when a function is called
          - For example:
             add(10, "banana")

             - would result in an error, even before the function is called
          - notice this avoids a lot of issues/errors that could come up either by accident or maliciously

  • type functions
       - in addition to having to specify the types of all variables and parameters, you also need to specify the type of value that a function returns
          - add returns an int
          - firstAndLastSame returns a boolean
          - what is void?
             - void is used when a function doesn't return a value

  • Strings
       - Strings are not a built-in data type, like they are in python (and other languages)
       - because of this, there is no special functionality for indexing using []

  • for loops
       - for loops in Java are a bit more general than for loops in python
       - for loops consist of three parts:
          1) a beginning statement that is only run once (initialization)
          2) the condition or test to see if the loop should keep going (condition)
          3) a statement that is run after each loop iteration (increment)

          - all three parts are inclosed in parentheses
          - each of the parts is separated by a semi-colon
          - the main body of the for loop is denoted by braces
          - like other block of code, we will indent to make it more readable
       - When the program is running and it encounters a for loop:
          - first, the initialization statement is run
             - this is only done once for a for loop!
             - often this is a variable declaration, but anything could go here
             - if a variable is declared, the scope of that variable is just the for loop block
          - next, the condition is checked. If it's false, the loop finishes immediately.
          - if the condition is true, the statements in the for loop block are executed
          - after each iteration, the increment condition is run once
          - the condition is then checked again and the process is repeated
       - really just syntactic sugar for a while loop

  • printing
       - not as nice as in python :(
       - A couple of different ways right now:
          - print something and then end the line:
             System.out.println(...)
          - print something, but DON'T end the line:
             System.out.print(...)
       - Java is pretty smart about converting things to strings
          - all built-in data types (ints, float, double, etc.) get converted in a straightforward way
          - most other types of objects also do the right thing

  • ++
       - Java has a few extra short-hands that are not in Pyhon
       - ++ is equivalent to += 1, for example:
          i++;
          
          does the same thing as

          i += 1

          or

          i = i + 1
       - -- also is available