BallWorlds
-
CSSE 120 - Fundamentals of Software Development 1
-
Rose-Hulman Institute of Technology
-
Computer Science & Software Engineering
Designing and implementing multiple classes: inheritance and interfaces
Part 3: Dud
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 and why to close open projects
-
How to import a project into your Eclipse workspace
-
How to compile and run a program
What you should do
Getting started
-
Skim through this document. Then return to this point.
-
Open your BallWorlds, Part 3
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 3 of BallWorlds?
-
Comment: You will implement 7 methods (of which 5 remain as stubs) and 1 constructor.
-
Question:
What time is it now? (Later we will ask you how long you spent on Part 3 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.
-
Close all your open projects, so that you can focus on the project that you are about to import.
-
Inside Eclipse:
Switch to the
SVN Repository Exploring
perspective.
-
Right-click in the SVN Repository view and select
New ~ Repository Location...
-
In the
Add SVN Repository
dialog box, enter:
-
URL:
http://svn.cs.rose-hulman.edu/repos/user1-user2
, where user1
and user2
are your two usernames, as your instructor told you..
Note, this may be a new repository for this lab, because you probably have a new partner.
-
Enter the username and password that your instructor gave you (not
your Kerberos password).
-
Eclipse should bring up this location in the SVN Repository view. If not,
get help now.
-
Right-click on BallWorlds and select
Check Out As Project
.
-
To work on the project, go back to the Java perspective (choose
Window ~ Open perspective ~ Java).
-
Compile and run BallWorlds. You should see no error messages, except that no Balls can be created yet by the BallButtons.
Studying the UML
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 Dud 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 three interfaces must a Dud implement?
-
Question:
According to the above UML class diagram, how many methods must you put in any class that implements the
Animate
interface? What are the names of those methods?
-
Question:
According to the above UML class diagram, how many methods must you put in any class that implements the
Drawable
interface? What are the names of those methods?
-
Question:
According to the above UML class diagram, how many methods must you put in any class that implements the
Relocatable
interface? What are the names of those methods?
-
A note in BallWorld's UML class diagram indicates that every Ball must have a constructor that:
-
Takes a
BallEnvironment
object
-
Adds the Ball to its World
Question:
Looking at the above UML class diagram, what is the only object that a Ball has? What method does that object have that will be helpful to a Ball?
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
-
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 construct
new
objects
-
How to use the
Java API specification
to learn about a class that is new to you
-
Why interfaces do not have constructors
Resources
Reading a method's signature
should be familiar territory to you by now, but here is an example as a reminder. Consider the following signatures of methods in the Component class:
Component getComponentAt(int x, int y)
boolean contains(Point p)
The first signature says that the
getComponentAt
method takes two
int
variables and returns a
Component.
The second signature says that the
contains
method takes a
Point
object and returns a
boolean
value (i.e., either
true
or
false).
Make notes to yourself as needed
about any of the above that is unfamiliar to you.
Constructing a new object
should be familiar territory to you by now, but here is an example as a reminder. Consider the following two expressions:
new JButton("Eat me")
new Point2D.Double(45.3, 22.2)
The first expression creates a new
JButton
whose label is the given String.
The second expression creates a new
Point2D.Double
object whose x and y coordinates are the numbers given.
Make notes to yourself as needed
about any of the above that is unfamiliar to you.
You should be familiar with how to
look up a class in the Java API specification.
If not,
-
Ask someone to show you how to do so
(or see the notes in
JavaEyes, Part 4).
-
Make notes to yourself as needed
so that you can look up other classes in the Java API easily.
What you should do
Thinking about a Dud
Now that you know
what
methods you must implement, let's investigate
how
you should implement them.
-
Question:
A Dud is a Ball that the system draws. That's it -- that is all a Dud does. With that in mind: Of the 7 methods that you must implement in the Dud class, which can remain do-nothing stubs and which must have an actual, do-something implementation?
-
Question:
It might make sense to have some fields in the Dud class. However, are fields absoutely necessary for the Dud class? That is, could you implement a Dud that has
no
fields?
-
Question:
The
getColor
method returns what type of object?
-
Question:
Using the following fact:
The
Color
class has a constructor that takes three integers -- the amount of red, green and blue in the constructed Color. Each integer should be an integer between 0 and 255.
write an expression that would construct a new Color object that has some red and green but no blue.
-
Question:
The
getShape
method returns what type of object?
-
Question:
Look up the Shape class in the Java API specification. Is Shape a class or an interface? Explain why the following does not make sense (and will NOT construct a Shape object):
new Shape()
-
Question:
The Java API specification for the Shape class lists many implementing classes for Shape. Find one that looks promising for a ball's (round) shape and write an expression that constructs a new object of that Shape.
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 Dud
What you should learn
-
How to implement a class from its UML class diagram
-
How to implement a method
-
How to construct
new
objects
-
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
Some questions/issues to consider throughout this exercise are:
-
What fields are necessary?
-
What stubs need to become non-stubs?
Some notes/reminders from
BallWorld's UML class diagram
to consider throughout this exercise are:
-
Every Ball subclass must have a constructor that takes a
BallEnvironment
object.
-
A Ball will not be displayed until both:
-
It is added to its World
-
It implements the
Drawable
interface meaningfully
What you should do
Implementing a Dud
As you do this part of the exercise:
-
Keep in mind the above questions and their answers.
-
Look to the Resources to 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
Dud
class that extends Ball and implements Animate, Drawable, Relocatable.
A Dud should do nothing except appear in the World.
-
It can appear wherever you wish, with any reasonable size and color.
|
Wrapping Up
What you should do
Summary
Perhaps the most important ideas that you saw in this part 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
|
-
Commit your changes back to your SVN repository. Be sure to add Dud.java to the repository (by checking the correct check box).
-
Question:
How many minutes did it actually take you to complete this part of BallWorlds?
-
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.
-
Submit your report for this part of BallWorlds.