04 ImplementingClasses
04 ImplementingClasses
• Documented Stubs
• Test-First Programming
Functions Objects
Black box Function Constructor and
exposes signature method
signatures
Encapsulated Operation Data storage and
inside the box implementation operation
implementation
Q1, 2
Essentially based on Big Java
◦ But using explicit this references
◦ And putting fields at the top of the class
Comparing and contrasting with Python
◦ Source code with Python examples is in SVN for
reference
Next slide shows the entire class
◦ Subsequent slides discuss it piece by piece
The BankAccount
class
Java Python
Q3
Javadoc comment precedes the method definition (always if
the method is public, optionally if the method is private)
Java Python
Q4-6
Javadoc comment precedes
the constructor definition
Java Python
Java constructors are
almost always public Q7-9
The public interface of an BankAccount
object:
◦ Is the inputs and outputs of
the black box BankAccount()
◦ Defines how we access the BankAccount(double initAmount)
object as a user
◦ Consists of:
void deposit(double amount)
public constructors of its
class, plus void withdraw(double amount)
public methods of its class double getBalance()
The private implementation
of an object consists of:
◦ Its (private) instance fields
◦ Definitions of its constructors
and methods
The above shows the public interface of BankAccount objects.
The next slides show their private implementation.
Q10
Generally no Javadoc here, since you should
choose variable names that are self-documenting.
Q11
/** javadoc… */ def __init__(self,
initAmt=0.0):
public BankAccount(double initAmount) { """docstring..."""
this.balance = initAmount; self.balance = initAmt
Java Python
Q12
/** javadoc… */ def getBalance(self):
"""docstring..."""
public double getBalance() { return self.balance
return this.balance;
}
private field
this.characterToCensor = ‘e’;
Step 1: Create the
(initially empty) class
Step 2: Write
documented stubs
this.characterToCensor = for the public
characterToCensure; interface of the class
Do you understand
what it means to use
documented stubs ?
Do you know what
you must document?
(Answer: anything public.)
Do you see why Censor
needs a field? How the
field is initialized? How
Censor the field is referenced
final version (using this)?
How Censor has two
constructors? How those
constructors are called in
CensorTest?
Should we have made a field for the
‘*’ constant? (Probably.)