Notes
Slide Show
Outline
1
CS110 Lecture 2
January 29, 2004
  • Announcements
    • hw1 part 1 – due right now
    • hw1 part 2 – due Tuesday night
  • Questions
  • Agenda
    • turnin
    • Object oriented programming (bank simulation)
    • XEmacs
2
Questions
  • Do ask in class when you
    • have come with a question
    • are a little confused
    • think I have made a mistake
    • think others might learn from your question/insight
  • Don’t ask
    • just to show off
    • if you are completely lost (come see me instead)
    • if you haven’t read the book and hw and your email
    • what happened in a class you missed
    • after class about something in the class


3
email etiquette (please)
  • Ask (in email)
    • after you have tried to solve your problem, but before you’ve wasted 8 hours stuck in one place
    • with enough detail so that I can help
  • Don’t expect immediate feedback
  • I may cc question and answer to class
  • Do correspond with  your friends
  • No junk mail
4
Electronic hw submission
  • turnin system: http://turnin.cs.umb.edu/        (available by Monday, perhaps sooner)
  • Instructions: follow link on course web page
  • Your user name (all lower case)
    • up to 6 characters from last name
    • balance to 8 characters from first name
      • John Kennedy ® kennedjo
      • Clark Kent ® kentclar
      • Wei Liu ® liuwei
  • Password: passwd (change it!)
5
Object oriented programming
  • The (software) world consists of objects
  • Each object is an instance of a class
  • An object has
    • fields that describe what it looks like (its state)
    • methods that describe how it behaves
  • One object sends a message to another asking it to use one of its methods to do some work
  • Add italicized words to your vocabulary
  • Illustrate these abstractions with bank example in Java
6
OOP in Java
  • File BankAccount.java describes BankAccount objects (instances of class BankAccount)
  • BankAccount.java has two audiences
    • people (programmers like us)
    • the Java compiler
  • Java files always start with comments
    • a convention good programs honor
    • not a rule in the Java language, but a rule for us
  • Comments are for people to read: Java doesn’t care
  • // text up to end of line is a comment
  • /** special javadoc comment – more later */
  • /* old style comment– rare */


7
BankAccount.java
8
fields : what an object looks like
  • Each field has a type, a name and a value
  • BankAccount object picture




  • Code in BankAccount.java tells type (int, for integer) and name (balance – good choice by programmer)


  • Value (999) may change from time to time
9
Bank object’s fields (picture)
10
Bank object’s fields (code)
  • From Bank.java, showing types and names of four fields
  •                         type         name
  • 22 private String bankName; // Bank’s name
  • 23
  • 24 private Terminal atm; // talks to customer
  • 25
  • 26 private BankAccount account1;
  • 27 private BankAccount account2;
  • Conventions:
    • Class names always begin with an upper case letter
    • Field names always begins with a lower case letter
  • If type is a class then value is that kind of object
11
Messages
  • Ask an object to work for you: send it a message
  • Bank.java (line 116)
  •   account.deposit(amount)         this Bank object sends a deposit message to object account of type BankAccount
  • Java syntax:   object.message(info)
  • syntax: what the program looks like on the page
  • deposit method in BankAccount.java does the work
12
methods : how an object behaves
  • BankAccount has several methods:
  • deposit (int amount) // line 47
    • add amount to current balance, changing value of balance field
  • getBalance( )  // line 58
    • tell whoever sent the message how much money is in this account (value of balance field)
    • balance does not change
    • the empty parentheses tell us this method needs no information from the sender to do its job

13
Messages (reprise)
  • Ask an object to work for you: send it a message
  • Bank.java (line 126)
  •   atm.println(“sorry, . . . ”)        this Bank object sends a  println message      to object atm of type Terminal asking it to print a String on the screen
  • Java syntax:   object.message(info)
  •    println(String something)                 method in Terminal.java does the work
  • Trust Terminal.java to do the right thing
14
Message invoking a method
15
Homework 1
  • Part 1 (hard copy due right now)
    • Play with Bank simulation
  • Part 2 (collected electronically Tuesday night)
    • Play with Bank simulation
    • Improve the Bank simulation
    • Write about your coding and testing
16
emacs is a programmer’s editor
  • prettyprint tab, java ® indent
  • compile ctrl-x ctrl-m
  •                            tools ® compile ® compile
  • loop through compiler errors                                 ctrl-x ` (backquote)                                         tools ® compile ® next error
  • run programs ctrl-x ctrl-r                                                  tools ® shell ® shell
  • learn from XEmacs/Java tutorial