03.ppt

advertisement
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
Lecture 3
1
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 Lecture
and3 XEmacs/Java tutorials
2
Compiling in emacs
C-x backquote
• positions cursor at
next bad line in file,
• scrolls to
next error message
Lecture 3
3
Run the program
ctrl-x ctrl-r opens a *shell* buffer
type
java Hello
at the shell prompt
type
whatever you like
at the program’s prompt
program echoes what you
type, then ends itself
shell prompt reappears
Lecture 3
4
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
Lecture 3
5
buffer pulldown menu
three emacs buffers
showing in two frames
taskbar – click here, or alt-TAB to visit hidden window
Lecture 3
CS110 home page
6
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!
Lecture 3
7
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
Lecture 3
8
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
Lecture 3
9
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
Lecture 3
10
Message invoking a method
Bank.java line 107
String command =
atm.readWord(“transaction: ”);
prompt = “transaction: ”
readWord (String prompt )
{
print prompt on screen
theWord = word user types
command = theWord
return theWord
}
somewhere in Terminal.java
execution flow: line 107 in Bank.java sends a readWord
message to a Terminal. Code for readWord method
somewhere in Terminal.java runs, then work resumes at
Lecture 3
line 108 in Bank.java
11
Message invoking a method
Bank.java line 62
BankAccount account =
this.whichAccount( )
Bank.java line 77
whichAccount( )
{
send atm readInt message
get number user types
account = that account
return the right BankAccount
}
execution flow: line 62 in Bank.java sends a whichAccount
message to itself. The method whichAccount runs,
returning a BankAccount (account1, account2 or null),
then work resumes at line 63 in Bank.java
Lecture 3
12
Message invoking a method
Bank.java line 67
this.process…For…( account)
Bank.java line 101
process…For…( BankAccount
account)
{
loop until user types “exit”
account is the account1 or the
account2 field of the Bank
do what user asks
}
execution flow: line 63 in Bank.java sends a process…
message to itself, telling it which account to work with. The
method process… runs, then work resumes at line 70 in
Bank.java
Lecture 3
13
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
Lecture 3
14
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
Lecture 3
15
a:\> java Bank
138
139
140
141
142
public static void main( String[] args )
{
Bank javaBank = new Bank(
"Engulf and Devour" );
javaBank.open();
}
• 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)
Lecture 3
16
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 ) {
64
bankIsOpen = false;
65
}
66
else {
67
this.process…ForAccount(account);
68
}
69
}
70
atm.println( "Goodbye from " + bankName );
Lecture 3
17
71 }
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
Lecture 3
18
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
Lecture 3
19
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, ...
Lecture 3
20
Download