Implement a boolean method for each rule

Assignment Help JAVA Programming
Reference no: EM131586133

Project - Passwords

Some websites impose certain rules for passwords. Write a method that checks whether a string is a valid password. Suppose the password rules are as follows:

1. A password must have at least eight characters, but no more than 32.
2. A password consists of only letters, digits, and special characters.
3. At least one letter must be upper case and one letter lower case.
4. A password must contain at least two digits.
5. A password must contain at least one special character (@, #, $, or %)

We desire to test our method with a program that prompts the user to enter a password and displays Valid Password if the rules are followed or Invalid Password followed by the reason otherwise.

Complete the class, CheckPassword shown below, that contains a static method, checkPassword, that performs all of these tests and returns an error code. A returned value of 0 indicates that the password is valid. Any other number corresponds to one of the 5 tests shown above.

The best way to handle this is break it down into a series of smaller tests.

For example, implement a boolean method for each rule. Simply have checkPassword sequentially check each rule. If a rule is not satisfied, then the remaining rules are skipped and a value corresponding to that test is returned.

For the purpose of this project, the following method names should be used:

1. hasValidSize
2. hasValidCharacters
3. hasUpperLowerCase
4. hasTwoDigits
5. hasSpecialCharacters

Each should take a String as a parameter.

Checking each rule seems daunting, but the String and Character classes provide all of the functionality necessary. Consider the following:

1. the length method tells you how many characters are in the string

2. the charAt method can extract a single character which may then be compared.

3. If ch is of type char, then the method call Character.isDigit(ch) tells you if it is numeric.

4. toUpper and toLower methods return strings in the desired case

5. The equals method compares one string with another. For example, if a string is compared with the uppercase version of itself, e.g., if s.equals(s.toUpper()) is true, one can deduce that there are no lowercase letters in the string.

The following is the outline for the CheckPassword class:

public class CheckPassword {

/**
* Validates content of a password returning a code value
* @param password
* @return 0 - password is valid
* 1 - wrong size.
* 2 - does not consist of only letters, digits, and
* special characters
* 3 - does not contain at least one upper case and one
* lower case character
* 4 - does not contain at least two digits an invalid
* character
* 5 - does not contain any of @, #, $, or %
*/
public static int checkPassword(String password) {

}

public static int checkPassword(String password) { if (hasValidSize(password) == false)
return 1;
// Add calls to other methods here
return 0;
}

public static boolean hasValidSize(String password) { int numberOfCharacters = password.length();
if (numberOfCharacters > 32 || numberOfCharacters < 8) return false;
return true;
}

// Other methods go here
}

The following can be used to test the CheckPassword class. Modify it to display the appropriate error message.

public class PasswordTester {

public static void main(String[] args) { String[] passwords = {
"abcd", // Wrong size
"abcd_defg", // Has a non-recognized special character "abcdefgh", // All one case
"Abcdefgh1", // Needs a second digit "Abcdefgh12", // Missing a special character

"Abcdefgh%12" // Valid
};

for (int i=0; i < passwords.length; i++) {
int code = CheckPassword.checkPassword(passwords[i]); display(code);
}
}

public static void display(int code) { System.out.println(code);
}
}

Reference no: EM131586133

Questions Cloud

Bond clean and dirty prices today : Assume today is November 1. What will the difference, if any, be between this bond's clean and dirty prices today?
Are any two of the given three events independent : Suppose that we know that P(E1) = 3/8 and that P(E2) = 3/9. How large and how small might P(E1 and E2) and P(E1 or E2) be?
Is there a conscious process you explore : Is there a conscious (or subconscious) process you explore when evaluating your options and how they may impact your desired outcome?
Determining the annual percentage rate : Given that the APR (namely the Annual Percentage Rate. That is, the stated interest agreed on the loan) of the loan is 3.4% per year
Implement a boolean method for each rule : Write a method that checks whether a string is a valid password - implement a boolean method for each rule
Does the drug free workplace act apply here : The area is in the back room and only that person has access to the room.Do you make the person remove them? Why or why not?
What are some of the key factors that should be considered : What are some of the key factors that should be considered when assigning tasks to employees? What are some ways to ensure project success before project begin?
What are the fixed costs of hamburger production : What are the fixed costs of hamburger production? What is the variable cost per hamburger?
Discuss the item for sale from a bag of carp : The website Noot!.com sells one aquatic thing (like newts) each day. Every so often, the item for sale is a Bag of Carp, recognizable by its iconic chartreuse.

Reviews

Write a Review

JAVA Programming Questions & Answers

  Recursive factorial program

Write a class Array that encapsulates an array and provides bounds-checked access. Create a recursive factorial program that prompts the user for an integer N and writes out a series of equations representing the calculation of N!.

  Hunt the wumpus game

Reprot on Hunt the Wumpus Game has Source Code listing, screen captures and UML design here and also, may include Javadoc source here.

  Create a gui interface

Create GUI Interface in java programing with these function: Sort by last name and print all employees info, Sort by job title and print all employees info, Sort by weekly salary and print all employees info, search by job title and print that emp..

  Plot pois on a graph

Write a JAVA program that would get the locations of all the POIs from the file and plot them on a map.

  Write a university grading system in java

University grading system maintains number of tables to store, retrieve and manipulate student marks. Write a JAVA program that would simulate a number of cars.

  Wolves and sheep: design a game

This project is designed a game in java. you choose whether you'd like to write a wolf or a sheep agent. Then, you are assigned to either a "sheep" or a "wolf" team.

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

  Determine the day of the week for new year''s day

This assignment contains a java project. Project evaluates the day of the week for New Year's Day.

  Write a java windowed application

Write a Java windowed application to do online quiz on general knowledge and the application also displays the quiz result.

  Input pairs of natural numbers

Java program to input pairs of natural numbers.

  Create classes implement java interface

Interface that contains a generic type. Create two classes that implement this interface.

  Java class, array, link list , generic class

These 14 questions covers java class, Array, link list , generic class.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd