unit18csce302HW2

HW 2
Kirk Scott
1
2
3
4
• Remember, the ultimate authority for what this
assignment consists of for any given semester is the
Word document posted on the course Web site
• In general the assignment covers:
• Observer
• Memento, Type 1, runtime
• Memento, Type 2, persistent
• Mediator
• ***It does not cover the Façade
• Adapter
5
• The contents of the Word document (at some
point in time) are given on the following
overheads
• The is just for use as an in-class reference
• It is representative of the assignment and
illustrates in general the kind of requirements
and check-offs there are for the homework
6
• CSCE 302, HW 2
• Explanation and Check-Off
•
•
• Name:
_________________________________
7
• Preliminary explanation of the assignment:
• You should find that this assignment is similar
in philosophy to the first assignment.
• You have a wide degree of latitude in how you
implement the different parts.
• On the other hand, you do have to implement
the specific patterns as noted in the
directions.
8
• The assignment covers the Observer,
Memento, Mediator, and Adapter design
patterns.
• The Façade pattern is not covered.
• Instead, there are two parts of the assignment
based on the Memento pattern.
• The general purpose of the assignment can be
described in this way:
9
• A. You need to sort through the material, the
design patterns, until you’ve got a clear
mental picture of what they are.
• B. You will also have to come to a clear
understanding of some particular
implementation tools/techniques, which
come from the Java API.
10
• C. In this assignment, you will not make up your
own classes, but significant parts of the
specifications are up to you.
• However, you do have to implement the design
patterns as specified.
• D. When turning in your assignment you will
need to demonstrate your examples to me,
explaining how they correctly reflect the pattern,
characteristics, or specifications that they’re
supposed to represent.
11
• When you hand in your homework, it is
reasonable to expect that you will show me
code, run the code, show the results, and use
both the source and the outcome to explain
what you accomplished.
• I will not collect anything from you.
• Evaluation will be done on this check-off sheet
in real time.
12
• In other situations I am known to simply do
black box testing.
• But in this case, expect to show code to
demonstrate conclusively that you did what
was requested.
• It’s initially your call on what you show and
how you explain it, but if I would like to see
something else in order for it to be clear to
me, I will ask.
13
• In some courses you are expected to do a formal
presentation or write a paper explaining some
development work you’ve done.
• In this course, in addition to writing the code
itself, you’re expected to practice the skill of
describing what you’ve done on a one-on-one,
piece-by-piece basis, with someone who is
knowledgeable about what the code was
supposed to do.
14
• In other words, the motivation behind the
assignment is to do some development work
and also to get experience in expository
presentation by talking to an individual, rather
than by doing a PowerPoint presentation to a
group.
15
• The individual parts of the assignment are
given below.
• The starting point for the first three parts of
the assignment is the example program
known as ColorSeekbar.
• A screen shot of it, its layout file, and its
activity Java file are given below, for reference.
16
• This printout is black and white, so the screen
shot requires a bit of explanation.
• There are three seekbars in the app. From top
to bottom, they are red, green, and blue.
• Each time you move any one of the seekbars,
the background color of the app will change to
whatever color is represented by the current
RGB value represented by the respective
values of the seekbars.
17
18
• This is the layout XML file for the app:
• *** See the posted homework document for
the XML layout file.
19
• This is the Java file for the app.
• In the code shown below, 3 lines have been
commented out and put into boldface.
• These lines of code are highlighted in this way
because they are necessary in the original version
of the app, as given, but they will have to be
eliminated in the version of the app that you will
create by correctly doing the assignment.
• *** See the posted homework document for
the Java code.
20
• 1. ______ 10 pts. Observer:
• As noted above, before the Java code for the
initial version of the app was shown, as a result of
doing this assignment, the lines of code
commented out that are commented out in the
app will no longer be functional.
• Leave those lines of code in your version, but
comment them out.
• When turning in your assignment, this will serve
as graphic evidence that you did the assignment
as directed.
21
• This first part of the assignment falls into 4
basic sub-parts.
• The sub-parts are given below, not necessarily
in the order that you may choose to
implement them.
• A. You will have to make the class MyColor
observable.
• (This is one half of the machinery needed for
using the observability tools in the Java API.)
22
• B. You will have to include the class
MyObserver, as given below, and complete its
implementation.
• (Notice that MyObserver implements the
Observer interface. This is the other half of
the machinery needed for using the
observability tools in the Java API.)
• *** See the posted homework document for
the MyObserver code.
23
• C. You will have to integrate the construction
of the necessary parts of the new version into
the onCreate() method.
• Among other things, this will be the place
where an instance of the MyObserver class is
constructed.
24
• D. You will have to replace the 3 lines of code
in the given version, which currently update
the background color of the app directly, with
the appropriate lines of code which will cause
the updating of the background to happen as
a result of changes to an observable object.
25
• What's shown below is a screen shot of the
app after parts 1, above, and 2, and 3, below,
have been completed.
• This is a preview of the final product of doing
parts 1, 2, and 3.
• Parts 1, 2, and 3 will be checked off together
in one app which implements all 3 parts and
looks like what's shown below.
26
• Note that the keyboard is automatically
shown by the system because the app has
EditText fields in it.
• You don't have to do anything to provide the
keyboard.
• However, you will have to provide the buttons
and EditText fields in the layout.
27
28
• 2. ______ 10 pts. Memento: Runtime
mementos supporting undo functionality:
• This part of the assignment concerns the
Undo button shown above.
• You will need to add the button to the app's
layout and provide a sendMessage() method
for it in the code.
29
• The complete code for this sendMessage()
method is given to you below.
• You have to use exactly this implementation of
the method.
• Your task is to implement the stack which holds
the mementos and integrate it into the
onCreate() method.
• The idea is that at any point during a run, you can
return the background to its previous color—
except when you've reached the point where
there is no previous color.
30
• The stack should be created with an initial default
color, and it should not be possible to pop the
stack when it contains just one element.
• In essence, you're being given a small problem in
reverse-engineering some software.
• If you can do this successfully, you will have
demonstrated your understanding of how the
Memento design pattern is implemented.
• *** See the posted homework document for
the sendMessage() method code.
31
• 3. ______ 10 pts. Memento: Persistent
mementos written to and read from storage:
• This part of the assignment concerns the Save
and Load buttons shown above.
• You will need to add these buttons to the
app's layout and provide sendMessage()
methods for them in the code.
32
• You should refer to the sendMessage() method
examples for serializable file I/O in the unit
overheads for examples of how to implement
these methods.
• The idea is that at any point during a run, you can
save the background color, and at any time during
a run, you can also load back in a saved
background color.
• The saving and loading of a background color
should work seamlessly with all of the other
features of the app.
33
• *******
• Questions 4 and 5:
• The code given here is the starting point for both
the Mediator question and the Adapter question.
• You will solve those two problems independently.
• Even if you can't get the mediator to work, it
would still be possible to get the adapter to work,
and vice-versa.
34
• Notice that this code was developed in the
Android Studio environment.
• It does not make use of Android
characteristics—except that it makes calls to
Log.i() rather than System.out.println().
• The output of the code, which establishes
what it does, appears in LogCat.
35
• The app simply uses the default layout and
does absolutely nothing there.
• You can ignore that aspect of Android when
considering the operation of the code.
• In effect, this code is straight Java code, except
for the fact that it has been created for use in
the Android Development environment.
• *** See the posted homework document for
the Java code.
36
• In the example code, the basic idea underlying
the node class is that any node can be paired
with any other node.
• This is indiscriminate:
• A node can be paired with itself, it can be paired
with another node more than once, and it can be
the case that the pairing of node X with node Y is
recorded in node X, while the pairing of node Y
with node X is also separately recorded in node Y.
37
• The onCreate() method has the effect of creating
10 nodes, and based on the pairing, putting them
into the relationship of a doubly linked ring.
• Nodes from 0 through 9 are paired in consecutive
order, with 9 paired with 0, and similarly, nodes
from 9 through 0 are paired in consecutive order,
with 0 paired with 9.
• The critical point about the code is that it records
the pairings in the nodes themselves, not in a
mediator.
38
• Shown below is the output of the code given
above.
• It graphically illustrates the consecutive
pairings which form a ring.
• Note for example, that 0 is paired with 1, and
1 is paired with 0, showing that the pairing, or
linking, goes in both directions.
39
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
.../.../Node: 0 is paired with
.../.../Node: 1
.../.../Node: 9
.../.../Node: 1 is paired with
.../.../Node: 0
.../.../Node: 2
.../.../Node: 2 is paired with
.../.../Node: 1
.../.../Node: 3
.../.../Node: 3 is paired with
.../.../Node: 2
.../.../Node: 4
.../.../Node: 4 is paired with
.../.../Node: 3
.../.../Node: 5
.../.../Node: 5 is paired with
.../.../Node: 4
.../.../Node: 6
.../.../Node: 6 is paired with
.../.../Node: 5
.../.../Node: 7
.../.../Node: 7 is paired with
.../.../Node: 6
.../.../Node: 8
.../.../Node: 8 is paired with
.../.../Node: 7
.../.../Node: 9
.../.../Node: 9 is paired with
.../.../Node: 8
.../.../Node: 0
*******
40
• 4. ______ 10 pts. Mediator:
• The goal of this problem is to accomplish the same set
of relationships between nodes, but to store the
relationships in a Mediator rather than storing them in
the node objects.
• The form of the output may be slightly different, but it
should illustrate the same set of relationships.
• When turning in your work for check-off, it will be
necessary to show the code and highlight how, in fact,
the desired output was obtained using a mediator.
41
• For this problem you are given the classes
shown below.
• You have to implement the class MyMediator
so that when this app is run, it correctly
captures the relationships of paired nodes.
• The showPairList() method of the MyMediator
class displays the relationships.
• It is your call on exactly how to implement it.
42
• The conditions are that it be complete and
correct, but the output doesn't have to
duplicate the output of the code given above.
• Sample, partial output is shown after the
classes given below.
• You can take that as your model of what
showPairList() should do, if you want to.
• *** See the posted homework document for
the Java code.
43
• Sample output:
•
•
•
•
•
•
•
.../.../Node: 0 is paired with node 1
.../.../Node: 1 is paired with node 0
.../.../Node: 1 is paired with node 2
.../.../Node: 2 is paired with node 1
.../.../Node: 2 is paired with node 3
...
.../.../Node: 0 is paired with node 9
44
• 5. ______ 10 pts. Adapter: Recall that the beginning
point for this question is the code given before
question 4.
• The goal of this problem is to substitute a new node
class for the old one, and using adaptation, to get
exactly the same results from the client code, which
has modified only to the extent that it uses the adapter
class rather than the original node class.
• When turning in your work for check-off, it will be
necessary to show the code and highlight how, in fact,
the desired output was obtained using an adapter.
45
• For this problem you are given the classes and
interface shown below.
• You have to implement the adapter class
NewNodeAdapter so that when this app is
run, it gives exactly the same output as the
initial program shown before problems 4 and
5.
46
• The point is this:
• By creating the adapter class, you are allowing
the app to give the same results as before,
while in fact using a base class which stores its
ID as a String rather than as an int.
47
• It's not necessary to change the use or
implementation of showWhatImPairedWith()
because the app just puts in integer ID values
anyway, and doesn't expect to find ID's in
nodes which are Strings which do not contain
valid sequences of integer digits.
• *** See the posted homework document for
the Java code.
48
• ______ out of a total of 50 points possible
49
The End
50