CS201 - Spring 2014 - Class 2

  • Admin
       - MBH 632 door code

  • review problems for the day

  • Java tutorials and reading

  • Quick review of Java basics
       - declaring a variable
          type variable_name = value;

          int x = 10;

          - you can only declare a variable with a given scope
       - you can also just declare a variable without assigning to it:
          type variable_name;
          int x;

          - Java will give it some default value depending on the type (0 for numbers)
       - assigning to a variable
          - once a variable has been declared you can assign to it using '='
          x = 20

       - to define a new method (aka functions):
          return_type function_name(parameter1, parameter2, ...){

             return some_value; // unless the return_type is void!
       - for loop

  • primitive data types
       - Java has 8 "primitive" types: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
       - These give us more control over how the data is stored
       - the most common ones we'll use for now:
          - int
          - double
          - boolean
          - char

  • object oriented programming
       - Java is an object oriented programming language (and so is Python, but not NetLogo)
       - objects are the key component of the language
       - What is an object?
          - an object is a particular instance of a class of objects
          - Two key components:
             1) data
             2) methods, that is, ways for accessing and manipulating that data
       - A class defines a blueprint for a collection of related objects
       - Classes of objects also define types

  • String class
       - Strings are objects: they have data and methods
       - What methods would you like to have for strings?
          - Put another way, what types of questions do you want to ask about strings and how would you like to manipulate them?
       - Look at the documentation for the String class: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html
          - A constructor defines how we can construct (create) an object
          - many, many methods:
             - charAt (which we've seen)
             - endsWith
             - concat
             - length
             - split
             - startsWith
             - substring
       - Calling methods
          - To declare a new string:
             String s = "this is some string";
             - strings are special and we can create them using the "" notation
          - The variable s is now a reference to a new string object
          - To call methods on that string object, we use the "dot" notation
             - s.endsWith("string")
             - s.charAt(0)
             - s.substring(0, 10)
             - ...
          - the dot notation says on the object that this variable references, call this method
       - What is the data for the string class?
          - the string itself, i.e. the sequence of characters
       - How is it stored?
          - We don't care!
          - This is the power of object oriented programming
             - When we define a class of objects, we describe the methods, which is how you can interact with the object
             - The details of the underlying implementation are hidden from us
          - Why is this beneficial?
             - hides details we don't care about
             - provides a very concrete way of interacting with the object
             - prevents other programmers (i.e. people using the object) from messing with the data in ways we don't want
             - allows us to change the underlying representation if we want (as long as we don't change the public interface)      
  • the primitive data types are NOT objects

  • Look at the isPrime and isPalindrome functions in SimpleFunctions.java in SimpleFunctions code

  • if/else statement
       - Java supports:
          - stand-alone if statements
          - if/else statements
          - "if/else if" statements
       - like other constructs, the condition needs to be in parentheses and the code inside the block surrounded by {}
       - else statement is optional
       - the "else-if" statement is just an else followed by another if

  • recursion
       - Java supports recursive functions

  • while loop
       - works just like in python or other languages
       - like other constructs, the condition needs to be in parentheses and the code inside the block surrounded by {}