CSCI 62Data Structures
Dr. Joshua StoughSeptember 9, 2008
Today
• Object-oriented design• Interface• Assert• Generics • Program 1.
OO
• Data Abstraction and Encapsulation– cars, toasters, lightbulbs, Strings...– interface – important to the user– implementation – details hidden from user– A fixed interface allows alternative implementations.
• Maximize efficiency, minimize errors– Hitchhiker’s Guide
• Concerning the development of fire:The girl in charge of fire development said
“When you’ve been in marketing as long as I have you’ll know that before any new product can be developed it has to be properly researched. We’ve got to find out what people want from fire, how they relate to it, what sort of image it has for them.
“Stick it up your nose,” Ford said.“Which is precisely the sort of thing we need
to know. Do people want fire that can be fitted nasally?”
• Concerning developing the wheel:“We’re having a little difficulty there.”“Difficulty?” exclaimed Ford. “Difficulty?
What do you mean, difficulty? It’s the single simplest machine in the entire Universe!”
The marketing girl soured him with a look.“All right, Mr. Wiseguy,” she said, “you’re so
clever, you tell us what color it should be.”
OO• Object Model
– class – defines objects of the class type.– object – instance of a class.– Object – all non-primitive objects extend Object.
• implements toString, equals– An object manages it own data
• constructor, accessor and mutator methods comprise the interface.
• Example: cannot change the key/account in a(n) Association/BankAccount
– package – collection of related classes (ideally).
• We’ve seen – Ratio – BankAccount– TextCoinStrip (soon)
OO
• public – everyone has access, • protected – children (subclasses),
via extends, have access, • private – only the instance has
access.• Overloading – same name,
different parameter list• Overriding – same
name/parameter list, child class.
OO – WordList Example
• No implementation details needed to use the WordList
• A way to think about it:– Write/think about the pseudocode
that uses the object BEFORE implementing the object.
– HangMan before WordList – Can be iterative – you start coding in
more detail and determine additional functionality.
Interfaces
• public interface ClassName• A class can implement an interface
– The class must implement all of the interface’s methods
Assert
• Firecrackers in the code• Defined in the structure5 package
(bailey.jar).• void pre(boolean test, String message)
• Can be placed in any method
Generics
• Recognize errors sooner rather later.• generic – class parameterized by the
type of data.• Java tutorial, generics example.
• Vector<Double>, or Vector<Boolean>
Class def.: public class Association<K,V>Instantiation: Association<String,Integer> personAttribute =
new Assocation<String,Integer>("Age",34); autoboxed
type parameters
Program 1
• TextCoinStrip– How did you do it?
• GraphicsCoinStrip– Make functional
• Instantiate the strip, place a coin• Comment out the void method bodies
– Add some functionality• mousePressed should carry the coin• mouseReleased should put the coin back.
Top Related