Object Oriented Analysis & Design
Lab # 10
The objectives of lab 10 are to:
Understand and implement State Machine diagrams.
State:
A state represents a discrete, continuous segment of time wherein the object’s behavior will be stable
- The object will stay in a state until it is stimulated to change by an event
State Machine Diagram:
State diagrams are used to specify the sequencing / timing behavior of objects in a class
- States - Events - Transitions
Basic components of a statechart diagram –
1. Initial state – We use a black filled circle represent the initial state of a System or a class.
2. Transition – We use a solid arrow to represent the transition or change of control from one
state to another. The arrow is labelled with the event which causes the change in state.
3. State – We use a rounded rectangle to represent a state. A state represents the conditions or
circumstances of an object of a class at an instant of time.
4. Fork – We use a rounded solid rectangular bar to represent a Fork notation with incomig
arrow from the parent state and outgoing arrows towards the newly created states. We use the
fork notation to represent a state splitting into two or more concurrent states.
5. Join – We use a rounded solid rectangular bar to represent a Join notation with incoming
arrows from the joining states and outgoing arrow towards the common goal state. We use the
join notation when two or more states concurrently converge into one on the occurrence of an
event or events.
Figure – a diagram using the fork notation Figure – a diagram using join notation
6. Self transition – We use a solid arrow pointing back to the state itself to represent a self
transition. There might be scenarios when the state of the object does not change upon the
occurrence of an event. We use self transitions to represent such cases.
7. Composite state – We use a rounded rectangle to represent a composite state also.We
represent a state with internal activities using a composite state.
8. Final state – We use a filled circle within a circle notation to represent the final state in a state
machine diagram.
Steps to draw a state diagram –
1. Identify the initial state and the final terminating states.
2. Identify the possible states in which the object can exist (boundary values corresponding to
different attributes guide us in identifying different states).
3. Label the events which trigger these transitions.
Example – state diagram for an online order –
Example – state diagram for media player –
Exercise
Task 1: Suppose that your TA only has two states of being: happy and sad. Further suppose that
your TA is basically cheerful and starts out happy. Also suppose that your TA, not having much of a
life, can only respond to two events: getting coffee (which makes the TA happy) and getting punched
in the nose (which, needless to say, makes her sad). Draw a state diagram to show the states of TA.
Task 2: Draw an state machine diagram for the following scenario:
Librarians categorise the library books into loanable and non-loanable books. The non-loanable
books are the reference books. However, the loanable books are the non-reference books. After
cataloguing the books, the books are available for loan. Students who borrow the library books
should return them back before the due date. Books that are 12 months over the due date would
be considered as a lost state. However, if those books are found in the future, they must be
returned back to the library. When the books are found not required in the library or have been
damaged, the book would be disposed.
Task 3: Draw the state machine diagram of course object. When a course is initialized it is in
Enrollment state. From the Enrollment state the course can be cancelled (and go to final state)
or the term is begin and course is in Beingtaught state. From the BeingTaught state, a student
can drop a class. If the size of class remains >0 the course remains in BeigTaught state;
otherwise the course is cancelled and goes to final state. When classes end, the course goes to
the FinalExam state. After final exam state the course is closed and goes to final state. the
receipt.