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 Lecture 2 1 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 Lecture 2 2 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 Lecture 2 3 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 • Clark Kent • Wei Liu kennedjo kentclar liuwei • Password: passwd (change it!) Lecture 2 4 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 Lecture 2 5 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 */ Lecture 2 6 BankAccount.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 62 // joi/1/bank/BankAccount.java // File name, date, authors in // comments at top of file // // Copyright 2003 Bill Campbell and Ethan Bolker /** * A BankAccount object has a private field for * this account's current balance, and public * methods to return and change the balance. * Brief description of class instances in * @see Bank javadoc comment block /** … */ * @version 1 */ public class BankAccount Official Java declaring the class { } Java code between these braces describes fields and methods of BankAccount objects Lecture 2 7 fields : what an object looks like • Each field has a type, a name and a value • BankAccount object picture BankAccount type of object int balance: 999 type of field field name: value • Code in BankAccount.java tells type (int, for integer) and name (balance – good choice by programmer) 17: private int balance; // work in whole dollars • Value (999) may change from time to time Lecture 2 8 Bank object’s fields (picture) Bank type of object “Engulf and Devour” BankAccount String bankName: type of field int int balance: 200 999 BankAccount account1: name of field BankAccount type of object BankAccount account2: Terminal int int balance: 200 200 Terminal atm: Lecture 2 when value is another object, 9 use an arrow Bank object’s fields (code) • From Bank.java, showing types and names of four fields type name 22 23 24 25 26 27 private String bankName; // Bank’s name private Terminal atm; // talks to customer private BankAccount account1; 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 Lecture 2 10 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 2 11 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 2 12 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 2 13 Message invoking a method Bank.java line 107 String command = atm.readWord(“transaction: ”); prompt = “transaction: ” readWord (String prompt ) { print prompt on screen get first word user types command = thatWord return thatWord } somewhere in Terminal.java execution flow: line 107 in Bank.java sends a readWord message to a Terminal. Code for readWord method in Terminal.java runs, then work resumes at line 104 in Lecture 2 Bank.java 14 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 Lecture 2 15 emacs is a programmer’s editor tab, java indent 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 • prettyprint • compile Lecture 2 16