Professional Documents
Culture Documents
Directions:
The exam is open book and there is no time limit. However make sure you
submit your document before end of day Tuesday Dec 13th.
Do your own work. Sign the honor pledge.
Honor Pledge: I have not given nor received unauthorized assistance in doing
this exam.
______________________________
Sign or print your name here.
1
1. UML Class Sequence diagram
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
The user enters one or more cities into the city field. This calls the Weather Action
actionPerformed listener, which creates a FetchTask. The FetchTask opens a connection to
openweathermap.org, parses the response, and updates the CenterText field accordingly. The
updated CenterText updates the display.
3
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 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 impl ement 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.
/* Publisher.java */
4
/* Subscriber.java */
import javax.swing.*;
import java.awt.*;
window.add(panel);
window.setVisible(true);
}
}
/* Data.java */
import java.util.ArrayList;
notifySubscribers();
}
}
5
public String getCountAsString() {
return Integer.toString(data.length());
}
import javax.swing.*;
import javax.swing.event.*;
@SuppressWarnings("serial")
public class View extends JPanel {
private JTextField textfield = new JTextField("", 10);
private Data data;
textfield.getDocument().addDocumentListener(new
DocumentListener() {
public void changedUpdate(DocumentEvent ev) {
}
});
}
}
/* ViewCount.java */
6
import javax.swing.*;
@SuppressWarnings("serial")
public class ViewCount extends JPanel implements Subscriber {
}
/* ViewPalindrome.java */
import javax.swing.*;
@SuppressWarnings("serial")
public class ViewPalindrome extends JPanel implements Subscriber {
The Publisher/Subscriber pattern is used when you need objects to respond to changes in or
actions performed by other objects. This is frequently seen implemented in actionListeners. For
example, a JButton may have an ActionListener object attached to it. When the state of the
JButton changes (it is pressed) that state change is published and the ActionListener subscriber
performs actions in response. The publisher needs to have a record of the objects that it must
7
notify about changes or updates. The publisher should implement a subscribe and an
unsubscribe method to manage this list. The subscribers should implement a specific method to
be called by the publisher when it is ready to publish.
3. J2EE
Yes, the JSON strings and data can still be represented on a webpage View using
Javascript and JQuery. MVC is a pattern for organizing code, REST is a server
communication pattern. They are not mutually exclusive.
The TestHttpExchange class overrode the methods of the HttpExchange class to allow
for JUnit testing to work without an active HTTP server. The TestHttpExchange class
had the same interface as the HttpExchange class and intercepted and processed
requests to it.
8
According to the Marsic definition of the Proxy pattern, the class must provide safe,
efficient, and correct access to the real subject, but the TestHttpExchange class did not
allow real access to an Http server and did not fully implement the full interface (most
methods just threw an error).
The textbook gives an example of how protection proxy objects can be used to limit
access to a server. In such a case, if different user types have different access levels, a
proxy object can check server requests and reject unauthorized requests before they are
ever sent to the server, lightening the demand on the server.