Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 21 CS110 Lecture 2 January 29, 2004 Announcements –hw1 part 1 – due right now –hw1 part 2 – due Tuesday night Questions Agenda –turnin –Object.

Similar presentations


Presentation on theme: "Lecture 21 CS110 Lecture 2 January 29, 2004 Announcements –hw1 part 1 – due right now –hw1 part 2 – due Tuesday night Questions Agenda –turnin –Object."— Presentation transcript:

1 Lecture 21 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 Lecture 22 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 Lecture 23 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 Lecture 24 Electronic hw submission turnin system: http://turnin.cs.umb.edu/ (available by Monday, perhaps sooner)http://turnin.cs.umb.edu/ 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 Lecture 25 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 Lecture 26 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 Lecture 27 1 // joi/1/bank/BankAccount.java 2 // 3 // 4 // Copyright 2003 Bill Campbell and Ethan Bolker 5 6 /** 7 * A BankAccount object has a private field for 8 * this account's current balance, and public 9 * methods to return and change the balance. 10 * 11 * @see Bank 12 * @version 1 13 */ 14 15 public class BankAccount 16 { 62 } BankAccount.java File name, date, authors in // comments at top of file Brief description of class instances in javadoc comment block /** … */ Java code between these braces describes fields and methods of BankAccount objects Official Java declaring the class

8 Lecture 28 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 balance:999 int fields : what an object looks like BankAccount 17: private int balance; // work in whole dollars type of object field name:value type of field

9 Lecture 29 Bank object’s fields (picture) “Engulf and Devour” int 200 int 200 BankAccount balance: BankAccount balance: Terminal String bankName: Bank BankAccount account1: BankAccount account2: Terminal atm: 999 200 int name of field type of field value when value is another object, use an arrow type of object

10 Lecture 210 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 Lecture 211 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 Lecture 212 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 Lecture 213 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 Lecture 214 Bank.java line 107 somewhere in Terminal.java Message invoking a method String command = atm.readWord(“transaction: ”); readWord (String prompt ) { print prompt on screen get first word user types return thatWord } prompt = “transaction: ” command = thatWord 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 Bank.java

15 Lecture 215 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 Lecture 216 emacs is a programmer’s editor prettyprint tab, java  indent compilectrl-x ctrl-m tools  compile  compile loop through compiler errors ctrl-x ` (backquote) tools  compile  next error run programsctrl-x ctrl-r tools  shell  shell learn from XEmacs/Java tutorial


Download ppt "Lecture 21 CS110 Lecture 2 January 29, 2004 Announcements –hw1 part 1 – due right now –hw1 part 2 – due Tuesday night Questions Agenda –turnin –Object."

Similar presentations


Ads by Google