Skip to content

Instantly share code, notes, and snippets.

@mlabisi
Created July 25, 2016 02:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlabisi/a615c80ca66dd5e1b1b708b868d337ed to your computer and use it in GitHub Desktop.
Save mlabisi/a615c80ca66dd5e1b1b708b868d337ed to your computer and use it in GitHub Desktop.
import java.util.Random;
import java.util.ArrayList;
public class KeyCreator {
// Define the variables
private Random random = new Random();
private int passwordLength;
private String password;
private ArrayList<Character> characters = new ArrayList<Character>();
public KeyCreator(int length) {
// Initialize the variable
this.passwordLength = length;
this.random = new Random();
this.password = "";
this.characters = new ArrayList<Character>();
}
public String createPassword() {
// write code that returns a randomized password
int i = 0;
this.password = "";
while (i < this.passwordLength) {
int index = random.nextInt(25);
char symbol = "abcdefghijklmnopqrstuvwxyz".charAt(index);
this.characters.add(symbol);
i++;
}
for (Character letter : this.characters) {
this.password += letter;
}
this.characters.clear();
return this.password;
}
}
public class Program {
public static void main(String[] args) {
PasswordRandomizer randomizer = new KeyCreator(13);
System.out.println("Password: " + randomizer.createPassword());
System.out.println("Password: " + randomizer.createPassword());
System.out.println("Password: " + randomizer.createPassword());
System.out.println("Password: " + randomizer.createPassword());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment