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

  Design a class that you can use to track the books

Define a class PileOfBooks that implements the interface described in question one. use a resizeable array oin your implementation. then write unit test woth adequate test cases for your implementation of class PiloOfBooks.

  Design a java program that simulates a slot machine

Instead of displaying images, the program will randomly select a word from the following list: Cherries, Oranges, Plums, Bars, and Bells. The program will select and display a word from this list three times.

  How to place a cursor at the end of line instead

How to place a cursor at the end of line instead of the beginning of the line? How do you put an exception so that input of a number only falls with in a specific range?

  Create a class named student that has three member variables

Create a class named Student that has three member variables

  Classes using set and get methods

Create a java program that contains two classes using set and get methods. I need the program to return the area and perimeter of a rectangle. I wrote a program and he returned to me saying I used the wrong constructors and didn't create a the sec..

  Create your own short and simple jdbc java code

Using the Schema you developed in Homework 1 for your e-Commerce project, design and implement a Virtual Private Database that limits database access. Your design should be based on two of your columns and two of your tables. (Hint: where col1 = v..

  One acre of land is equivalent to

One acre of land is equivalent to 43,560 square feet. Write a program that calculates the number of square feet in a tract of land with 3.5acres.

  Prepare a polynomial-time algorithm

Write a polynomial-time algorithm that, given an initial configuration (P_1, P_2, ..., P_k), decides if it is a winning configuration - Prepare a polynomial-time algorithm that decides if it is a winning configuration.

  Implementation activities of software development

Analysis, design, and implementation activities of software development

  Describe the swing applet life cycle

Describe the swing applet life cycle. Create a swing applet that displays the date and time in a JTextField with the JLabel "Today is" when the user clicks a JButton

  Write the syntax (one line) to declare an int array

Write the syntax using a loop to print out every odd value from the array above

  Produce a design document

You will design and deliver a website foes fictional client part-1 Website Design Document and produce a design document

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