CS51 - Spring 2010 - Lecture 1

  • http://www.xkcd.com/768/

  • introduction to students
       - name
       - major (or expected major), year
       - CS background
       - Why are you taking this course?
       - What do you hope to get out of the course?

  • Discuss with your neighbor...
       - What is computer science?
          - computer science is more than programming
          - study of computers
          - solving problems using computers as the tool

       - What are computers good at?
          - Following well defined, unambiguous set of instructions (algorithms)
          - Computers can do the same thing over and over again exactly the same way

       - What aren't computers good at?
          - Computers can only do what they're told (via the program)

  • Goals of the class
       - introduction to computer science...
          - design, code and debug medium-sized programs in Java
          - basic analysis of programs for correctness and efficiency
       - Asteroids/Pacman (test programs from last year)

  • Administrative
       - Course web page: http://www.cs.pomona.edu/classes/cs051/
       - Textbook - need to get it
       - Labs - required!
          - we have two lab sections
          - you need to be signed up for one or the other
       - All handouts, labs, assignments, etc. will be posted on the course web site
       - Late policy
       - Grading
       - Academic honesty

  • Style of the course
       - interactive!
          - ask lots of questions
          - participate
          - be expected to do group work in class
       - show lots of demos

  • show MakeBox code
       - Eclipse IDE (Integrated Development Environment)
       - Computer programs are written in computer languages (in our case Java). These are high-level instructions that people can understand. Programs are then compiled (or translated) into more basic instructions for the computer to execute.
       - What do you think this program does?
       - comments, either // or /*
       - The IDE uses colors to help distinguish different types of text in the program
       - Programs use a variety of "special words" in the language (often indicated in red/maroon by the IDE). I'll explain these along the way, but some you'll just have to ignore for now.
       - "new" keyword creates a new object (an instance of a class) and is called the "constructor" of the object
       - What would you need to specify to draw shapes (and text) on the screen?
       - new FilledRect(x_location, y_location, width, height, canvas);
          - new FramedOval(...) defines the bounding rectangle around an oval
          - new Line(x_start, y_start, x_end, y_end, canvas)
       - new Text(text_to_display (in quotes), x_start, y_start, canvas);
       - coordinate system starts in the upper left hand corner with positive x to the right and positive y down
       - curly braces denote blocks of text that go together
          - here we have three main blocks
       - semicolons (;) denote the end of a "statement" or one step for the computer to execute
       - header of the class (MakeBox)
          - public class <class_name> extends WindowController
          - body of the class is defined by the curly braces
          - Class: a set, collection, group or configuration containing members regarded as having certain attributes or traits in common.
       - classes contain methods: ours has two
       - method headers:
          - public void begin()
          - public void onMousePress(Location point)
       - methods denote a chunk of code that will be executed together at the same time
          - when do you think these methods will be called/invoked?
       - What other mouse handling methods do you think we have?
          - onMousePress(Location p)
          - onMouseRelease(Location p)
          - onMouseClick(Location p)
          - onMouseMove(Location p)
          - onMouseDrag(Location p)
          - onMouseEnter(Location p)
          - onMouseExit(Location p)

  • show MakeBox demo

  • show UpDown demo
       - what do you see here?
          - two text strings (UP and DOWN)
          - x coordinates should be the same
          - y coordinate different
       - which method should these be created in?
       - what other methods should we have?
          - onMousePress
             - set color of each text
          - onMouseRelease
             - set color of each

  • Variables
       - allow us to label/name something so that we can refer to it later
       - before you can use a variable, you must declare it
          - put it inside the class body (these are called instance variables)
          - private <variable_type> <variable_name>;
       - we assign values to variables using '=' (which is different the '=' in math)
       - instance variables can be used in ALL methods inside the class and in different calls to the same method
       - variables give us a reference to an object and allow us call methods associated with that particular object

  • show UpDown code
       - Three basic steps to using a variable:
          - define the variable
             private Text up;

             private <type/classname> <variable_name>;
          - assign to it
             up = new Text("UP", 200 220, canvas);
          - use it
             up.setColor(Color.RED);
       - two private instance variables
          - called instance variables because we get a copy of each one for each instance (object) of the class that we create
       - setColor is a method associated with the class Text
       
  • show ImprovedMakeBox demo
       - how does this differ from the previous MakeBox demo?
          - now we create a new box everytime we click
       - what information do we need to know to create the box?
          - the dimensions of the box
          - where to create the box, i.e. where we clicked
       - what do you think the code looks like?

  • show ImprovedMakeBox code
       - really simple!
       - method parameters
          - a way to pass information along to a method
          - defined in the method header:
             - onMousePress(Location pt)
          - can have multiple parameters, which are separated by commas
          - or none (i.e "()")
          - parameters look similar to instance variables:
             - they have a type ("Location" in the example above)
             - and they have a name ("pt" in the example above)
          - we can then use that parameter, just like any other variable
          - when a method is called, that information is supplied to the method and bound to the formal parameter (similar to if we'd done an assignment)
          - in this case, "pt" gets the location of where the mouse was pressed
          - for the other mouse methods, the parameter will similarly be given the location of the corresponding mouse event
       - formal parameters vs. actual parameters
          - like we saw with setColor, we can pass information to the method
       - all we do is create a new FramedRect at the location of the mouse click
       - Location is a class, just like the other ones we've seen... what other methods do you think it has?
          - getX();
          - getY();
       - pt.getX() and pt.getY() are method calls that get the x and y coordinates of the particular Location instance

  • show Spirograph demo
       - What's going on?