Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOP with Java, David J. Barnes Adding Sequential Behavior1 Adding Behavior Previous class definitions have passively maintained attributes. Active behavior.

Similar presentations


Presentation on theme: "OOP with Java, David J. Barnes Adding Sequential Behavior1 Adding Behavior Previous class definitions have passively maintained attributes. Active behavior."— Presentation transcript:

1 OOP with Java, David J. Barnes Adding Sequential Behavior1 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.

2 OOP with Java, David J. Barnes Adding Sequential Behavior2 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.

3 OOP with Java, David J. Barnes Adding Sequential Behavior3 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.

4 OOP with Java, David J. Barnes Adding Sequential Behavior4 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.

5 OOP with Java, David J. Barnes Adding Sequential Behavior5 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.

6 OOP with Java, David J. Barnes Adding Sequential Behavior6 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.

7 OOP with Java, David J. Barnes Adding Sequential Behavior7 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); }... }

8 OOP with Java, David J. Barnes Adding Sequential Behavior8 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

9 OOP with Java, David J. Barnes Adding Sequential Behavior9 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.

10 OOP with Java, David J. Barnes Adding Sequential Behavior10 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".

11 OOP with Java, David J. Barnes Adding Sequential Behavior11 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.

12 OOP with Java, David J. Barnes Adding Sequential Behavior12 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.

13 OOP with Java, David J. Barnes Adding Sequential Behavior13 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.

14 OOP with Java, David J. Barnes Adding Sequential Behavior14 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;

15 OOP with Java, David J. Barnes Adding Sequential Behavior15 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.

16 OOP with Java, David J. Barnes Adding Sequential Behavior16 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.


Download ppt "OOP with Java, David J. Barnes Adding Sequential Behavior1 Adding Behavior Previous class definitions have passively maintained attributes. Active behavior."

Similar presentations


Ads by Google