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 4: DudThatMoves
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 30 to 60 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 project's report
-
How to use a project'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 project's report
-
Using a project'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 for
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 4
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 4 of BallWorlds?
-
Comment: You will override about 3 methods and introduce several fields.
-
Question:
What time is it now? (Later we will ask you how long you spent on Part 4 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 can be created yet by the BallButtons.
Reviewing Dud
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
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
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
-
Question:
What happens if you click the Dud button multiple times? Are you creating multiple Dud objects? Do they all show up on the screen? Why or why not?
-
Question:
What is the difference between a
class
and an
instance of that class
?
-
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.
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.
Reviewing the UML Class Diagram
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
Resources
Reading a UML class diagram
should be familiar territory to you by now.
If not,
What you should do
Understanding DudThatMoves 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 Dud extend?
-
Question:
According to the above UML class diagram, what class must a DudThatMoves extend?
-
Question:
According to the above UML class diagram, what three interfaces must a Dud implement?
-
Question:
According to the above UML class diagram, what interfaces, if any, must a DudThatMoves implement?
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
-
What it means to
override
a method
-
How to determine which methods to override when writing a subclass
-
How to determine what fields, if any, are necessary or helpful
-
How to call a superclass' constructor from a subclass' constructor
-
Why to call a superclass' constructor from a subclass' constructor
Resources
To
call a superclass' constructor, use the
super
keyword, as in the following example.
public class Animal {
protected String name;
public Animal() {
this.name = "No-Name animal";
}
public Animal(String name) {
this.name = name;
}
}
|
public class Cat extends Animal {
public Cat() {
super();
}
public Cat(String name) {
super(name);
}
}
|
public class Dog extends Animal {
public Dog() {
// Calls the superclass' constructor implicitly
}
public Dog(String name) {
super();
}
}
|
|
The Cat's constructors call the respective constructors in its Animal superclass.
The Dog's constructors call the no-parameter Animal constructor, either implicitly or explicitly. If the Animal class lacked a no-parameter constructor, the Dog constructors would yield compile-time errors.
What you should do
Thinking about a DudThatMoves
-
Question:
If you wanted a DudThatMoves to behave exactly like a Dud, what Dud methods, if any, would a DudThatMoves need to override?
-
In fact, a DudThatMoves should behave exactly like a Dud with one exception -- a DudThatMoves should move.
The framework calls
act
on each Ball every 20th of a second or so. So, a DudThatMove's
act
method is the place to change the DudThatMove's position.
|
Question:
What data should a DudThatMoves store in fields? List several reasonable choices for how to store that data, that is, list several types that could reasonably be used for that data. What type seems most natural to you?
-
Question:
As explained above, a DudThatMoves must override the Dud's
act
method. Which, if any, of Dud's other methods must a DudThatMoves override?
-
A subclass' constructor must call a constructor of its superclass, either explicitly (as recommended by our code conventions) or implicitly. Consider the following DudThatMoves constructor:
public DudThatMoves(BallEnvironment ballEnvironment) {
}
Question:
Write a statement that, placed in the above DudThatMoves constructor, would call a Dud's constructor and pass it the
ballEnvironment
object that is given to the DudThatMoves' constructor.
Question:
Write a statement that, placed in the above DudThatMoves constructor, would call a Dud's constructor and pass it nothing.
Question:
Why would it be a mistake in your project to call a Dud's constructor and pass it nothing?
-
Question:
What methods, if any, must a DudThatMoves have beyond those that override a Dud method?
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 DudThatMoves
What you should learn
-
How to
override
a method
-
How to write constructors in a subclass
-
How to construct
new
objects
-
The meaning of the keyword
this
-
The meaning of the keyword
super
-
How to apply knowledge gleamed from reading the Java API specification of a class
-
How to use one object to do work on behalf of another object
-
How to determine what fields are necessary
-
How to determine what methods to override
Resources
Questions to Consider
Some questions/issues to consider throughout this exercise are:
-
What fields are necessary?
-
What Dud methods does DudThatMoves need to override?
Notes and Reminders
Some notes/reminders from
BallWorld's UML class diagram
to consider throughout this exercise are:
Overriding Methods
To
override a method, simply implement the method in the subclass (here, DudThatMoves).
-
For example, to override the Dud's
act
method, just put an implementation of
act
in DudThatMoves.
-
Most of the methods should
not
appear in DudThatMoves. Instead, they should be
inherited
from Dud.
-
That is the power of
extending
a class.
There are two technical notes for overriding methods.
-
The first line of your constructor should call the superclass' constructor, for example:
super(ballEnvironment);
-
Fields need to be declared to be
protected
(not
private) to be referenced in subclasses.
What you should do
Implementing a DudThatMoves
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)
-
Ask questions as desired!
-
Keeping in mind the above:
Augment your BallWorlds project to include a
DudThatMoves
class that extends Dud.
A DudThatMoves should behave just like a Dud, except that it moves on its own in a straight line (i.e., a constant velocity).
-
It can move at any reasonable speed you choose -- try numbers like 0.1 for starters.
-
It can start wherever you wish, with any reasonable size and color.
-
Its color should be different from colors of the Ball types that you previously implemented.
|
Wrapping Up
What you should do
Summary
Perhaps the most important ideas that you saw in Part 4 of BallWorlds are:
-
How to
extend a class
-
How to
override a method
-
The meaning of the keyword
super
-
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.