You are on page 1of 8

CST438 Software Engineering Fall 2016 Final Exam

Name : _Wendy Gray____________________________

Directions:
The exam is open book and there is no time limit. However make sure you
submit your document before end of day Friday Dec 16th.
Do your own work. Sign the honor pledge.

Honor Pledge: I have not given nor received unauthorized assistance in doing
this exam.

____Wendy Gray_________________
Sign or print your name here.

Problem Points Your Score


1 30
2 30
3 20
4 20
total 100

1
1. UML Class Sequence diagram

A UML sequence diagram is used to show the details of class collaborations.

Download the file finalExam_questino1_UML.zip that contains classes: Weather (the main
class), WeatherJason and FetchTask. Create an eclipse Java project and copy these classes
into the project. To compile and execute the program you need to have the gson.jar file (also in
the zip) in your build path.

The user enters one or more names of cities separated by commas

An asynchronous background task is created to get the weather information from the web site
openweathermap.org and display it in a not too interesting text format (weather.com has nothing
to worry about here).

Complete the UML sequence diagram to show the interactions among the objects:
Weather.ActionListener, FetchTask,the UI components and the openweathermap.org web site.

In particular, show the creation of the FetchTask object, how does control pass to it, when does
control return to the UI and when do the cityField and centerField components get updated. It
is important that you show the interactions in the correct sequence and that the arrows originate
from the correct calling object and terminate on the correct called object.

2
Disable cityField creates
call() loop

Request weather

Send weather
Send city weather info
Enable cityField

<Paste your complete UML diagram here. >


Please see additions to the diagram above.
<Explain briefly in words what the diagram shows>
This diagram shows the user entering one or some cities in the City Field, which, when the user
navigates away from the field, get sent from the Action Listener to create a FetchTask object
and parsed into an ArrayList. The action listener runs the FetchTask call() method when on
another available thread, which sends each city string in the ArrayList individually to get the
corresponding weather in GSON, parse back into String, and send back to the CenterText field
for the user to view.

2. Pub/Sub (also called Observer) Design Pattern

Download the ProblemDesign.zip file. It contains 6 classes; App is the main class. Create a
Java project and copy the 6 classes into the project.

A palindrome is text that reads the same backwards as forwards. Examples are

tattarrattat
Never odd or even.
Do geese see God?
A Toyota! Race fast.. safe car: a toyota.

The application displays a window with 3 field, the user enters text in the center field and the
program shows the character count in the top field and determines if the text is a palindrome

3
and displays YES or NO in the lower field. The 3 field are implements as 3 views over a
common data model (using the Model-View-Controller pattern).

The program as written does not update the other fields when the text is entered.

Use the Publish/Subscriber pattern to finish the program. You will need to
define Publisher and Subscriber interfaces
decide which class(es) need to implement the subscriber interface. Remember that you
need a receive method on the subscriber.
the Data class should be the publisher and notify all subscribers whenever the text
changes.

<copy your source code for Publisher and Subscriber java interfaces
here>
Publisher interface:
public interface Publisher {
public void register(Subscriber obj);
public void notifySubscribers();
public Object getUpdate(Subscriber sub);
}

Subscriber interface:
public interface Subscriber {
public void receive();
public void setPublisher(Publisher pub);
}

<copy your modifications to Data, ViewCount, ViewPalindrome, View


classes here>
Changes to App class:
ViewCount myViewCount = new ViewCount("Character Count", data);
ViewPalindrome myViewPalindrome = new ViewPalindrome("Palindrome?",
data);

4
data.register(myViewCount);
data.register(myViewPalindrome);

panel.add(new View("Text", data), BorderLayout.CENTER);


panel.add(myViewCount, BorderLayout.NORTH) ;
panel.add(myViewPalindrome, BorderLayout.SOUTH);

Changes to Data class:


public class Data implements Publisher{

private String data ="";


private List<Subscriber> subscribers;

public Data(){
this.subscribers=new ArrayList<>();
}

public String getText() { return data;};

public void setText( String str) {


if (! data.equals(str)) {
data = str;
notifySubscribers();
}
}

public String getCountAsString() {


return Integer.toString( data.length() );
}

@Override
public void notifySubscribers() {
for (Subscriber obj : subscribers) {
obj.receive();
}
}

@Override
public Object getUpdate(Subscriber sub) {
return this;
}

@Override
public void register(Subscriber obj) {
if(!subscribers.contains(obj)){
subscribers.add(obj);
obj.setPublisher(this);
}
}

}
Changes to ViewCount:
public class ViewCount extends JPanel implements Subscriber{

private JTextField textfield = new JTextField("", 10);

5
private Publisher pub;

public ViewCount(String label, Data model) {


add( new JLabel(label));
textfield.setText( model.getCountAsString() );
textfield.setEditable(false);
add(textfield);
}

@Override
public void receive() {
// TODO Auto-generated method stub
Data model = (Data)pub.getUpdate(this);
textfield.setText( model.getCountAsString() );
}

@Override
public void setPublisher(Publisher pub) {
// TODO Auto-generated method stub
this.pub = pub;
}

}
Changes to ViewPalindrome:
public class ViewPalindrome extends JPanel implements Subscriber {

private JTextField textfield = new JTextField("", 10);


private Publisher pub;

public ViewPalindrome (String label, Data model) {


add( new JLabel(label));
textfield.setText( Palindrome.isPalindrome(model.getText() ) ? "yes"
: "no" );
textfield.setEditable(false);
add(textfield);
}
@Override
public void receive() {
// TODO Auto-generated method stub
Data model = (Data)pub.getUpdate(this);
textfield.setText( Palindrome.isPalindrome(model.getText() ) ?
"yes" : "no" );
}

@Override
public void setPublisher(Publisher pub) {
// TODO Auto-generated method stub
this.pub = pub;
}
}
Changes to View class: None, since it needs no updates when its
textField is modified.

6
Explain describe the Publisher / Subscriber pattern. When do you use
it? How do you implement it?
The Publisher / Subscriber pattern should be used when multiple objects (subscribers) need to
perform updates whenever another object (publisher) updates. For instance, this application
requires the ViewCount and ViewPalindrome objects to update their respective textFields
whenever the Data object updates. We create an interface for publisher and for subscriber for
them to implement, which includes a receive() method for the subscribers. So the beginning of
the app registers each subscriber by putting those objects into a list within the publisher object
(Data). Then, when the Data object changes, it can go through its list of subscribers, and call
their respective receive() methods, which are written to update whatever is necessary within
their own scope.

3. J2EE

a. In your own words describe what a Model-View-Controller pattern


is and how J2EE (JSP java server pages, Servlets, and JPA Entity classes)
implement this pattern.

<your answer>
A MVC pattern is one that keeps the data and UI code separate with
controller code as the go-between. Whatever is displayed in the UI is
based on the data (model), and the controller constructs the UI (view) as
necessary based on that data. Likewise, the input from the UI changes
the data via the controller. J2EE implements this with Servlet code as the
controller to get and set data in a database via JPA Entity classes
(model), and updates & receives input from a UI in the form of html on
JSPs (view).

b. When writing a REST-JSON application, there are no JSPs that


create html. Can an MVC pattern still be applied to design and implementation of
a REST-JSON server application?

<your answer>
To some extent, yes, a MVC pattern can be implemented. The REST-
JSON code becomes the View and one could write a Java class to be the
Controller, with JPA Entity classes still as the Model. One could even
write further code to take the JSON result and format it nicely for display
and further input.

4. Proxy Design Pattern

7
a. Read or review the description of the Proxy design pattern in
Marsic 2.6.4 page 264. Describe ways in which the TestHttpExchange class that
was used in Assignment 4 on Junit test of the Http Hangman game, is an
example of using the Proxy pattern.

<your answer>
Marsic describes a Proxy pattern as using an object that controls or
enhances access to another object. TestHttpExchange does this in
instead of accessing HttpExchange directly for testing, TestHttpExchange
has individual methods to access and test HttpExchanges methods and
data. TestHttpExchange enhances the access in that it adds extra code
to further show test data.

b. Describe one way in which the TestHttpExchange class is not an


example of the Proxy pattern according to the Marsic definition of Proxy pattern.

<your answer>
Marsic also describes a Proxy pattern as needed when the logistics of
accessing the subjects services is overly complex and comparable or
greater in size than that of the clients primary responsibility. Therefore,
the reason for using the TestHttpExchange does not fit the reasons for
using a Proxy pattern. TestHttpExchange is not created because of
complex access of HttpExchange, but because its placed in a specific
environment to run tests.

c. Briefly describe one other situation where proxy objects would be


useful. It might be something that you coded for other courses but did not realize
at the time it was a Proxy pattern; it might be an example from the textbook; or
an example that you think up on your own.

<your answer>
An example of a situation where a proxy object would be useful is in the
creating and displaying of an image file. For instance, there could be a
reason to not load an image file until its time to display. So an Image
interface could be defined with a display() method that could be
implemented by the RealImage and ProxyImage classes. RealImage
loads the image from the filename within the constructor, but ProxyImage
just saves the filename. Then, once ProxyImages display() is called, it
creates an instance of RealImage, which loads the image, if the image
hasnt already been loaded. Using ProxyImage therefore saves
resources in cases where an Image object is instantiated but never has
display() called, since the image wont ever actually load.

You might also like