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?

Code


Ping Pong with BDD

Follow along closely with the coursework and create the Ping Pong application using the BDD process. You've already created a Ping Pong application in Intro to Programming, so you should be familiar with how it works. This is intentional. 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).

Project

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":

@Test
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:

Project

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.

Hint

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:

@Test
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:

Project

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

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.

Hints

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:

Collections.sort(yourListNameHere);

Palindromes

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