BallWorlds
-
CSSE 120 - Fundamentals of Software Development 1
-
Rose-Hulman Institute of Technology
-
Computer Science & Software Engineering
Designing and implementing multple classes: inheritance and interfaces
Part 5: Mover
Learning objectives
After completing all the parts of this lab, you should ...
... Be able to
Explain
the following concepts:
-
The role in software development of:
-
specifications
-
design
-
UML class diagrams
-
documented stubs
-
Relationships in UML class diagrams:
-
IS-A (X extends Y)
-
IS-A (X implements Y)
-
HAS-A (X constructs a Y)
-
HAS-A (X refers to Y)
-
What it means to:
-
implement an interface
-
extend a class
-
override a method
-
The meaning of:
-
The keyword
new
-
The keywords
this
and
super
-
The visibility modifiers:
-
A method's signature -- what it tells you
-
Constructors:
-
When and how constructors are called directly
-
When and how constructors are called from a subclass
-
What constructors should do
-
Why interfaces do not have constructors
-
The difference between a class and an instance of a class
-
The following patterns:
-
The counting pattern
count = count + 1
-
The summing pattern
sum = sum + x
-
The toggle pattern
blah = !blah
-
The value of:
-
A UML class diagram
-
An interface
-
The Java API specification
... Be able to
Do
the following:
-
Read a UML class diagram and:
-
Implement a class from its UML class diagram
-
Implement a hierarchy of classes from a UML class diagram
-
Use an IDE (Eclipse) effectively to:
-
Create documented stubs for a class that implements an interface
-
Implement and modify code
-
Carry out functional tests
-
Do unit testing
-
Use objects effectively:
-
Construct new objects
-
Use one object to do work on behalf of another object
-
Apply the counting and summing and toggle patterns accurately
-
Use the Java API specification effectively:
-
To learn about a class that is new to you
-
To apply what you learn
-
Implement a class that implements an interface, including:
-
Determine which stubbed methods need to become actual, do-something implementations
-
Determine what fields are necessary or helpful
-
Implement a class that extends another class, including:
-
Determine which methods a subclass must override
-
Determine what fields are necessary or helpful
-
Write appropriate constructors for subclasses
-
Use the keywords
this
and
super
effectively
Items in bold above
are learning objectives for
this part of the lab.
Instructions:
Pair Programming
-
One partner begins as the "driver", runs Eclipse, and types program code.
-
The other partner observes and types answer into the lab report in Angel.
-
Both partners should be
equal
participants in problem solving.
-
Don't hesitate to ask questions of classmates and share your learning with them.
Lab report
-
When you see a question prefaced by
Question:
blah blah blah ...
put your answer in your
BallWorlds Report
(on Angel) for this part.
Time limit
-
Most students complete this exercise in 60 to 120 minutes.
-
If you find yourself spending much more time than that on this exercise, please see your instructor. (We want to help you avoid getting bogged down.)
Getting Started
What you should learn
-
How to open a lab's report
-
How to use a lab's report
-
Begin to develop the skill of
estimating effort
in software development
-
How to compile and run a program
Resources
The following steps should be familiar for you by now:
-
Opening a lab's report
-
Using a lab's report to reinforce your learning from the exercise
-
Closing open projects
-
Importing an existing project into your Eclipse workspace
-
Importing Eclipse preferences into your Eclipse workspace
-
Compiling and running a program
If not,
-
Look at the Resources of
WordGames, Part 1
for help as needed
-
Make notes to yourself as needed
so that you can do these steps quickly and easily (with your notes) in the future
What you should do
Getting started
-
Skim through this document. Then return to this point.
-
Open your BallWorlds, Part 5
report
(on Angel)
-
Question:
Who is your partner for this report?
-
Question:
About how many minutes do you think it will take you to complete Part 5 of BallWorlds?
-
Comment: You will implement 7 methods plus a constructor and introduce several fields.
-
Question:
What time is it now? (Later we will ask you how long you spent on Part 5 of BallWorlds.)
Reminder: Each time that you see a question
in this document
prefaced by
"Question:
yada yada ..."
put your answer in your BallWorlds report (on Angel)
at that time.
-
For each
Question:, just wait until you reach the question
in this document
before answering it in your report.
-
Open Eclipse. Update your BallWorlds project from your SVN repository (Team ~ Update), or else check out BallWorlds from your pair's SVN repository if you don't have it on your computer yet.
-
Compile and run BallWorlds. You should see no error messages, except that only Duds and DudsThatMove can be created yet by the BallButtons.
Reviewing Dud and DudThatMoves
What you should learn
-
The difference between a
class
and
an instance of a class
-
What it means to
implement an interface
-
How to use one object to do work on behalf of another object
-
The meaning of the keyword
this
-
How to determine what fields are necessary or helpful
Resources
The following concepts should be clear to you now:
-
The difference between a
class
and
an instance of a class
-
What
interfaces
tell you
-
How to use one object to do work on behalf of another object
-
How to determine what fields are necessary or helpful
If not,
-
Ask questions as needed.
-
Make notes to yourself as needed
to solidify your understanding of the above concepts.
-
Continue BallWorlds
since it will reinforce the above concepts.
What you should do
Reviewing ideas from Dud and DudThatMoves
-
Question:
What is the difference between a
class
and an
instance of that class
?
-
Question:
What is the difference between an
interface
and a
class that implements that interface
?
-
Question:
According to BallWorld's UML class diagram, a Dud has to implement the
Animate,
Drawable
and
Relocatable
interfaces? What does that tell you?
-
Question:
Your Dud's constructor containted a statement like this:
ballEnvironment.addBall(this);
Explain that statement, word by word.
-
Question:
What fields did your DudThatMoves have? Why were they necessary?
If any of the above questions are still confusing to you, ask your instructor or an assistant for help NOW
-- you will be unable to complete the rest of this exercise without understanding the answers to the above questions.
Thinking Before Doing
What you should learn
-
The role of
specifications
in software development
-
The role of a
UML class diagram
in software development
-
How to read a
UML class diagram
-
IS-A (X
extends
Y) relationships in UML class diagram
-
IS-A (X
implements
Y) relationships in UML class diagram
-
HAS-A (X
refers to
Y) relationships in UML class diagram
-
The value of
interfaces
in specifying softare
Resources
For a class to implement an interface, the class must supply definitions for each of the methods whose signatures appear in the interface. However, nothing prohibits those methods from being stubs -- while comments in the interface may describe the intended behaviors of the methods of the interface, only the form of those methods is enforced.
A class responds to mouse events
by implementing the
MouseListener
and/or
MouseMotionListener
interfaces.
Reading a UML class diagram
should be familiar territory to you by now.
If not,
What you should do
Understanding Mover from BallWorld's UML class diagram
-
Briefly review the
specifications
of the BallWorlds project.
-
Briefly review the
UML class diagram
for the BallWorlds project.
Note its color-coding:
-
The six
yellow
classes are the six classes that you will eventually implement.
-
Those yellow classes extend the abstract Ball class, which in turn implements the three blue interfaces. Thus,
your classes must implement those three blue interfaces.
-
The Ball class, and hence
each of your classes which extends Ball, have the grey BallEnvironment object.
-
Some of your Ball classes may want to
refer to instances of the green classes.
So the
only
part of the UML class diagram relevant to what you will implement are the colored items listed above.
|
-
Question:
According to the above UML class diagram, what class must a Mover extend?
-
Question:
According to the above UML class diagram, what interfaces must a Mover implement?
-
Question:
Consider the 7 methods that a Mover must implement to satisfy the contracts of the three interfaces that a Mover implements. Of those 7 methods, which two methods must a Mover implement meaningfully (i.e., not merely stubbed) in order to appear on the screen?
-
Question:
Consider the 7 methods that a Mover must implement to satisfy the contracts of the three interfaces that a Mover implements. Of those 7 methods, which method must a Mover implement meaningfully (i.e., not merely stubbed) in order to move?
-
Question:
According to the above UML class diagram, what class implements both
MouseListener
and
MouseMotionListener
(and hence responds to mouse events)?
-
Question:
The World responds to a mouse-press by selecting the Ball nearest the mouse (and showing this by turning that Ball red while the mouse is pressed). Of the 7 Ball methods that must be implemented, which one must be implemented in order for this Ball-selection to work correctly?
-
Question:
The World responds to a mouse-drag by (repeatedly, throughout the drag) telling the selected Ball to move to the current mouse-point. Of the 7 Ball methods that must be implemented, which one must be implemented in order for this Ball-dragging to work correctly?
-
Question:
The World responds to a mouse-left-click (i.e. press and release) by telling the selected Ball to toggle between the paused and not-paused state. (So, if the Ball is acting, it should stop acting; while if it is not acting, it should resume acting.) Of the 7 Ball methods that must be implemented, which one must be implemented in order for this Ball-pause-resume to work correctly?
-
Question:
The World responds to a mouse-right-press by telling the selected Ball to kill itself, that is, to have itself removed from its World. Of the 7 Ball methods that must be implemented, which one must be implemented in order for this Ball-suicide to work correctly?
If any of the above questions are still confusing to you, ask your instructor or an assistant for help NOW
-- you will be unable to complete the rest of this exercise without understanding the answers to the above questions.
Implementing Mover
What you should learn
-
How to implement a class from its UML class diagram
-
How to determine which stubbed methods need to become actual, do-something implementations
-
How to determine what fields, if any, are necessary or helpful
-
How to read a method's
signature
-
How to implement a method
-
How to construct
new
objects
-
How to use the
Java API specification
to learn about a class that is new to you
-
How to apply knowledge gleamed from reading the Java API specification of a class
-
The meaning of the keyword
this
-
How to use one object to do work on behalf of another object
Resources
Iterative Enhancement Plans
When implementing per an
iterative enhancement plan, one implements and tests each stage of the plan before continuing to the next stage of the plan.
Consider While Working
Some questions/issues to consider throughout this exercise are:
-
What fields are necessary?
-
At each stage, what stubs need to become non-stubs?
Reminders from the BallWorlds UML
Some notes/reminders from
BallWorld's UML class diagram
to consider throughout this exercise are:
What you should do
Implementing Mover
As you do this part of the exercise:
-
Keep in mind the above questions and their answers.
-
Look to the Resources above as needed.
-
Do
pair programming
-
Implement by using
documented
stubs
before converting the relevant stubs to actual, do-something implementations
-
You need not do any unit tests (although you are welcome to do so), since the visual nature of BallWorlds lends itself to functional tests (i.e., just run BallWorlds and see if the Balls you implement behaves as desired)
-
Use the following as an
iterative enhancement plan
-- implement and
test
each stage before continuing to the next stage.
-
Ask questions as desired!
-
Think
about how to solve the following problems and
understand
what you do -- don't just copy-and-paste blindly.
-
Keeping in mind the above (especially
think, don't just copy-and-paste
and
test each stage before continuing to the next stage):
Augment your BallWorlds project to include a
Mover
class that
extends Ball
(not DudThatMoves) and
implements Animate, Drawable, Relocatable.
A Mover should be able to:
-
Stage 1:
Appear on the screen, with any reasonable size and color.
-
It should
start in the exact middle of its world (even if its world is resized).
-
Its color should be different from colors of the Ball types that you previously implemented.
-
Stage 2:
Move on its own in a straight line (i.e. at a constant velocity).
-
Each Mover should have its own fixed velocity that is
set at random when the Mover is constructed.
-
The random velocity should be in any reasonable range that includes all directions (not just the "positive" direction).
-
Stage 3:
Be "selectable" by the mouse.
-
To accomplish this, implement the
distanceFrom
method per its specification.
-
Hint: you do NOT need to know any "formula" for distances!
-
Important:
go back to
Dud
and implement
distanceFrom
there also. Otherwise, depending on what stub you used for
distanceFrom
in Dud, a Dud might always be seen as the "closest" ball.
-
Stage 4:
Be "draggable" by the mouse.
-
To accomplish this, implement the
moveTo
method per its specification.
-
Stage 5:
Be "pausable" by the mouse.
-
To accomplish this, implement the
pauseOrResume
method per its specification.
-
Stage 6:
Be "killable" by right-clicks of the mouse.
-
To accomplish this, implement the
die
method per its specification.
|
Wrapping Up
What you should do
Summary
Perhaps the most important ideas that you saw in Part 3 of BallWorlds are:
-
How to implement a class from its UML class diagram
-
How to construct
new
objects
-
How to read the
Java API specification
of a class and apply what you learn
-
The meaning of the keyword
this
-
How to use one object to do work on behalf of another object
-
How to determine what fields are necessary or helpful
-
The difference between a
class
and
an instance of a class
|
-
Question:
How many minutes did it actually take you to complete this part of this lab?
-
Question:
Compute the ratio of the time you ACTUALLY TOOK to complete this part of this lab to the time you ESTIMATED that you would take. Choose the ratio in the report that is closest to your ratio.
-
Commit your changes to your SVN repository, being sure that any new files are added.
-
Submit your report for this part of BallWorlds.