
































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

































Accessor and Mutator Classes ^ Accessor•
Does not change the state of the implicitparameter (e.g.
getBalance
)
^ Mutator• changes the state of the implicit parameter
(e.g. deposit ) ^ Rule of thumb• Mutator should return void ^ Immutable class:• all methods are accessors (e.g.
String
)
Preconditions
^ Publish preconditions so the caller won'tcall methods with bad parameters/**
amount >= 0
)
*/ ^ Typical use:• To restrict the parameters of a method• To require that a method is only called when
the object is in an appropriate state ^ Method can do
anything
if called when
precondition not fulfilled
^ Condition that's true after a method hascompleted.^ /**
*^ Deposits
money
into
this
account.
*^ (Postcondition:
getBalance()
>=^
0)
*^ @param
amount
the
amount
of^
money
to^
deposit
*^ (Precondition:
amount
=^
*/ ^ Don't document trivial postconditions that repeatthe
@return
clause
^ Contract• If caller fulfills precondition, method must fulfill
postcondition
BankAccount.java
/**A^ bank
account
has^ a balance
that^ can
be^ changed
by^ deposits
and^ withdrawals.*/
public class BankAccount
private
static
int^ lastAccountNumber; private
int^ accountNumber; private
double
balance; /**Constructs
a^ bank
account
with^ a^ zero^
balance*/
public BankAccount()
this(0);} /**Constructs
a^ bank
account
with^ a^ given
balance
*^ @param
initialBalance
the^ initial
balance*/
public BankAccount(double
initialBalance){
balance
=^ initialBalance; lastAccountNumber++;accountNumber
=^ lastAccountNumbre; } /**^ Deposits
money into^ the
bank^ account.
*^ @param
amount
the^ amount
to^ deposit*/
public void^ deposit(double
amount)
double^
newBalance
=^ balance
+^ amount;
balance
=^ newBalance; } /**Withdraws
money^
from^ the
bank^ account.
*^ @param
amount
the^ amount
to^ withdraw*/
public void^ withdraw(double
amount){
double^
newBalance
=^ balance
-^ amount;
balance
=^ newBalance; } /**Gets^ the^ current
balance
of^ the
bank^ account.
*^ @return
the^ current
balance
public double
getBalance()
{^ return
balance;
Reference Types: Consequences
public
class
Publication
{
private
String
author;
Publication* @param*/ public
Publication(String
author){
this.author
=^ author;
String
a; a^ =^
this.author; } }
this. author
a this. author
a
^ Can always use class without importing^ java.awt.Color
^ Tedious to use fully qualified name ^ Import lets you use shorter class name^ import
java.awt.Color;
.^.^
. Color
backgroundColor
=^ new
Color(.
.^ .);
^ Can import all classes in a package^ import
java.awt.;* ^ Never need to import java.lang
^ Package names should be unambiguous ^ Recommendation: start with reverseddomain name^ org.omg
.CORBA org.maju
.advprogramming
^ Path name should match package name^ org/maju/advprogramming/Course.java
^ To learn how to throw exceptions ^ To be able to design your own exceptionclasses ^ To understand the difference betweenchecked and unchecked exceptions ^ To learn how to catch exceptions ^ To know when and where to catch anexception
Separating Error Handling Code Traditional approach to error handling: methodreturns error code^ readFile {
^ What happens ifthe file can't be opened?the length of the file can't be determined?enough memory can't be allocated?if the read fails?if the file can't be closed? ^ To answer, you'd have to add a lot of code to doerror detection, reporting and handling
Separating Error Handling Code errorCodeType