teaching machines

CS 245 Lab 1 – JUnit

September 4, 2013 by . Filed under cs245, fall 2013, labs.

Welcome to the first lab of CS 245. Lab is a time for us to throw some problems at you and for you to clarify your understanding, collaborate with others, get things wrong, and ask lots of questions.

Your work for each checkpoint section is inspected by your instructor or TA. Checkpoints are only inspected during lab—not office hours or any other time. If you do not get them inspected before the end of lab, you may do so in the first 15 minutes of the next lab.

Synopsis

In this lab, you will do some unit testing of some methods that you write. The first exercise involves writing code to test a password’s strength. A longer password pulled from a larger alphabet takes more time to guess and is therefore safer. You’ll write methods that report how varied a password is and write JUnit tests to verify the methods’ correctness. In the second exercise, you pick a problem to solve and write tests of your implementation.

Setup

First:

Set up an Eclipse project named cs245 and a package named lab01. (I suggest creating one project for the whole semester, with each lab and homework in its own package. Otherwise, you will have to do a lot of fiddling with project configuration.)

Save all work to one of your H drive directories. Why not C?

The JUnit assertions are all tucked away inside the Assert class, which is not automatically visible to your code. To add the JUnit 4 library, right-click on your cs245 project, go to Build Path / Add Libraries…, choose JUnit / JUnit 4, and hit Finish.

The various assertions at your disposal have reasonable documentation.

Create two classes: one for your password methods and one for your JUnit tests. Use the red-green-refactor method approach to software development as you complete the following checkpoints. Feel free to consult the code from our first lecture.

Checkpoint 1

Partner A gets the computer. Implement a check to see if a password contains whitespace characters. Write the test first and a signature plus return statement. The test should fail. Then implement a fix.

Partner B gets the computer. Implement a query for how many digits a password contains.

Partner A gets the computer. Implement a query for how many uppercase letters a password contains. Implement a query for how many lowercase letters a password contains.

Partner B gets the computer. Implement a check to see if a password is valid. A password is valid if it contains at least 2 numbers, has an uppercase letter, has a lowercase letter, and contains no punctuation.

Show us your work when done.

Checkpoint 2

Between the two of you, decide upon a computational task whose difficulty is somewhere between trivial and impossible. (For example, the task might be tracking a Set of names, which is like a list but doesn’t allow repeats. You’ll want to know the number of names, be able to retrieve the ith name, add new names, check to see if a name has already been added, remove a name, etc.) One of you writes code to implement the task and that passes all tests. The other writes JUnit tests that try to break the code.

Show us your work when done.