4 - Takoma Software

Download Report

Transcript 4 - Takoma Software

Modular Programming
and
UML Class and Object Diagrams
Session 4
LBSC 790 / INFM 718B
Building the Human-Computer
Interface
Agenda
• Modular Programming
– Homework: Java
• UML Class and Object Diagrams
– Homework: Use Cases
• Idea Rally
Sources of Complexity
• Java syntax
– Learn to read past syntax to see ideas
– Copy working examples
• Interaction of data and control structures
– Structured programming
• Object structure
– Practice modeling using UML
• Modularity
– Our focus this week
Modularity? Who cares?
• Limit complexity
– Extent
– Interaction
– Abstraction
• Minimize duplication
What can go wrong here?
?
?
Defensive Programming
• Goal of software: create desired output
• Programs transform input into output
– Some inputs may yield undesired output
• Methods should enforce input assumptions
– Guards against the user and the programmer!
• Everything should be done inside methods
Passing Arguments in Java
• Call by value
– Primitive data types
– Object references
• The two meanings of “equal”
• Overloaded method names
– Depends on parameter type, number, and
order
Returning Values in Java
• Return type declaration
– No return statement for “void”
• Return statement causes termination
– Multiple return statements can be present
• First one executed causes termination
– Type must match declaration
– Objects encapsulate complex return types
Uses of Methods in Java
• Constructor
• Set and get
• Helper
• Event handler
– Stay tuned until we get to GUI’s
Classes to Explore in the API
• String
– Manipulate strings (e.g., to extract substrings)
• StringTokenizer
– Pick apart strings (e.g., into words)
• GregorianCalendar
– Dates and times
• Hashtable
– Like arrays, but with any object as the index
Exercise
• Read the Book class
• Create the Library class
• Test the two together
Things To Pay Attention To
Syntax
Modular Programming
•
•
•
•
•
•
•
•
•
•
•
How layout helps reading
How variables are named
How strings are used
How output is created
How applications are invoked
Modularity of methods
How methods are invoked
How arguments work
How scope is managed
How errors are handled
How results are passed
Structured Programming
• How arrays are used
• How boolean flags are used
Object Structure
• How classes are defined
Getting to the Object Structure
• Capturing the big picture
• Designing the object structure
– Class diagram (“entity-relationship” diagram)
– Object diagram (used to show examples)
• Represent a candidate workflow
• Represent object interactions for a scenario
• Represent event-object interactions
Object Modeling
• Models are abstractions of reality
– That are created for a specific purpose
• Classes represent types of entities
– That are important for the chosen purpose
– Specify subordinate objects and methods
• Objects are specific instances of
classes
– Encapsulate specific objects and methods
Bill’s Budget Flight Finder
Flight Finder Background
• The traveler may specify the origin, destination, type
of flight (e.g. one-way, round-trip) exact or
approximate dates, and then initiate the search. Once
the search completes, the traveler may step through
the interesting routes one at a time, viewing
information on which their decision might be based.
Once one or more acceptable routes have been
identified, the traveler can then select and book the
flight, by providing personal and payment information.
FlightFinder Use Case
Diagram
Search
AA/
Sabre
Process
Sched
Select
Traveler
Book
Search Use Case Narrative
• Assumptions
– Current AA/Sabre schedule available
• Pre-conditions
– None
• Initiation
– Search function selected
• Dialog
– Search parameters selected, search initiated,
unknown locations resolved, route list displayed
• Termination
– Route search complete (normal), unknown location
(error)
• Post-conditions
– Route list displayed (if available) or blank
Select Use Case Narrative
• Assumptions
– Route list displayed
• Pre-conditions
– At least one available route
• Initiation
– Route selected from list
• Dialog
– Select route, display details in map+timeline+text
• Termination
– Display completed
• Post-conditions
– Details displayed
Flight Finder Background
• The traveler may specify the origin, destination, type
of flight (e.g. one-way, round-trip) exact or
approximate dates, and then initiate the search. Once
the search completes, the traveler may step through
the interesting routes one at a time, viewing
information on which their decision might be based.
Once one or more acceptable routes have been
identified, the traveler can then select and book the
flight, by providing personal and payment information.
Bill’s Budget Flight Finder
FlightFinder Class Diagram (1)
Route
*
*
1..9
Sequence of
1
2
GUI
Airfield
Displayed in
1
Map
Displayed in
1
Flies between
1
1
Timeline
1
Sequence of
*
Displayed in
0..1
1..*
Leg
Travel
between
2
Flight
*
*
Contains
1
Schedule
Uses
Location
AircraftType
1
Relationships
• Object relationships
– Has-a
– Aggregation
– Composition
• Class relationships
– Generalization (“extends”)
– “Implements”
FlightFinder Class Diagram (2)
Displayed
in
+addLeg(l:Leg)
+summary():String
GUI
Displayed in
+search(p:Param)
+display(r:Route)
0..1
Airfield
1
-name:String
-icaoIdentifier:String{4}
1
Map
+draw(r:Route)
1
Timeline
+draw(r:Route)
1
Displayed in
1
-seats: integer
-departureTime: Time
-arrivalTime:Time
+parse(s:String)
+summary():String
1
Sequence of
*
2
*
Flight
-flightNumber:String
*
+parse(s:String)
Schedule
1
-updated:Time
-source:URL
+parse(f:File)
Travel
between
2
1..*
Uses
*
Leg
Contains
*
1..9
Sequence of
Flies between
Route
Location
-latitude:double
-longitude:double
-timeZone:TimeZone
+distanceTo(l:Location)
AircraftType
-name:String
-speed:int
+flightTime(o:Location,
d:Location)
1
FlightFinder Object Diagram
1st
1:Route
2nd
:GUI
:Leg
NV514:Flight
-seats=34
-departureTime=1600Z
-arrivalTime=1900Z
-flightNumber:NV514
Lambert:Airfield
:Leg
AF302:Flight
-name=Lambert Field
-icaoIdentifier=KSTL
-seats=3
-departureTime=1200Z
-arrivalTime:1400Z
-flightNumber:AF302
O
LAX:Airfield
-name=LA Intl Airport
-icaoIdentifier=KBLV
:Map
Andrews:Airfield
D
-name=Andrews AFB
-icaoIdentifier=KADW
Sep26:Schedule
O
O
:Timeline
DC:Location
Scott:Airfield
-latitude:38-48N
-longitude:076-51W
-timeZone=EASTERN
-name=Scott AFB
-icaoIdentifier=KBLV
-updated:Sep 26/1200Z
-source:http://www…
D
C-9B:AircraftType
-name=C-9B
-speed=450 knots
LA:Location
D
-latitude=33-57N
-longitude=118-24W
-timeZone=PACIFIC
Key: O = Origin
D = Destination
Class Critique
• Multiple instances must be possible
– Each uniquely identifiable
•
•
•
•
Can’t be subdivided into distinct classes
Multiple attributes needed to describe
No empty attribute values
Methods needed
– At least for creation and modification
Attribute Critique
• Naturally describes the object
– Does not describe some other object
• Value will be known for some instances
• Value will never be empty
• Cannot be subdivided into smaller
attributes
• One value, not a list of values
• Cannot be computed from other
attributes
Practice Exercise
• Create a Class Diagram for an academic transcript
– Include a method to compute grade point average
– Include a method to print the transcript
• Code (only) the transcript class in java
– Include a main method that tests the class
– Create stubs for any other needed classes
• Run your main method
Idea Rally
• Pitches rigidly timed (90 seconds)
• Focus on the points that you believe
will most impress your classmates
• Not graded
• Discussion afterwards
Show and Tell
• What cool resources have you found?
– Books and Web sites?
• What new Java tricks have you learned?
• What do you need to know to make
progress?
Coming up
• Exercise 3 (self-graded)
– Will post by Friday evening
– Not Graded
• Next week:
– Object-oriented programming
• Read: HFJ 7,8, and 9
– Collaboration and sequence diagrams
• Read: UML, sessions 16-19
Muddiest Point(s)
• The most confusing thing discussed
– Modular programming & Java
– UML
• One sentence each