Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Password generator. Your program should generate a random password that satis

ID: 3634593 • Letter: 1

Question


1. Password generator. Your program should generate a random password
that satises the following constraints:
(a) The only valid characters are a{z A{Z 1{9 * @ % $ + -
(b) The password must be 8 characters long
(c) It must contain at least one of each kind of character i.e., lower-case
character, uppper-case character, digit, symbol
(d) It must not be predictable. By this I mean that position 4, for
example, cannot always be a digit.
Hint: Generate a password and then check if it satises the con-
straint

Explanation / Answer

public static void main(String[] args)
{
// make a list of characters
java.util.ArrayList<Character> list = new java.util.ArrayList<Character>();

// add a random digit, symbol, uppercase letter, lowercase letter
list.add((char)((int)(Math.random()*9)+'1'));
list.add((char)((int)(Math.random()*26)+'a'));
list.add((char)((int)(Math.random()*26)+'A'));
char[] symbols = new char['*', '@', '%', '$', '+', '-'};
list.add(symbols[(int)(Math.random()*symbols.length)];

// add 4 more uppercase characters for a total of 8, you could add any kind of character here
for(int i = 0; i < 4; i++)
list.add((char)((int)(Math.random()*26)+'A'));
// change the order to make it unpredictable
java.util.Collections.shuffle(list);
// extract password from list
String password = "";
for(char c : list)
password += c;

System.out.println(password);
}