starting foreach CS230: Data Structures

CS230

 

Random passwords

Goals:

  1. to work with Java's Strings and Random classes.
  2. to practice method definitions and method invocations
  3. to practice with loops
  4. to practice with logical predicates

Task: Generate Random Passwords

The problem

People have troubles coming up with random passwords, so some companies are trying to help by generating random passwords for them. A good random password should appear "pronounceable", that is, one could attempt to read it as if it was a real word.

In this assignment we will create pronounceable random passwords by coming up with words that do not have two vowels or two consonants in a row. Consider for example:

WUZE
REGOL
DIDUGO
QUGEBUT
PIBEMAMA

You are asked to write a program, RandomPasswords, that generates such random passwords of various lengths. Start by writing pseudocode for an algorithm that follows the specifications below.

When you have persuaded yourself that you have a well-defined and correct plan, move on to writing java code.

Specifications

  • Write a method, generateRandomPassword(), that takes as input an integer n, and produces a word consisting of random letters so that: The word has length n, and has a vowel in every even location, but never in an odd location. See the example above.

  • The main() method should call the generateRandomPassword() to produce random words with lengths between 4 and 8 letters, as in the example above.

  • Only the main() method is static. Every other method must not be. Which means, that you need to use a dummy object named RandomPasswords in main().

  • Here is how we want you to choose random letters: Your generated passwords should only contain capital letters. Define a String named alphabet that contains all the capital letters of the English alphabet, such as:

String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; .

Pick a random character from alphabet. (The Random class should be used here)

  • How can you make sure the random character you chose is a vowel? (Or a consonant?) Use a loop keep to keep choosing a random character until you find one that is a vowel. (Similarly for choosing a consonant.)

  • In defining the method generateRandomPassword() you MUST use the predicate helper methods that we saw in class: isVowel() and isEven().

  • It is expected that your code has careful and meaningful documentation (top of the file and top of methods javadoc, as well as inline documentation). Top of the file javadoc should include values for the @author and @version tags. Top of the method javadoc should include values for the @param and @return tags as needed.

What to submit

Submit the following files (only):

  • your RandomPasswords.java file, and
  • a file with your test results, named TestingRandomPasswords.txt

(OPTIONAL) Do you want to check that your code passes our basic tests?

Use the JUnit test fileRandomPasswordsTest.java code we provide here.