Notes
Slide Show
Outline
1
CS110 Lecture 3
February 2, 2004
  • Announcements
    • hw1 part 1 very poorly proofread!
    • hw1 part 2 - electronic collection tonight: memo.txt and Bank.java
    • hw2 available soon (probably tomorrow)
    • java and XEmacs available from lab PCs to burn onto a CD
    • class ends at 12:45, not 12:30 (your loss last time)
    • read ahead in JOI Chapter 2
  • Agenda
    • questions
    • software development cycle – emacs as an IDE
    • messages and methods
    • flow control: messages, while, if else


2
emacs is a programmer’s editor
  • compile: ctrl-x ctrl-m
  • loop through compiler errors:                                 ctrl-x ` (backquote)
  • run programs: ctrl-x ctrl-r
  • prettyprint: tab, java ® indent
    • if a line doesn’t indent to where you think it should, look for a previous error – missing ; or unbalanced {} or ()
  • buffers and windows
  • learn from XEmacs and XEmacs/Java tutorials
3
Compiling in emacs
4
Run the program
ctrl-x ctrl-r opens a *shell* buffer
5
Buffers and frames
  • c-x c-m and c-x c-r split emacs frame, show *compilation* or *shell* in other half
  •  c-x 1 unsplits frame, shows only buffer containing cursor
  • Buffers pulldown menu allows you to switch between buffers (keyboard:  ctrl-x b )
  • Use File ® Open in New Frame to create a second frame  (not Open in New Window)
  • If both frames show same buffer, typing in one is visible in the other
6
 
7
Prettyprinting
  •  Whitespace (blank, tab, newline) conventions
    •  one statement per line (most lines end with ;)
    •  use blank lines wisely
    •  indent if statements and while loops
    •  line up {} braces
    • TAB in emacs will indent a line properly
  •  Follow conventions  in code you modify
  •  We charge for prettyprinting errors in hw!
8
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
9
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

10
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
11
Message invoking a method
12
Message invoking a method
13
Message invoking a method
14
Exam question
  • Where are messages passed in the following lines of code? In each case identify
    • the object sending the message
    • the name of the message
    • the name and type of the object receiving the message
    • the name and type of the parameters (the information sent along with the message)
  • Practice on the code you read every day
  • Quiz your friends
15
Java flow control
  • Flow control: which line executes next?
  • Command java Foo starts execution in main method in class Foo (files Foo.java, Foo.class)
  • Java executes statements in order
    • (statement ends with ‘;’, usually one per line)
  • Sequential order changes when
    • a message is sent, invoking a method elsewhere
    • code reaches end of  a loop body ( e.g. while)
    • if - else logic skips statements


16
a:\> java Bank
  • 138  public static void main( String[] args )
  • 139  {
  • 140      Bank javaBank = new Bank(                          "Engulf and Devour" );
  • 141      javaBank.open();
  • 142  }


  • Program starts at line 140 (first in Bank main method),                          creating a new Bank object
  • Line 141 sends that Bank object a message asking it to open itself
  • Execution jumps to the code in Bank’s open method
  • When that method is done we’re at the end of main, program is done, control returns to shell (shell prompt)
17
open method in class Bank
  • 57 public void open()
  • 58 {
  • 59   atm.println( "Welcome to " + bankName );
  • 60   boolean bankIsOpen = true;
  • 61   while ( bankIsOpen ) {
  • 62      BankAccount account =                      this.whichAccount();
  • 63      if ( account == null ) {
  •          bankIsOpen = false;
  • 65      }
  • 66      else {
  • 67          this.process…ForAccount(account);
  • 68      }
  • 69   }
  • 70   atm.println( "Goodbye from " + bankName );
  • 71 }
18
while: Bank.java lines 58-64
  • Line 60: variable bankIsOpen has  type  boolean: value may be  true or  false
  • At line 61, while tests value of bankIsOpen
    • go to line 62 if bankIsOpen == true
    • go to line 70 if bankIsOpen == false
  • Value of bankIsOpen might change from true to false in lines 63, 64
  • Loop body is code between brace { on line 61 and brace } on line 69
  • Note how indentation (prettyprinting) helps you see the loop body


19
if: Bank.java lines 63-68
  • Line 62 gets value for variable account  of type BankAccount
  • Line 63 tests the value of that variable
  • if account == null // no account
    • execute line 64
    • skip to line 69 (then back to 61 and then to 70)
  • else (that is, otherwise)
    • skip line 64,65
    • execute line 67
    • continue at line 69 (then back to 61, then to 62)
  • Note how indentation (prettyprinting) helps you see the logic


20
Talking about programs
  • Add these words to your active vocabulary:          I use them in class. You use them when you ask questions, talk to classmates, in your memo, and in your exam
  • field, type, value, state, behavior, message, method, program, object, syntax, semantics, model, simulate, architecture, design, file, class, implementation, user interface, compile, edit, run, test, error, source code, flow control, loop, convention, prettyprint, ...