teaching machines

CS 245 Lab 1 – JUnit and GUIs

January 23, 2014 by . Filed under cs245, labs, spring 2014.

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.

Setup

First:

Set up an Eclipse workspace and project for the course on your H drive. Create a package named lab01.

Checkpoint 1

Passwords take longer to crack if they use symbols from a larger alphabet. In this first checkpoint, you’ll write some methods to test the “crackability” of a password. You will test these tests with JUnit.

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 project, go to Build Path / Add Libraries..., choose JUnit / JUnit 4, and hit Finish.

The various assertions at your disposal have reasonable documentation. Also check out the Character class. Do not resort to ASCII value comparisons or long sequences of character comparisons. You have better things to do in life.

  1. Implement a check to see if a password contains punctuation. Write the test first and a signature plus return statement. The test should fail. Then implement a fix.
  2. Implement a query for how many digits a password contains.
  3. Implement a query for how many uppercase letters a password contains. Implement a query for how many lowercase letters a password contains.
  4. Implement a check to see if a password is complex enough. A password is complex enough if it contains at least 2 digits, has an uppercase letter, has a lowercase letter, and contains punctuation.

Show us your work when done.

Checkpoint 2

Create a simple JFrame with two widgets: a JPasswordField and a JButton that—when clicked—reports the complexity of the password. You can trigger your code with something like:

final JPasswordField passwordBox = ...
...
complexityCheckButton.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent event) {
    // extract the password with passwordBox.getPassword()
    // I'm leaving out some details -- you can do it!
    // report complexity
  }
});

We’ll talk more about ActionListeners. Stay tuned.

Show us your work when done.