Notes
Slide Show
Outline
1
CS110 Lecture 11
Thursday, March 4, 2004
  • Announcements
    • hw5 available, due next Thursday
    • Exam answers posted
  • Agenda
    • exam results
    • arrays
    • better Banks and BankAccounts
    • ArrayLists
2
Exam results
  • Range: (<30) – (100/102)
  • 50 isn’t wonderful, but it’s OK (passing)
  • Answers posted – no questions answered until you have studied them
  • Check my addition, please (both ways!)
  • Letter grade ranges on next slide are for exam only, don’t take hw into account
3
 
4
Array
  • Simplest Java object for managing a list
  • array syntax uses square brackets [] several ways
  • See ArrayDemo.java in JOI
  • Study CommandLineArgsDemo.java (JOI)
  • Study ArrayDemo2.java (handout)



5
Command line arguments in Java
    • public 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



6
Command line arguments in Java
  • class CommandLineArgsDemo
  • public 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


7
ArrayDemo2.java
  • Array practice, some idioms/tricks
    • converting Strings to ints
    • finding sum, maximum and minimum

    • > java ArrayDemo2 -1 -2 -3 4 5
    • total: 3
    • maximum: 5
    • minimum: -3



8
Two dimensional arrays,                           nested for loops                          (from Screen.java)
  • 24 private char[][] pixels;
  • 45 public void clear()
  • 46 {
  • 47   for (int x = 0; x < width; x++) {
  •       for ( int y = 0; y < height; y++ ) {
  •           pixels[x][y] = BLANK;
  •       }
  •   }
  • }


9
arrays are Objects                                  (even though there is no Array class) …
  • The square brackets provide the special syntax for sending an array a message
  • Created with new
  • Size is determined at creation time but not at declaration time
  • 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
10
array summary
  • declare:  Type[ ] myArray;                           // Type is class name, or primitive
  • create:    myArray = new Type[ size ];
  • put:        myArray[ index ] = …;
  • get:        Type x = myArray[ index ];                 myArray[ index ].message();
  • length:    myArray.length;
  • range:     0,1,…, myArray.length-1


11
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



12
Arrays in the better Bank
  • 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];


13
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)


14
boxes and arrows for arrays
15
Improved BankAccount
  • Private fields (all have getter methods)
    • int  balance;
    • int  transactionCount;
    • Bank issuingBank;
  • Constructor
    • public BankAccount(int initialBalance,               Bank issuingBank)
    • {
    •     this.issuingBank = issuingBank;
    •     this.deposit(initialBalance);
    • }
16
Improved Bank
  • banker commands
    • create new account (for hw5)
    • report on totals
    • deal with a customer
      • deposit
      • withdraw
      • get balance
      • transfer
17
Bank and BankAccount cooperate
  • public int deposit( int amount )
  • {
  •     this.incrementBalance( amount );
  •     this.countTransaction();
  •     return amount ;
  • }
  • public void incrementBalance(int amount)
  • {
  •     balance += amount;
  •     this.getIssuingBank().         incrementBalance( amount );
  • }
18
 A Bank object and its fields
19
ArrayList
  • A Collection class in the Java API
  • Like an array: stores elements at positions 0,1,…
  • Can grow as needed!
  • Unlike an array in other ways too
    • stores only objects (not primitive types)
    • can store different kinds of objects simultaneously                         (so you must cast when retrieving)
    • send messages instead of using [ ] for access
  • Read ArrayListDemo.java
20
ArrayList API
  • can store:      any Object, not primitive types
  • declaration:  ArrayList myList
  • creation:       myList = new ArrayList( )
  • put at end:    myList.add( obj )
  • put at index: myList.add( index, obj )
    • moves later entries down the list
  • get:               (Type)myList.get( index )
    • cast to proper Type after get
  • replace:        myList.set( index, obj )
  • remove:       myList.remove( index )
  • size:             myList.size( )
  • looping:       for( ; ; )11
21
Boxes and arrows for ArrayList
22
hw5
  • array practice (sorting)
  • ArrayList practice: modify Bank.java so that it uses an ArrayList of BankAccounts instead of an array
    • BankAccounts have numbers 0, 1, 2, …
    • Banker can open as many accounts as she likes!