Post

Created by @nathanedwards
 at November 1st 2023, 5:02:37 am.

AP Computer Science Exam Question:

Given the following requirements, write a Java code snippet that generates a random 6-digit password, using the Random class, with the following specifications:

  • The password must contain a combination of uppercase letters, lowercase letters, and digits.
  • The password must have at least one uppercase letter, one lowercase letter, and one digit.
  • The password must be completely random and generated each time the code runs.

Implement the following method:

/**
 * Generates a random 6-digit password that meets the specified requirements.
 *
 * @return the randomly generated password
 */
public static String generatePassword() {
    // your code here
}

Explain any assumptions, limitations, or design decisions you made while implementing the method.

Answer and Explanation:

The Random class in Java provides methods to generate random numbers and can be used to fulfill the requirements of this problem.

Here's the implementation of the generatePassword method:

import java.util.Random;

public class RandomPasswordGenerator {
    public static void main(String[] args) {
        String password = generatePassword();
        System.out.println(password);
    }

    public static String generatePassword() {
        Random random = new Random();
        StringBuilder password = new StringBuilder();

        // Generate a random uppercase letter
        char uppercaseLetter = (char) (random.nextInt(26) + 'A');
        password.append(uppercaseLetter);

        // Generate a random lowercase letter
        char lowercaseLetter = (char) (random.nextInt(26) + 'a');
        password.append(lowercaseLetter);

        // Generate a random digit
        char digit = (char) (random.nextInt(10) + '0');
        password.append(digit);

        // Generate remaining random characters
        for (int i = 0; i < 3; i++) {
            char randomChar = (char) (random.nextInt(62) + 'a');
            password.append(randomChar);
        }

        // Shuffle the password characters
        for (int i = password.length() - 1; i > 0; i--) {
            int j = random.nextInt(i + 1);
            char temp = password.charAt(i);
            password.setCharAt(i, password.charAt(j));
            password.setCharAt(j, temp);
        }

        return password.toString();
    }
}

Explanation:

  1. We create a new instance of the Random class to generate random numbers.
  2. We use a StringBuilder to build the password incrementally.
  3. We generate a random uppercase letter using the formula (char) (random.nextInt(26) + 'A'). This generates a random number between 0 and 25, which we add to the ASCII value of 'A' to get a random uppercase letter.
  4. We append the generated uppercase letter to the password StringBuilder.
  5. Similarly, we generate a random lowercase letter and a random digit using the same approach.
  6. Next, we generate 3 remaining random characters by generating a random number between 0 and 61 (since there are 26 uppercase letters, 26 lowercase letters, and 10 digits) and adding it to the ASCII value of 'a'.
  7. We append these random characters to the password StringBuilder.
  8. To ensure that the password is completely random, we shuffle the characters of the password. We iterate through each character starting from the last one, randomly swap it with another character using the setCharAt method of StringBuilder.
  9. Finally, we return the generated password as a string.

Assumptions/Limitations/Design Decisions:

  • We assume that the generated password should be exactly 6 characters long, as stated in the problem requirements.
  • The password will contain a combination of uppercase letters, lowercase letters, and digits, and will always have at least one of each.
  • The password will be completely random and generated each time the code runs.
  • The password generation process does not use any external libraries or functions beyond the Random class.
  • We assume that the generated password does not need to be cryptographically secure.