Transcript Ch5

Adding Behavior
• Previous class definitions have passively
maintained attributes.
• Active behavior involves:
– Sequences of actions.
– Choices between alternative actions.
– Repetition of one or more actions.
• Statements inside method bodies.
OOP with Java, David
J. Barnes
Adding Sequential Behavior
1
Putting Things in Order
• Tasks commonly involve performing a
sequence of steps:
– First do this.
– Then do that.
– Finally do the other.
• Controlling a Ship:
– move, setCourse, move, report.
OOP with Java, David
J. Barnes
Adding Sequential Behavior
2
Order and Effect
• Some orders make no sense.
– A ship cannot be moved before it has been
constructed.
• Different orders may have different effects:
– Sell your old car. Pay the money into a bank.
Write a check to buy a new car.
– Write a check to buy a new car. Sell your old
car. Pay the money into a bank.
OOP with Java, David
J. Barnes
Adding Sequential Behavior
3
Order and State
• Order often matters when actions are
dependent upon the state of something.
– Deposits and withdrawals affect the state of a
bank account.
– Burning fossil fuel affects the environment in
which individuals live.
OOP with Java, David
J. Barnes
Adding Sequential Behavior
4
Explicit and Implicit State
Change
• Mutators names are chosen to show that
they have explicit effects.
– e.g., setField.
• Other methods have implicit effects.
– move implicitly affects a ship’s position.
OOP with Java, David
J. Barnes
Adding Sequential Behavior
5
Arithmetic Expressions
• Expressions involving numerical data types:
– byte , (char), int, long, short.
– double, float.
• Operators for addition, subtraction,
negation, multiplication and division.
• Different results for integer and floating
point types.
OOP with Java, David
J. Barnes
Adding Sequential Behavior
6
The Addition Operator
class PennyBankAccount {
...
// Credit the account with the given amount.
public void credit(long amount){
// Find out current balance.
long currentBalance = getBalance();
// Add in the amount to be credited.
currentBalance = currentBalance+amount;
// Set the attribute from the new value.
setBalance(currentBalance);
}
...
}
OOP with Java, David
J. Barnes
Adding Sequential Behavior
7
Multiplication and Division
• Three operators are: * / %
• Division between integers results in
truncation:
– 5/3 gives a result of 1
• Division between floating point numbers:
– 5.0/3.0 gives a result of 1.6666666666666667
• Modulus (%) gives remainder after division.
– 11%3 gives a result of 2
OOP with Java, David
J. Barnes
Adding Sequential Behavior
8
Order of Precedence
• Rules governing order of evaluation in a
multi-operator expression.
–
–
–
–
Parentheses take precedence over operators.
Multiplication and division.
Addition and subtraction.
Left to right for equal precedence operators.
OOP with Java, David
J. Barnes
Adding Sequential Behavior
9
String Concatenation
• The addition operator has a further use,
when one of its operands is a String.
– Create a new String that is the concatenation of
its operands:
• "big"+"apple" creates "bigapple".
• A non-String operand is converted to a
String.
– "version "+10 creates "version 10".
OOP with Java, David
J. Barnes
Adding Sequential Behavior
10
The Char Type
• In Java, a char occupies 16 bits.
• Java based on the Unicode character set.
• Character literals enclosed between single
quote characters: 'A', '9', '!', etc.
• Char values may be stored in integer
variables, but not vice-versa.
OOP with Java, David
J. Barnes
Adding Sequential Behavior
11
Type Conversion
• Implicit Type Conversion
– Integer to floating point type.
– Less-precise value converted to more-precise
type.
• Primitive Type Checking
– A more-precise value may not be used where a
less-precise type is required:
• E.g., cannot store a double value into an int variable.
OOP with Java, David
J. Barnes
Adding Sequential Behavior
12
Primitive Type Casting
• The normal type checking rules may be
subverted by using a cast:
– A type name written between parentheses:
• int x = (int) 3.14159;
• Casts usually involve loss of information, so
may be risky.
OOP with Java, David
J. Barnes
Adding Sequential Behavior
13
Combined Assignment Operators
• Operation and assignment are common:
– balance = balance+amount;
• Combined assignment operators exist for all
the binary arithmetic operators:
– balance += amount;
– mark *= scalingFactor;
OOP with Java, David
J. Barnes
Adding Sequential Behavior
14
Reading Input Using SimpleInput
• SimpleInput: a class to simplify input.
– Reading Numerical Values:
• nextInt, nextDouble.
– Reading Strings:
• nextLine, nextWord.
– Reading Boolean Values:
• nextBoolean.
– Reading Data from a File.
OOP with Java, David
J. Barnes
Adding Sequential Behavior
15
Review
• Object behavior often involves sequences of
steps.
• Standard arithmetic operations are available.
• Order of precedence must be considered.
• Type compatibility rules must be obeyed.
• A cast permits normal type rules to be
broken.
OOP with Java, David
J. Barnes
Adding Sequential Behavior
16