Question

In: Computer Science

I have attempted to implement some changes to my source code and was unable to succefully...

I have attempted to implement some changes to my source code and was unable to succefully implement there are (two) changes. I am in the learning process so comments help in the learning curve so if possible leave comments.

PLEASE DONT CHANGE ANY OF THE SOURCE CODE - unless needed to implement the changes.

* Change the program so that the user can choose to either check if a password is valid or have the program randomly generate a password that is valid.

* Add the functionality to generate a valid password, display this password and end the program.

SOURCE CODE PROVIDED BELOW

import java.util.Scanner;

public class PasswordChecker {

   public static void main(String[] args) {
      
       Scanner keyboard = new Scanner(System.in);
       String password;
       int uppers = 0;
       int lowers = 0;
       int numbers = 0;
       int special = 0;
       int length = 0;
      
       System.out.println("Enter your password: ");
       System.out.println("Rules: ");
       System.out.println("* 9 to 30 characters in length");
       System.out.println("* contain at least one uppercase letter (A - Z)");
       System.out.println("* contain at least one lowercase letter (a - z)");
       System.out.println("* contain at least one number digit (0 - 9)");
       System.out.println("* contain at least one special character (# @ $ % + = )");
       password = keyboard.nextLine();
      
      
       length = password.length();
       if ((length < 9) || (length > 30)) {
           System.out.println("Please re-enter password, keep in mind more than 9 char and less than 30");
       }
       else
       {
           for(int i = 0; i < length; i++) {
      
               if(Character.isUpperCase(password.charAt(i)))
                   uppers++;
          
               else if (Character.isLowerCase(password.charAt(i)))
                   lowers++;
          
               else if (Character.isDigit(password.charAt(i)))
                   numbers++;
              
               else if (specialCompare(password.charAt(i)))
                   special++;
              
           }  
       }
      
       if((uppers == 0) || (lowers == 0) || (numbers == 0) || (special == 0))
           System.out.println("Password is missing one of the requirements");
       else
           System.out.println("Good password");
   }//End main  
  
   static boolean specialCompare(char a) {
       switch(a) {
       case '@':
       case '#':
       case '$':
       case '%':
       case '+':
       case '=':
           return true;
       }
       return false;
   }//End specialCompare

}//End class

Solutions

Expert Solution

//All the code in bold is the code added by me

import java.util.*;

public class PasswordChecker
{

public static void main (String[]args)
{

Scanner keyboard = new Scanner (System.in);
String password;
int uppers = 0;
int lowers = 0;
int numbers = 0;
int special = 0;
int length = 0;
int choice = 0;

System.out.println ("press 1 to enter your own password.");
System.out.println ("press 2 to get an auto generated password.");
choice = keyboard.nextInt ();
if (choice == 1)
{

System.out.println ("Enter your password: ");
System.out.println ("Rules: ");
System.out.println ("* 9 to 30 characters in length");
System.out.println ("* contain at least one uppercase letter (A - Z)");
System.out.println ("* contain at least one lowercase letter (a - z)");
System.out.println ("* contain at least one number digit (0 - 9)");
System.out.println ("* contain at least one special character (# @ $ % + = )");
password = keyboard.nextLine ();

length = password.length ();
if ((length < 9) || (length > 30)) System.out.println("Please re-enter password, keep in mind more than 9 char and less than 30");
else
{

for (int i = 0; i < length; i++)
{

if (Character.isUpperCase (password.charAt (i))) uppers++;

else if (Character.isLowerCase (password.charAt (i))) lowers++;

else if (Character.isDigit (password.charAt (i))) numbers++;

else if (specialCompare (password.charAt (i))) special++;

}

}

if ((uppers == 0) || (lowers == 0) || (numbers == 0) || (special == 0)) System.out.println ("Password is missing one of the requirements");
else System.out.println ("Good password");

} //End is x==1
else if (choice == 2)
{

//If x==2 we will auto generate pass word

password = "";
int x = 0;
int count = 0;

length = getRandomInRange (9, 30); // here we randomly decide length of our password
for (int i = 0; i < length; i++) // In this loop we generate each and every character of password randomly
{

if (count < 4 && (length - i - 1) == (4 - count))
{

//this if ensures if each of number, special character, lowercase and uppercase is taken atleast once

if (uppers == 0)
{

password += getUpperCase ();
count++;
uppers++;

}
else if (lowers == 0)
{

password += getLowerCase ();
count++;
lowers++;

}
else if (numbers == 0)
{

password += getNumber ();
count++;
numbers++;

}
else
{

password += getSpecialCharacter ();
count++;
special++;

}

continue;

}

x = getRandomInRange (1, 4);

//Below if else ladder decides which of the four available cases to use as our next character in password.
if (x == 1) //for uppercase
{

password += getUpperCase ();
if (uppers == 0) count++;
uppers++;

}
else if (x == 2) //for lowercase
{

password += getLowerCase ();
if (lowers == 0) count++;
lowers++;

}
else if (x == 3) //for number
{

password += getNumber ();
if (numbers == 0) count++;
numbers++;

}
else //for special character
{

password += getSpecialCharacter ();
if (special == 0) count++;
special++;

}

}
System.out.println(password);

} // End if x == 2
else System.out.println (choice + " is an invalid choice. Exiting.....");

} //End Main

public static char getUpperCase () //this function will get a random uppercase character
{

return (char) getRandomInRange (65, 90);

}

public static char getLowerCase () //this function will get a random lowercase character
{

return (char) getRandomInRange (97, 122);

}

public static int getNumber () //this function will get a random number between 0 - 9
{

return getRandomInRange (0, 9);

}

public static char getSpecialCharacter () //this function will get a random special character
{

int x = getRandomInRange (1, 6);
switch (x)
{

case 1:
return '@';
case 2:
return '#';
case 3:
return '$';
case 4:
return '%';
case 5:
return '+';
case 6:
return '=';

}

return '@';

}

public static int getRandomInRange (int min, int max) //this function will generate a random in the given range.
{

Random r = new Random ();
return r.nextInt ((max - min) + 1) + min;

}

static boolean specialCompare (char a)
{
switch (a)
{
case '@':
case '#':
case '$':
case '%':
case '+':
case '=':
   return true;
}
return false;
}               //End specialCompare
}

OUTPUT:-


Related Solutions

Below is my source code for file merging. when i run the code my merged file...
Below is my source code for file merging. when i run the code my merged file is blank and it never shows merging complete prompt. i dont see any errors or why my code would be causing this. i saved both files with male names and female names in the same location my source code is in as a rtf #include #include #include using namespace std; int main() { ifstream inFile1; ifstream inFile2; ofstream outFile1; int mClientNumber, fClientNumber; string mClientName;...
This is my C language code. I have some problems with the linked list. I can't...
This is my C language code. I have some problems with the linked list. I can't store the current. After current = temp, I don't know how to move to the next node. current = current-> next keeps making current into NULL. #include #include #include #include struct node{int data; struct node *next;}; int main() {     struct node *head, *current, *temp, *trash;     srand(time(0));     int randNumber = rand()%51;     if(randNumber != 49)     {         temp = (struct node*)malloc(sizeof(struct node));         current = (struct node*)malloc(sizeof(struct node));...
In Python I have a code: here's my problem, and below it is my code. Below...
In Python I have a code: here's my problem, and below it is my code. Below that is the error I received. Please assist. Complete the swapCaps() function to change all lowercase letters in string to uppercase letters and all uppercase letters to lowercase letters. Anything else remains the same. Examples: swapCaps( 'Hope you are all enjoying October' ) returns 'hOPE YOU ARE ALL ENJOYING oCTOBER' swapCaps( 'i hope my caps lock does not get stuck on' ) returns 'I...
This is the code I have. My problem is my output includes ", 0" at the...
This is the code I have. My problem is my output includes ", 0" at the end and I want to exclude that. // File: main.cpp /*---------- BEGIN - DO NOT EDIT CODE ----------*/ #include <iostream> #include <fstream> #include <sstream> #include <iomanip> using namespace std; using index_t = int; using num_count_t = int; using isConnected_t = bool; using sum_t = int; const int MAX_SIZE = 100; // Global variable to be used to count the recursive calls. int recursiveCount =...
The source code I have is what i'm trying to fix for the assignment at the...
The source code I have is what i'm trying to fix for the assignment at the bottom. Source Code: #include <iostream> #include <cstdlib> #include <ctime> #include <iomanip> using namespace std; const int NUM_ROWS = 10; const int NUM_COLS = 10; // Setting values in a 10 by 10 array of random integers (1 - 100) // Pre: twoDArray has been declared with row and column size of NUM_COLS // Must have constant integer NUM_COLS declared // rowSize must be less...
in java: In my code at have my last two methods that I cannot exactly figure...
in java: In my code at have my last two methods that I cannot exactly figure out how to execute. I have too convert a Roman number to its decimal form for the case 3 switch. The two methods I cannot figure out are the public static int valueOf(int numeral) and public static int convertRomanNumber(int total, int length, String numeral). This is what my code looks like so far: public static void main(String[] args) { // TODO Auto-generated method stub...
The code following is what I have so far. It does not meet my requirements. My...
The code following is what I have so far. It does not meet my requirements. My problem is that while this program runs, it doesn't let the user execute the functions of addBook, isInList or compareLists to add, check, or compare. Please assist in correcting this issue. Thank you! Write a C++ program to implement a singly linked list of books. The book details should include the following: title, author, and ISBN. The program should include the following functions: addBook:...
I'm getting an error with my code on my EvenDemo class. I am supposed to have...
I'm getting an error with my code on my EvenDemo class. I am supposed to have two classes, Event and Event Demo. Below is my code.  What is a better way for me to write this? //******************************************************** // Event Class code //******************************************************** package java1; import java.util.Scanner; public class Event {    public final static double lowerPricePerGuest = 32.00;    public final static double higherPricePerGuest = 35.00;    public final static int cutOffValue = 50;    public boolean largeEvent;    private String...
I have an error on my code. the compiler says 'setLastName' was not declared in this...
I have an error on my code. the compiler says 'setLastName' was not declared in this scope. this is my code : #include <iostream> using std::cout; using std::cin; using std::endl; #include <string> using std::string; class Employee {    public :               Employee(string fname, string lname, int salary)        {            setFirstName(fname);            setLastName(lname);            setMonthlySalary(salary);        }               void setFirstName(string fname)        {       ...
I need implement the operation bellow. Please add comment on some lines to compare with my...
I need implement the operation bellow. Please add comment on some lines to compare with my code. The program will implement the following operations on the Matrix class: 1. Matrix addition 2. Matrix subtraction 3. Matrix multiplication 4. Matrix scalar multiplication 5. Matrix transpose Vector class will implement the following operations: 1. Vector length 2. Vector inner (dot product) 3. Vector angle Vector 3D Class The class Vector3D is derived from the Vector class, and it implements a vector in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT