In: Computer Science
Introduction:
One of the advantages of using Loops is to reduce the amount of programming effort especially when repeating the code is required. To demonstrate this advantages, the code below shows the PasswordGeneration class. The purpose of this program is to generate an ad hoc 12 letters long password upon a selection of a character from a string that contains a pool of letters.
/*simple Java program that generate a 12 characters long passwords * author : Dr. Modafar Ati * Version 1.0 */ public class PasswordGeneration { public static void main(String[] args) { String password; //create a pool of letters String letters = new String("&*()789FGHIJKabcdefghijklmLMNO^|{]}PQRSTU!@#$%VWXYZ123456nopqrstuvwxyzABCDE"); //select 12 random characters from the above pool char c1 = letters.charAt((int)(Math.random()*letters.length())); char c2 = letters.charAt((int)(Math.random()*letters.length())); char c3 = letters.charAt((int)(Math.random()*letters.length())); char c4 = letters.charAt((int)(Math.random()*letters.length())); char c5 = letters.charAt((int)(Math.random()*letters.length())); char c6 = letters.charAt((int)(Math.random()*letters.length())); char c7 = letters.charAt((int)(Math.random()*letters.length())); char c8 = letters.charAt((int)(Math.random()*letters.length())); char c9 = letters.charAt((int)(Math.random()*letters.length())); char c10 = letters.charAt((int)(Math.random()*letters.length())); char c11 = letters.charAt((int)(Math.random()*letters.length())); char c12 = letters.charAt((int)(Math.random()*letters.length()));
// Combine all characters into a string that represent the generated password password = Character.toString(c1) +Character.toString(c2) + Character.toString(c3); password += Character.toString(c4) +Character.toString(c5) + Character.toString(c6); password += Character.toString(c7) +Character.toString(c8) + Character.toString(c9); password += Character.toString(c10) +Character.toString(c11) + Character.toString(c12);
//present the user with the generated password System.out.println("Password generetad is : " +password); } } |
Now upon implementing Loops strategy, the number of lines of the previous program will be reduced significantly. Version 2 of the above code can be written after adding more functionality to it is shown below. The new code has extra flexibility added to it by given the user to specify the size of the password required.
/*simple Java program that generate a flexible size passwords * author : Dr. Modafar Ati * Version 2.0 */ import java.util.Scanner; public class PasswordGeneration { public static void main(String[] args) { Scanner input = new Scanner(System.in); String password=""; char c; int size = 0; String letters = new String("&*()789FGHIJKabcdefghijklmLMNO^|{]}PQRSTU!@#$%VWXYZ123456nopqrstuvwxyzABCDE"); System.out.print("What is the length of password would you like to generate : "); int lengthOfPasswords =input.nextInt();
while(size < lengthOfPasswords) { //select a random character from the list c = letters.charAt((int)(Math.random()*letters.length()));
//now append the letter to the pass String password += Character.toString(c);
size++; } System.out.println(password); input.close(); } } |
Task:
You are required to modify the second version of the program to satisfy the following conditions:
PS: Below is a sample output of the program
How many passwords would like to generate : 5 What is the length of password would you like to generate : 15 The following 5 are now generated ________________________________________________________________ tE3gEZMSEHtA6Ku n%iPfc%JGZaxGk6 Z^Rhdz5f7OIVx2F jh3dldFfFO9W)kS Kpjn$]9Pmpm(bX3 Would you like to generate another set of passwords (Y/N ) : y How many passwords would like to generate : 10 What is the length of password would you like to generate : 10 The following 10 are now generated ________________________________________________________________ &^upcN&DvY *x^D2(tX}$ Ie9HjCCIkD oOr*mT{kpC 4H7cz$m2U$ (]BBIRykgW *V8vTh2DN& BMxw%W8*|v dyLivkl)U1 hP4eFeI5FM Would you like to generate another set of passwords (Y/N ) : N Thank you |
Notes :
Here is the working code. Can be tweaked if required. I've added comments wherever necessary
import java.util.Scanner;
public class PasswordGeneration {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String password;
int size;
char c;
String choice;
String letters = new String("&*()789FGHIJKabcdefghijklmLMNO^|{]}PQRSTU!@#$%VWXYZ123456nopqrstuvwxyzABCDE");
//New String, Replace with student mail and student name
String nameAndEmail = "[email protected]";
//do-while loop to execute whole program as long as user wants
do {
System.out.print("How many passwords would like to generate : ");
int numberOfPassword = input.nextInt();
System.out.print("What is the length of password would you like to generate : ");
int lengthOfPasswords;
//take valid input from user
do {
lengthOfPasswords = input.nextInt();
if(lengthOfPasswords>18) {
System.out.println("Lenth of password cannot be greater than 18 characters.");
System.out.print("What is the length of password would you like to generate : ");
}
}while(lengthOfPasswords>18);
System.out.println("The following " + numberOfPassword + " are now generated \n");
System.out.println("-----------------------------------------------------------------");
//to generate multiple passwords as per users requirement
for(int i=0; i<numberOfPassword; i++) {
password="";
size = 0;
while(size < lengthOfPasswords) {
//When value of size is even, select a character from 'letters' String, otherwise select from 'nameAndEmail' String
if(size%2==0) {
//select a random character from the String letters
c = letters.charAt((int)(Math.random()*letters.length()));
}else {
//select a random character from the String nameAndEmail
c = nameAndEmail.charAt((int)(Math.random()*nameAndEmail.length()));
}
//now append the letter to the pass String
password += Character.toString(c);
size++;
}
System.out.println(password);
}
System.out.print("Would you like to generate another set of passwords (Y/N ) : ");
input.nextLine(); //dummy input to empty the input buffer
choice = input.nextLine();
}while(choice.equalsIgnoreCase("Y"));
//to format output spacing
System.out.printf("%20s", "Thank You");
input.close();
}
}
Output: