More Object-Oriented Programming Details
Construct Account class
- [Draw UML diagram]
- attributes: user (string), pinCode, failedAttempts
- methods: constructor w/ user, pin;
getUser, isAuthenticated(user, pin), resetFailures,
isAcceptableNewPin, setPin
- Implement constructor, getUser
- implement reset failures to zero
- Implement isAuthenticated:
- return false if too many failures
- return true if match user and pin code, reseting # failures
- increment number of failures otherwise
- What would happen if increment number of failures when above max?
- Implement isAcceptableNewPin
- Ensure not same pin and new pin is large enough
- Convert pin to string of characters, loop through each checking
if character matches any remaining ones
- Implement setPin
- Call isAcceptableNewPin
- If not acceptable, print error and do System.exit
- Discuss AccountTest.java
- Discuss javadoc and Account.java
- Consider encapsulation
- what if opponent gets a memory dump?
- Could encode pinCode, even user
- Simple encoding: negate and add 53
- Observe value in trace
- Show passes tests
Today's Goals
- Define and use class constants
- Understand the importance of information hiding and encapsulation
- Define and use methods that have reference data types as arguments -
String values
- Define and use overloaded methods - add method to check just PIN
- Call methods of the same class - isAcceptableNewPin
- Draw and explain memory diagrams that illustrate the instantiation of
objects
- Describe the role of the garbage collector - what happens if set
username to a gigabyte of data and then change it?
- Compare the equality of two different objects - add method to compare
usernames
- Swap the data in two different objects - add method swapPinCodes
- Avoid redundant code by calling one constructor from a different
constructor - add constructor with just username, default pinCode to 0
Exam 2 Review
- Java API
- Math.sqrt, Math.pow, Math.sin/cos/tan,
Math.abs, Math.PI
- Additional types: double, float, int, long, short; approx ranges
- Wrappers for primitive types: Integer, Float, Double, etc.
- Double.toString
- Double.parseDouble, etc.
- String: length, substring, charAt
- Loops: while, for, do
- more on if, switch
- steps to write a loop
- !(x && y) equivalent to !x || !y
- classes, UML diagram for a class
- attribute, method
- public vs. private
- accessor, mutator methods
- conventions for method names