Question

In: Computer Science

Introduction: One of the advantages of using Loops is to reduce the amount of programming effort...

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:

  1. Create a second string that contains your full id and full name.
  2. The program should select from both the original string and the string created that contains your id and name generated in 1 above.
  3. Size of the password should not exceed 18 letters long
  4. The program should allow the user to specify the number of passwords to be generated
  5. The program should allow the user to run the program as many times as he/she require by prompting the user need to generate a number of passwords again.

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 :

  1. Lab is worth 5% of total mark
  2. Submission of the lab is no later than Thursday 31st October 2020 @ 18:00
  3. No submission allowed after the deadline
  4. Any submission via email or after deadline will not be accepted
  5. This lab is individual work
  6. You are required to submit your soft copy of your java code file via Blackboard
  7. You are required to submit a document showing screenshots of the output
  8. Failing to submit any of the above will cause reduction of 50% of the mark

Solutions

Expert Solution

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:


Related Solutions

In an effort to reduce plastics, a shampoo bottle filling process is using machines to fill...
In an effort to reduce plastics, a shampoo bottle filling process is using machines to fill reusable bottles labeled 200 ml. The amount of shampoo filled in a bottle is normally distributed with the mean 205 ml and standard deviation 2 ml. a. A bottle filled with more than 210 ml of shampoo will overflow. Suppose you need to ensure that no more than 0.15% of the bottles will overflow in the filling process. Assume that you can calibrate the...
Book - Introduction to Programming Using Visual Basic 11th Edition by David I. Schneider Programming Language...
Book - Introduction to Programming Using Visual Basic 11th Edition by David I. Schneider Programming Language - Visual Studio 2017 RESTAURANT MENU Write a program to place an order from the restaurant menu in Table 4.13. Use the form in Fig. 4.70, and write the program so that each group box is invisible and becomes visible only when its corresponding check box is checked. After the button is clicked, the cost of the meal should be calculated. (NOTE: The Checked...
C programming Rewrite the following function using no loops, and only tail call recursion double question5...
C programming Rewrite the following function using no loops, and only tail call recursion double question5 (int in) { int i; int result; for (result = rand(), i = 0; i < in; i += 3) { result /= i; result += rand(); } return result; }
describe the government effort to reduce audit failures
describe the government effort to reduce audit failures
C++ Programming level 1 using step number 2 is a must Guess the Number Introduction In...
C++ Programming level 1 using step number 2 is a must Guess the Number Introduction In this assignment you will create a program that will simulate a number guessing game. Skills: random, while-loop, Boolean flags Algorithm to win The guess the number game has an algorithm to lead to the winning value. The way it works is say you have the set of numbers 1 to 100. You always begin by choosing the half-way point, then a hint will be...
Introduction Introduction to Data Structures programming assignments can be completed either in C++ (preferred) or in...
Introduction Introduction to Data Structures programming assignments can be completed either in C++ (preferred) or in Java. In both cases you cannot use libraries or packages that contain pre-built data structures, other than built-in support for objects, arrays, references, and pointers. Classes in C++ and Java can represent anything in the real world. This assignment is to write a compiler for Z++ programming language. The Z++ Programming Language Your program will test if an expression entered by the application user...
As part of an effort to increase cash and reduce the cash cycle, the connections between...
As part of an effort to increase cash and reduce the cash cycle, the connections between the AP and AR processes are often scrutinized to see if the company is fully billing what it is entitled to bill … to this end, folks sometimes ask about "unbilled" and how long that "unbilled" has remained in that state … is number for "unbilled" best derived from what has been declared as revenue under 606 but not yet billed or as what...
Suppose that, in an effort to reduce the federal deficit, Congress increases the top personal tax...
Suppose that, in an effort to reduce the federal deficit, Congress increases the top personal tax rate on interest and dividends to 38% but retains a 12% tax rate on realized capital gains. The corporate tax rate stays at 40%. Assume capital gains are 50% of equity income. a. Compute the total corporate plus personal taxes paid on each $1 of debt income. (Do not round intermediate calculations. Round your answer to 2 decimal places.) Total tax            $ b. Compute...
in you own word do a case study on plastic bottles, as in effort to reduce...
in you own word do a case study on plastic bottles, as in effort to reduce plastic bottles.
introduction: C PROGRAMMING For this assignment you will write an encoder and a decoder for a...
introduction: C PROGRAMMING For this assignment you will write an encoder and a decoder for a modified "book cipher." A book cipher uses a document or book as the cipher key, and the cipher itself uses numbers that reference the words within the text. For example, one of the Beale ciphers used an edition of The Declaration of Independence as the cipher key. The cipher you will write will use a pair of numbers corresponding to each letter in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT