CS5500  Foundations of Software Engineering Fall 2019 Professor Weintraub

Homework 5: Behavoral Design Patterns

This homework involves developing code using behavioral design patterns, namely observers. Grading will consider:

1. Whether your solution works for any standard tests (given in the homework) and your tests. 2. Design eciency and ecacy. 3. Code style and documentation.

In evaluating the code, we will run maven from the command line. You should insure your pom. con- tains everything needed to run your code standalone. Reminder, we highly recommend you try running maven from the command line on your own machine before your nal push to submit your work. Code that doesn't work will be an automatic 15% deduction.

Same rules apply for style. There should be no code smells agged. Do not make excessive use of @suppresswarnings to get around this requirement. Excessive use will invoke a deduction It should also go without mentioning that proper javadoc and code styling should be followed.

Turn in your answers (the project) by pushing them to your personal class repository under homework_3. Remember, you must work by yourself and your work must be original. No internet except for Java language or class help.

1 Observer Pattern (20 points)

Figure 1 lays out code for a very simple subject class. It doesn't do much other than change its state and reect its state. Its state is a simple string.

However, subject is very popular and dierent communities want to know when its state changes. Each community wants to act on this information in a dierent way. In some sense, this isn't too far o from the idea of following someone on Twitter. When a person you are interested in sends out a tweet, you want to know that person tweeted and what the tweet said.

Our observers are simple. One repeats what Subject's state changed to as string of the ASCII values (expressed as decimal numbers) of the characters in the changed state. Another watches for key words. If the Subject's state includes one or more of the key words in the Observer's list (case insensitive), the observer sounds an alarm which keywords were used and the time of day in which the word appeared. A third observer outputs the reverse of the string.

For example, suppose Subject's state change to Design Patterns - Hoorah. Let's assume Observer2 is looking for the word pattern and the word Fozzie.

Observer1 would report: 68 101 115 105 103 110 32 80 97 116 116 101 114 110 115 32 45 32 72 111 111 114 97 104 CS5500  Foundations of Software Engineering Homework 5: Behavoral Design Patterns

1 public class Subject { 2 3 private String state; 4 5 public String getState() {

6 return state; 7 } 8 9 public void setState(String state) { 10 this.state = state; 11 }

12 }

Figure 1: Code for Subject Class

Figure 2: UML for Observer Pattern, taken from the lectures

Observer2 would report: Alert: "pattern" used at 1:12pm 10/12/2019

Observer3 would report: harooH - snrettaP ngiseD Figure 2 shows a UML class diagram for the observer pattern.

What to do: 1. Implement subject and these three observers using the observer design pattern. (15 pts)

2. Provide a write-up (one page, max) explaining why your code is good and why your test suite is sucient. (5 pts)

2 Iterator Pattern (30 points)

A set of natural numbers can be represented compactly using an array of int values. The basic idea is to use the i th bit to indicate whether the number i is in the set. In Java, an int is represented using 32 bits, so in this representation the set {0, 1, 4, 7, 31} can be represented by the binary number 10000000000000000000000010010011. For sets containing larger numbers, an array of ints can be used, where the rst element represents bits 0-31, the second represents bits 32-63, and so on. Figure 3 (see page 4) gives the interfaces for Iterator and IBitVector.

2 CS5500  Foundations of Software Engineering Homework 5: Behavoral Design Patterns

What to do:

1. Implement IBitVector with a class that implements each of the methods except the iterator() method. You must only iterate on the bitvector. (12 pts)

2. Implement the iterator() method. You may add additional private classes, methods and elds if needed. Extend your test suite to test your iterator. (13 pts)

3. Provide a write-up (one page, max) explaining why your test suite is sucient. (5 pts)

3 Adapt a Stack (20 points)

Your task is to create a Stack per the interface dened in Figure 4 (see page 5) by adapting a Java ArrayList.

What to do:

1. Implement the stack in a package called stack. Place your tests in a package called stacktests. (15 pts)

2. Provide a write-up (one page, max) explaining why your test suite is sucient. (5 pts)

Exta and Entirely Optional: Using Sonarqube to Generate Coverage Reports

Sonarqube is a static analyzer. It's an open-source tool that evaluates code for smellse.g. duplicated code, coding standards, does testingunit tests and code coverage, code style and complexity,and security vulnerabilities. It works on Java code, which is why we use it in the project. It integrates with many tools, e.g. Maven and , which we use in the project. Sonarlint is Sonarqube's plug-in for and Intellij. It also integrates with git to detect when changes occur.

As extra credit, you have the option to use Sonarqube in this assignment. You can earn up to an additional seven (7) points, depending on the extent to which you integrate Sonarqube into your work. Here is a breakdown of the possibilities to earn extra points. There is no downside point-wise if you try but fail to integrate Sonarqube successfully.

1. If you incorporate a Sonarqube report in your document, that will be worth two (2) additional points. You must include the entire report, not excerpts, on the nal submission.

2. If you install Sonarqube on your local machine, that will be worth two (2) points. You will need to provide evidence that Sonarqube is installed and working. You can nd how to do this at https: //docs.sonarqube.org/latest/setup/get-started-2-minutes/. You can download Sonarqube at https://www.sonarqube.org/downloads/.

3. If you integrate Sonarqube into your maven POM and it becomes part of your project's workow. The documentation on how to incorporate Sonarqube with maven is at https://docs.sonarqube.org/ display/SCAN/Analyzing+with+SonarQube+Scanner+for+Maven. If you integrate the two success- fully, meaning we can do mvn sonar:sonar from the grader's terminal, you can earn an additional three (3) points.

3 CS5500  Foundations of Software Engineering Homework 5: Behavoral Design Patterns

1 /**

2 *A generic iterator 3 */ 4 public interface Iterator { 5 6 /** 7 * Check if there are more elements

8 */ 9 boolean hasAnotherElement(); 10 11 /** 12 * Return the next element 13 */

14 T nextElement(); 15 } 16 17 /** 18 *A bitvector.

19 */ 20 public interface IBitVector { 21 22 /** 23 * Determine if the bit at positioni is set. 24 */

25 public boolean get(int i); 26 27 /** 28 * Set the bit at positioni. 29 */ 30 public void set(int i);

31 32 /** 33 * Clear the bit at positioni. 34 */ 35 public void clear(int i); 36

37 /** 38 * Set the bits in the argument BitVectorb. 39 */ 40 public void copy(BitVector b); 41

42 /** 43 * Determine the number of non-zero bits in the BitVector. 44 */ 45 public int size(); 46 47 /**

48 * Iterator over the Integer values represented by this BitVector. 49 */ 50 public Iterator iterator(); 51 52 }

Figure 3: Interface Specication for Iterator

4 CS5500  Foundations of Software Engineering Homework 5: Behavoral Design Patterns

1 /** 2 *A generic stack 3 */ 4 public interface Stack {

5 6 /** 7 * Tests if this stack is empty. 8 * @returns true if and only if this stack contains no items; false otherwise. 9 */ 10 public boolean empty()

11 12 /** 13 * Pushes an item onto the top of this stack. 14 * @params item- the item to be pushed onto this stack. 15 * @returns the item argument.

16 */ 17 public E push(E item); 18 19 /** 20 * Removes the object at the top of this stack and returns that object as the value of 21 * this function.

22 * @returns The object at the top of this stack(the last item of the Vector object). 23 * Throws: EmptyStackException- if this stack is empty. 24 */ 25 public E pop() 26 27 /**

28 * Looks at the object at the top of this stack without removing it from the stack. 29 * Returns: the object at the top of this stack. 30 * Throws: EmptyStackException- if this stack is empty. 31 */ 32 public E peek() 33

34 }

Figure 4: Interface Specication for Stack

5