Exercise Tuesday

Goal: Focus on understanding the BDD process, and familiarizing yourself with JUnit and automated testing. Also, it will be required follow the "Red, Green, Refactor" workflow for every project moving forward. Emphasize test coverage and testing as opposed to moving through the material quickly.

Warm Up

  • What is an annotation? What does it look like? What does it do?
  • What is the naming convention for JUnit test methods?
  • When we compile our code, where do the compiled .class files live?
  • Why is Behavior-Driven Development beneficial?
  • What is the difference between an Array and an ArrayList? When do you use one over the other?
  • Why don't we use == to compare strings?


Ping Pong with BDD

Follow along closely with the coursework and create the following simple application with the BDD process. NOTE: this app is a simpler version of the Beep Boop project you completed for intro. Review your app to jog your memory as to how to approach this problem. Use % (modulo) to check for divisibility just as you did in intro unit.

Create an application that takes a number from a user and returns a range of numbers from 1 to the chosen number with the following exceptions:

Numbers divisible by 3 are replaced with the String "ping"
Numbers divisible by 5 are replaced with the String "pong"
Numbers divisible by 15 are replaced with String "ping-pong"
Here is the first two specs for this project:

Behaviour: Counts up to provided number
Input: 2
Output: [1,2]

Behaviour: Counts up to provided number and replaces numbers divisible by 3 with a string
Input: 3
Output: [1,2, "ping"]

Break the program down into simple, individual behaviors with input/output examples. Begin with the simplest possible behavior. List these behaviors (also known as "specs" or "specifications") in the project's README.md file.

Focus on developing Ping Pong with BDD and coded tests, and on the Java-specific syntax and concepts. Take your time and make sure you and your partner are comfortable with JUnit tests and the "Red, Green, Refactor" workflow before moving on.

Scrabble Scorer

In-Class Lessons

Before you begin this next assignment, read through the following lessons with your partner. They contain information about concepts that will be very relevant to your application (hint, hint).


Write a command line application that takes a word and returns its Scrabble score. For reference, here are the point values of each letter in Scrabble:

A, E, I, O, U, L, N, R, S, T       1
D, G                               2
B, C, M, P                         3
F, H, V, W, Y                      4
K                                  5
J, X                               8
Q, Z                               10

Here is your first spec to get you started. As you can see, the simplest possible behavior has been identified as "returning a score for a single letter":

public void calculateScore_returnsScoreForSingleLetter_1() throws Exception {
  Scrabble testScrabble = new Scrabble();
  Integer expected = 1;
  assertEquals(expected, testScrabble.calculateScore("a"));

Code and test all back-end logic before creating a command line interface.

Rock, Paper, Scissors

In-Class Lesson

Before you begin, read through the following lesson with your partner:


Create a Rock, Paper, Scissors command line game. Start out by making a two-player game. Each player will have to enter their choice while their opponent looks away from the screen. Write a method to handle evaluating the different combinations of plays, and determining a winner. Make sure to consider the possibility of tie games, too.

Write specs for all possible combination of plays. Evaluating each different combo (ie: Rock vs. Scissors, Paper vs. Rock, etc.) should be considered a unique behavior, and therefore each combo will require its own spec.

And remember, always use .equals() to compare strings.

Further Exploration

Create a one player version of Rock, Paper, Scissors, where a single user may play against the computer. The program should randomly choose paper, rock or scissors.


It's tricky to test randomness. After all, how can you predict something that's random?! Unit tests are not ideal for testing for randomness, because they generally test for booleans: either an expected result IS the same as the result returned by your function, or it is not.

There are more complex ways to test for randomness that you could explore (One example: Run your random generator ~ 100 times. Then, parse that data and and look at the distribution you receive. If the results are too similar, then your random generator or algorithm is not very random) but they go a bit beyond what we can do during the daytime. If you are interested in this topic, have a look at the discussion on this thread.

For now, we'll simply test your 'random' method by simply confirming that it successfully returns an instance of a String, like this:

public void computerChooses_shouldBeAString_true() throws Exception {
  Game testGame = new Game();
  String computerChoice =  testGame.computerChooses();
  assertEquals(true, computerChoice instanceof String);

Find and Replace

In-Class Lesson

If you get to this project in class today, make sure to read through the following lesson from tonight's homework before you begin:


Create a command line interface application where a user enters a string, chooses a word in that string, and provides a replacement for that word. Your method could look something like myGame.findAndReplace("hello world", "world", "universe"), which would return "Hello universe".

Begin by replacing only whole words, like the example above. After that, take into account matches that are not whole words. For example myGame.findAndReplace("I am walking my cat to the cathedral", "cat", "dog") would return "I am walking my dog to the doghedral."

Make sure to account for odd user inputs like all capitalization, partial capitalization, etc. Each of these should also have a corresponding test.


Anagrams are words that can be rearranged into other words. Create a command line application that allows users to submit two words, and see whether they are anagrams of one another.

If you are able to solve this, create another method that accepts multiple words. The application should display all words in the list that are anagrams of one another.


Hint #1: Strings can be transformed into Arrays using the .split() method:

> "act cat arm ram".split(" ");
java.lang.String[] res = ["act", "cat", "arm", "ram"]

Hint #2: Arrays may be transformed into ArrayLists like this:

List<Element> elementList = new ArrayList<Element>(Arrays.asList(array));

Hint #3: Lists may be sorted using something like this:



A palindrome is any word, phrase, number, or other sequence of characters which reads the same backward or forward. Create a command line interface application that can identify if a word is a palindrome. As always, focus on back-end logic first, and thoroughly tests each behavior before moving on to the next.

Prime Sifting

Given a number, write a method that returns all of the prime numbers less than that number.

This is a tricky problem, and I want you to use the Sieve of Eratosthenes to solve it. Here's how the Sieve of Eratosthenes works to find a number up to a given number:

  • Create a list of numbers from 2 through n: 2, 3, 4, ..., number.
  • Initially, let prime equal 2, the first prime number.
  • Starting from prime, remove all multiples of prime from the list.
  • Increment prime by 1.
  • When you reach number, all the remaining numbers in the list are primes.

You also might find this video helpful in explaining the Sieve.

Peer Code Review

  • Tests have complete coverage for all behaviors.
  • Application(s) work as expected.
  • All tests are passing.
  • Java logic is easy to understand.
  • Code and Git documentation follows best practices (descriptive variable names, proper indentation, detailed commit messages, well-formatted README, etc.)