+ All Categories
Home > Documents > Lecture 101 CS110 Lecture 10 Thursday, February 26 2004 Announcements –hw4 due tonight –Exam...

Lecture 101 CS110 Lecture 10 Thursday, February 26 2004 Announcements –hw4 due tonight –Exam...

Date post: 05-Jan-2016
Category:
Upload: curtis-porter
View: 215 times
Download: 0 times
Share this document with a friend
Popular Tags:
21
Lecture 10 1 CS110 Lecture 10 Thursday, February 26 2004 • Announcements – hw4 due tonight Exam next Tuesday (sample posted) • Agenda – questions – what’s on the exam? – collections (Chapter 4) – arrays – better Banks and BankAccounts
Transcript
Page 1: Lecture 101 CS110 Lecture 10 Thursday, February 26 2004 Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.

Lecture 10 1

CS110 Lecture 10Thursday, February 26 2004

• Announcements– hw4 due tonight– Exam next Tuesday (sample posted)

• Agenda– questions– what’s on the exam?– collections (Chapter 4)– arrays– better Banks and BankAccounts

Page 2: Lecture 101 CS110 Lecture 10 Thursday, February 26 2004 Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.

Lecture 10 2

Exam preview• Based on course work so far (JOI

Chapters 1,2,3)• Given source code Foo.java to read:

– what does java Foo do?– find tokens, keywords, identifiers, messages …– comment on programming conventions– draw a box-and-arrow picture

• Write simple code using if/else, while, for• Sample posted on course web page

Page 3: Lecture 101 CS110 Lecture 10 Thursday, February 26 2004 Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.

Lecture 10 3

Vocabulary java, token, keyword, identifier, convention,

API, message, method, int, double, boolean, if, else, while, for, declaration, delegation, variable, class, instance, field, source code, static, scope, client, comment, javadoc, compile, constructor, final, flow control, object, main, string, syntax, semantics, this, true, false, unit test, self documenting test, new, null, void, public, private, pseudocode, …

Page 4: Lecture 101 CS110 Lecture 10 Thursday, February 26 2004 Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.

Lecture 10 4

Applications

• Bank, BankAccount

• LinearEquation, Temperatures, PhoneBill

• IntArithmetic, DoubleArithmetic, Exponentiate

• TextFile

• HLine, Box, Screen, VLine, Frame, …

Page 5: Lecture 101 CS110 Lecture 10 Thursday, February 26 2004 Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.

Lecture 10 5

hw4 - Exponentiate

• needs just main

• pseudocode– create a Terminal– get base and exponent from user– create any BigIntegers you need– send a message to do the computation– print the result

Page 6: Lecture 101 CS110 Lecture 10 Thursday, February 26 2004 Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.

Lecture 10 6

Collections• More programs manipulate data than do arithmetic

– Library catalog manages a collection of books

– Registrar maintains a database of student records

– EStore has list of Items in Warehouse and in ShoppingCart

– Bank deals with a list of BankAccounts

– Screen manages a set of pixels

– Windows folder holds files (and other folders)

• … list, set, database, group, collection, container ...

Page 7: Lecture 101 CS110 Lecture 10 Thursday, February 26 2004 Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.

Lecture 10 7

Collections

• A Collection is an object that stores things

• Collection API must provide ways to– make an empty container (constructor)– put things in: put, insert, add, store, ...– know what’s there:

• get, retrieve, find, lookup, ... • loop on contents

– throw things out: delete, remove, kill, ...

• Java provides array, List, Map, Set

Page 8: Lecture 101 CS110 Lecture 10 Thursday, February 26 2004 Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.

Lecture 10 8

A better Bank (Version 4)...

• Maintains a list of BankAccounts

• Allows banker to create new accounts while the Bank is open

• Keeps track of the total balance of all the accounts in it, and of the total number of transactions performed by its accounts

Page 9: Lecture 101 CS110 Lecture 10 Thursday, February 26 2004 Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.

Lecture 10 9

Array• Simplest Java object for managing a list• array syntax uses square brackets [] several ways • Bank.java (version 4) uses an array• Declare field accountList of type “array of

BankAccounts” (line 32)

private BankAccount[] accountList ; • Create the array, ready to hold 3 Items (60)

accountList = new BankAccount[NUM_ACCOUNTS];

Page 10: Lecture 101 CS110 Lecture 10 Thursday, February 26 2004 Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.

Lecture 10 10

An array for 3 BankAccounts

// fill array on lines 66-68

accountList [0] = new BA( 0, this); accountList [1] = new BA(100, this); accountList [2] = new BA(200, this);

• Improved BankAccount object has a field to hold a reference to the Bank it belongs to (more later)

Page 11: Lecture 101 CS110 Lecture 10 Thursday, February 26 2004 Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.

Lecture 10 11

boxes and arrows for arraysaList BA[]; declares array of BA (line 32)

0:

BankAccount[]

1:

2:

Bank

accountList:

0:

BankAccount[]

1:

2:

BankAccount

BankAccount

BankAccount

BankAccount[]

Bank

accountList:

null

null

null

null

Bank

accountList:

aList = new BA[3]; creates array object (60)

BankAccount[]

BankAccount[]

aList[0]

=new BA(); fills array object (66-)

Page 12: Lecture 101 CS110 Lecture 10 Thursday, February 26 2004 Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.

Lecture 10 12

Seeing what’s in an array

BA acct = accountList[1];

atm.println(“Account 1 balance: ” + acct.getBalance());

prints

Account 1 balance: 100

Page 13: Lecture 101 CS110 Lecture 10 Thursday, February 26 2004 Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.

Lecture 10 13

looping over an array// lines 211-214 in report methodfor (int i = 0; i < NUM_ACCOUNTS; i++) { terminal.println( i + “\t” + accountList[i].getBalance() + … )

}

• accountList[i] is (a reference to) the Object at position i.• send a getBalance message to each account in the Bank in

succession• prints 0 0 x

1 100 x 2 200 x\t is tab

Page 14: Lecture 101 CS110 Lecture 10 Thursday, February 26 2004 Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.

Lecture 10 14

array summary

• declare: Type[ ] myArray; // Type is class name, // or primitive

• create: myArray = new Type[size]; // int size• put: myArray[position] = …; // int position• get: Type x = myArray[position];

myArray[position].message();• length: myArray.length; // final public field• range: 0,1,…, myArray.length-1read short self documenting program Array.java

Page 15: Lecture 101 CS110 Lecture 10 Thursday, February 26 2004 Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.

Lecture 10 15

arrays are Objects• Created with new• Size is determined at creation time but not at

declaration time• The square brackets provide the special syntax for

sending an array a message• myArray[i] refers to the value stored at index i

(which must be an integer)• Stored values may be primitive (in the box) or a

reference to an object (an arrow) but all values must have the same type

Page 16: Lecture 101 CS110 Lecture 10 Thursday, February 26 2004 Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.

Lecture 10 16

Improved BankAccount• Private fields (all have getter methods)

int balance;

int transactionCount;

Bank issuingBank;

• Constructorpublic BankAccount(int initialBalance, Bank issuingBank)

{

this.issuingBank = issuingBank;

this.deposit(initialBalance);

}

Page 17: Lecture 101 CS110 Lecture 10 Thursday, February 26 2004 Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.

Lecture 10 17

Command line arguments in Javapublic static void main(String[] args)

• args is a parameter for the main method• The declaration says it’s an array of String objects• Its contents are the words on the command line after java ClassName

• Argument array can have any name you wish– args is conventional– old C programmers may call it argv

Page 18: Lecture 101 CS110 Lecture 10 Thursday, February 26 2004 Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.

Lecture 10 18

Command line arguments in Java• class CommandLineArgsDemopublic static void main( String[] args ){ for (int i = 0; i < args.length; i++){ System.out.println('|'+args[i]+'|');

}}• %> java CLID message is “hello, world” |message| |is| |hello, world|

• Experiment with %> java Bank -e

Page 19: Lecture 101 CS110 Lecture 10 Thursday, February 26 2004 Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.

Lecture 10 19

Improved Bank • banker commands

– create new account– report on totals– deal with a customer

• deposit

• withdraw

• get balance

• transfer

Page 20: Lecture 101 CS110 Lecture 10 Thursday, February 26 2004 Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.

Lecture 10 20

Bank and BankAccount cooperatepublic int deposit(int amount){ this.incrementBalance( amount); this.countTransaction(); return amount ;}public void incrementBalance(int amount){ balance += amount; this.getIssuingBank().

incrementBalance( amount );}

this BankAccount asks the Bank it is in to update its own balance field

Page 21: Lecture 101 CS110 Lecture 10 Thursday, February 26 2004 Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.

Lecture 10 21

A Bank object and its fields

Bank

atm:

accountList:

balance:

transactionCount:

BankAccount[]

Terminal

balance:

int

transactionCount:

issuing Bank:

int

Bank

BankAccount

300

4

bankName:

BankAccount[]

0:

1:

2:

int

int

Terminal

String“River Bank”

600

9balance:

int

transactionCount:

issuing Bank:

int

Bank

BankAccount

200

3

balance:

int

transactionCount:

issuing Bank:

int

Bank

BankAccount

100

2


Recommended