You are on page 1of 9

CST438 Software Engineering Fall 2016 Final Exam

Name : Heather McCabe

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.

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
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 */

public interface Publisher {


public boolean subscribe(Subscriber subscriber);
public boolean unsubscribe(Subscriber subscriber);
}

4
/* Subscriber.java */

public interface Subscriber {


public void receive();
}
/* App.java */

import javax.swing.*;
import java.awt.*;

public class App {


public static void main(String[] args) {
Data data = new Data();

JFrame window = new JFrame();


window.setSize(300, 200);

JPanel panel = new JPanel();


panel.setLayout( new BorderLayout());

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

ViewCount viewCount = new ViewCount("Character Count", data);


data.subscribe(viewCount);
panel.add(viewCount, BorderLayout.NORTH);

ViewPalindrome viewPalindrome = new ViewPalindrome("Palindrome?",


data);
data.subscribe(viewPalindrome);
panel.add(viewPalindrome, BorderLayout.SOUTH);

window.add(panel);
window.setVisible(true);
}
}
/* Data.java */

import java.util.ArrayList;

public class Data implements Publisher {

private String data ="";


private ArrayList<Subscriber> subscribersList = new
ArrayList<Subscriber>();

public String getText() {


return data;
}

// Update the text and notify subscribers if it changed


public void setText(String str) {
if (!data.equals(str)) {
data = str;

notifySubscribers();
}
}

5
public String getCountAsString() {
return Integer.toString(data.length());
}

public boolean subscribe(Subscriber subscriber) {


if (subscribersList.contains(subscriber)) {
return false;
} else {
return subscribersList.add(subscriber);
}
}

public boolean unsubscribe(Subscriber subscriber) {


return subscribersList.remove(subscriber);
}

private void notifySubscribers() {


for (Subscriber s : subscribersList) {
s.receive();
}
}
}
/* View.java */

import javax.swing.*;
import javax.swing.event.*;

@SuppressWarnings("serial")
public class View extends JPanel {
private JTextField textfield = new JTextField("", 10);
private Data data;

public View(String label, Data model) {


data = model;
add(new JLabel(label));
textfield.setText(data.getText());
add(textfield);

textfield.getDocument().addDocumentListener(new
DocumentListener() {
public void changedUpdate(DocumentEvent ev) {
}

public void removeUpdate(DocumentEvent ev){


data.setText(textfield.getText());
}

public void insertUpdate(DocumentEvent ev){


data.setText(textfield.getText());
}

});
}
}
/* ViewCount.java */

6
import javax.swing.*;

@SuppressWarnings("serial")
public class ViewCount extends JPanel implements Subscriber {

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


private Data data;

public ViewCount(String label, Data data) {


this.data = data;
add(new JLabel(label));
textfield.setText(this.data.getCountAsString());
textfield.setEditable(false);
add(textfield);

public void receive() {


textfield.setText(data.getCountAsString());
}

}
/* ViewPalindrome.java */

import javax.swing.*;

@SuppressWarnings("serial")
public class ViewPalindrome extends JPanel implements Subscriber {

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


private Data data;

public ViewPalindrome (String label, Data model) {


data = model;
add( new JLabel(label));
textfield.setText( Palindrome.isPalindrome(model.getText() ) ? "yes"
: "no" );
textfield.setEditable(false);
add(textfield);
}

public void receive() {


if (Palindrome.isPalindrome(data.getText())) {
textfield.setText("true");
} else {
textfield.setText("false");
}
}
}

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

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.

A Model-View-Controller pattern (MVC) splits an application into 3 different parts with


different responsibilities. The Model is generally the applications data and associated
methods for data manipulation. The View is the output that the user sees. The Controller
is responsible for communication between the Model and the View. In J2EE, the View is
the JSP java server pages that render HTML for displaying web pages, the Model is the
JPA Entity classes, and the Controller is the Servlets that interact with the Model classes
and feed the output to the View JSPs.

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?

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.

4. Proxy Design Pattern

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.

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.

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.

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).

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.

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.

You might also like