Outline of this exercise • Learning objectives of the tutorial: – After this exercise, a student should be able to: • • • • Read and create UML class diagrams Describe the basic process for developing a UML class diagram Explain what is easy about the process and what is hard Explain the iterative nature of the design process • Process: – Review the specification of BallWorld – Develop a UML class diagram for BallWord • Together (in groups), step by step • Review the sample solution, and invent your own version of the model before our next class (to turn in) – use handout as a template (see the last slide in this presentation ) Fundamentals of Software Development 1 Slide 1 Specification of BallWorld • BallWorld is just that -- a world that contains various kinds of balls – along with hotspots and perhaps other things eventually • Each of the buttons represents a Java class that you will write – Pressing a button creates a ball of that type • Types of Balls might include: – Dud: does nothing – DudThatMoves: just moves in a constant, random velocity – Bouncer: like a Mover, but bounces off the walls of – Dragger: the user can drag it the World around and kill (delete) it – Shrinker: A Bouncer that grows and shrinks in size – Mover: like a Dragger, but – Exploder: Grows, then explodes and replicates itself! moves like a MovingDud Questions? You cannot design software Fundamentals of Software Development 1 Slide 2 unless you know what it is supposed to do! Design BallWorld, by drawing a UML class diagram for it Review the specification, then: 1. Identify kinds of objects – these become classes 2. Identify relationships between classes • • is-a (extends), is-a (implements), has-a Identify interfaces that specify what is needed for the classes to relate 3. For each class, determine its attributes and 4. For each class, for each attribute/operation, determine its type operations Fundamentals of Software Development 1 Slide 3 List the classes in BallWorld • List the kinds of things – classes – that you think belong in the BallWorld design: – Some are visual – Some are internal – Some are relevant Java library classes like JFrame Fundamentals of Software Development 1 • When done, compare your list with other lists around the room Slide 4 The classes I chose – without thinking about Java • Visual things: – BallWorldFrame • Where everything gets displayed – BallButton • A single class, with several instances with different labels • Relevant Java library classes: – ? Fundamentals of Software Development 1 • Non-visual things: – BallWorld • The class that contains main and starts the program. (Ok, I thought about Java after all!) – Ball – Various subclasses of Ball – HotSpot Slide 5 The classes I chose – knowing Java • Visual things: – – – – BallWorldFrame WorldPanel ButtonsPanel BallButton • A single class, with several instances with different labels • Relevant Java library classes: – JFrame – JPanel – JButton Question: Why not just let the Fundamentals of Software WorldPanel Development 1play this role? • Non-visual things: – BallWorld • The class that contains main – – – – Ball Various subclasses of Ball HotSpot World Answer: I choose to separate function from display of that function Slide 6 • The class that manages all the Balls and HotSpots Where we are in the process… Design BallWorld, by drawing a UML class diagram for it We just did our first Review the specification, then: iteration of Step 1 1. Identify kinds of objects – these become classes 2. Identify relationships between classes • • is-a (extends), is-a (implements), has-a Identify interfaces that specify what is needed for the classes to relate 3. For each class, determine its attributes and 4. For each class, for each attribute/operation, determine its type operations Fundamentals of Software Development 1 Now we’ll do our first iteration of Step 2, beginning with the is-a (extends) relationship Slide 7 Likely Is-a relationships JButton BallButton JFrame ButtonsPanel JPanel BallWorldFrame Draw the is-a relationships between the classes shown BallWorld WorldPanel Notation: A B A is-a B, that is, A extends B Ball HotSpot World Please draw along on your handout, and ask questions! (Use pencil.) Fundamentals of Software Development 1 This means that A inherits all the attributes (data) and operations (behaviors) of B Questions on is-a so far? Slide 8 More Is-a relationships • Types of Balls might include: – Dud: does nothing – MovingDud: just moves in a constant, random velocity – Dragger: the user can drag it around and kill (delete) it – Mover: like a Dragger, but moves like a MovingDud Ball – Bouncer: like a Mover, but bounces off the walls of the World – Shrinker: A Bouncer that grows and shrinks in size – Exploder: Grows, then explodes and replicates itself! Draw possible is-a relationships between the types of Balls Mover Notation: Dud Dragger MovingDud Fundamentals of Software Development 1 Bouncer Shrinker Exploder ... A B A is-a B, that is, A extends B This means that A inherits all the attributes (data) and operations (behaviors) of B Questions on is-a relationships? Slide 9 Likely Has-a relationships JButton BallButton JFrame ButtonsPanel JPanel BallWorldFrame Draw possible has-a relationships in which objects are created, for the classes shown BallWorlds WorldPanel Notation: A B A has-a B, here, A creates a B Ball HotSpot Indicate the multiplicity on each end: World I also use color (here, lime green, but more often I use red) to indicate has-a relationships that involve object creation. This is not standard UML. Fundamentals of Software Development 1 1 .. * (one or more) 3 (exactly 3) (blank often means 1) Slide 10 Has-a JButton relationships BallButton 7..* JFrame ButtonsPanel JPanel BallWorldFrame Notation: A B A has-a B, here, A creates a B BallWorlds WorldPanel Ball HotSpot 1..* World Indicate the multiplicity on each end: 1 .. * (one or more), blank for exactly 1 Part of an answer: I envision a single frame, but possibly many Worlds and associated Panels. Do you see why that leads to my choices? I chose to have the BallWorlds create each World, which creates the two panels (WorldPanel and ButtonPanel). Why? Who else might I have chosen to create the At this point, we have a problem – what remains Fundamentals of Software World? The Panels? Development 1 to be created? Who should create them?Slide 11 Has-a JButton relationships BallButton 7..* JFrame ButtonsPanel JPanel BallWorldFrame Notation: A B A has-a B, here, A creates a B BallWorld WorldPanel Ball We have yet to create the Balls and HotSpots? Who should create the HotSpots? Fundamentals of Software Development 1 HotSpot 0..* 1..* Indicate the multiplicity on each end: 1 .. * (one or more), blank for exactly 1 World Answer: the World seems a natural choice. Who creates a new Ball, e.g., a new Exploder? How? Answer: the human user, by pressing a BallButton. Slide 12 Has-a JButton relationships BallButton 7..* JFrame ButtonsPanel JPanel BallWorldFrame Notation: A B A has-a B, here, A creates a B BallWorld WorldPanel Ball HotSpot 0..* 1..* Indicate the multiplicity on each end: 1 .. * (one or more), blank for exactly 1 World So the human user creates each Ball (e.g. an Exploder), by pressing a BallButton. When the human presses a BallButton, a Ball of the appropriate type must be: Constructed Added to the World Drawn In the current design, the latter two are impossible. Explain. Fundamentals of Software Development 1 Slide 13 Has-a JButton relationships BallButton 7..* JFrame ButtonsPanel JPanel BallWorldFrame Notation: A B A has-a B, here, A creates a B BallWorld WorldPanel Ball HotSpot 0..* 1..* Indicate the multiplicity on each end: 1 .. * (one or more), blank for exactly 1 World So the human user creates each Ball (e.g. an Exploder), by pressing a BallButton. When the human presses a BallButton, a Ball of the appropriate type must be: Constructed Added to the World Drawn In the current design, the latter two are impossible. Draw the has-a relationships that are needed – who needs to know whom? These are has-a relationships, but do NOT involve Fundamentals of Software Development Slide 14 creation of 1objects. How are these relationships going to be implemented? is-a has-a JButton Relationships JFrame 1..* BallButton 1..* ButtonsPanel JPanel BallWorldFrame 1..* Draw links to enable the construct/add/draw actions. Revise your answers to the “who-should” questions as needed, as you design. Dud Fundamentals of Software Development 1 • Constructed WorldPanel BallWorld 1..* Bouncer When a new Ball is created, it must be: World Ball Bloater • Added to the World • Drawn Who should construct the Ball? Add it to the World? Draw it? Exploder ... Slide 15 is-a has-a JButton Relationships JFrame BallButton 1..* ButtonsPanel 1..* JPanel BallWorldFrame 1..* WorldPanel BallWorld 1..* BallButton: construct the new Ball. No links needed. (Why?) BallButton: tell the World to add the Ball. What does that imply is needed in the diagram? Dud Fundamentals of Software Development 1 Bouncer World Draw links to enable the construct/add/draw actions. Revise your answers to the “who-should” questions as needed, as you design. Here are my decisions, as I made them. Ball Bloater Exploder ... Slide 16 is-a has-a JButton Relationships JFrame 1..* BallButton 1..* ButtonsPanel JPanel BallWorldFrame 1..* BallWorld • BallButton must have the World WorldPanel 1..* BallButton: construct the new Ball. No links needed. How does the BallButton obtain the World? BallButton: tell the World to add the Ball. Hence: World • World must be able to add a Ball What has-a relationships does that imply need to be added, for the BallButton to have the World? Ball Answer: BallWorldFrame creates the World,Bloater then sends it to the ButtonsPanel, Dud Bouncer Exploder . . . who Fundamentals of Software sends it to each BallButton. Do you see why it was a good decision for Development 1 Slide 17 BallWorldFrame to create the 3 big things? is-a has-a JButton Relationships JFrame 1..* BallButton 1..* ButtonsPanel JPanel BallWorldFrame 1..* BallWorld • BallButton must have the World WorldPanel 1..* BallButton: construct the new Ball. No links needed. How does the BallButton obtain the World? BallButton: tell the World to add the Ball. Hence: World • World must be able to add a Ball What has-a relationships does that imply need to be added, for the BallButton to have the World? Ball Answer: BallWorldFrame creates the World,Bloater then sends it to the ButtonsPanel, Dud Bouncer Exploder . . . who Fundamentals of Software sends it to each BallButton. Do you see why it was a good decision for Development 1 Slide 18 BallWorldFrame to create the 3 big things? is-a has-a JButton Relationships JFrame 1..* BallButton 1..* ButtonsPanel JPanel BallWorldFrame 1..* WorldPanel BallWorld When a new Ball is created, it must be: 1..* Who creates a new Ball, e.g., a new Exploder? How? Answer: the human user, by pressing a BallButton. Dud Fundamentals of Software Development 1 The World is intended to hold all of the balls. What has-a does that imply? Draw it. Bouncer World 1..* Ball Bloater • Constructed • Added to the World • Drawn In the current design, some ofExploder this is impossible. ... Explain. Slide 19 is-a has-a JButton Relationships JFrame 1..* BallButton 1..* ButtonsPanel JPanel BallWorldFrame Drawable interface 1..* BallWorld Animatable interface WorldPanel WorldManagement interface 1..* World 1..* Ball Dud Fundamentals of Software Development 1 Bouncer What about interfaces? – This is a chance to give the design strategic consistency by Bloater defining Exploder .. up front how .the 20 interacting entities shouldSlide talk! More questions • Attributes: What does a Ball have? • When the human user clicks a BallButton, should the World become aware of the constructed Ball? If so, how? • How is drawing done? – WorldPanel gives info to Ball, or Ball gives info to WorldPanel? • How do Balls “act”? What else should the human user be able to do to Balls? How is that accomplished? • Who should act independently (i.e., what threads should exist)? • What interfaces would be useful in the above? In particular, what should the following be able to do: – Ball Fundamentals of Software Development 1 HotSpot ManagerOfBalls ManagerOfHotSpots Homework – Finish this tutorial and your drawing. Put your name on it – and on the back explain what you did differently! We’ll discuss next class… Slide 21
© Copyright 2026 Paperzz