Create a new Java project in Eclipse named YourPittID_Assignment3 . For example,
ID: 3666814 • Letter: C
Question
Create a new Java project in Eclipse named YourPittID_Assignment3. For example, if your Pitt ID is abc123, your project should be named abc123_Assignment3.
Within your project, create a new package named edu.pitt.is17.assignment3
Do not use numeric or String literals – all numbers or Strings must be assigned to variables or constants! Any fixed factor or fixed String used in a computation should be defined as constant.
Write meaningful comments for every variable and every step in your solutions – make sure to explain what each variable represents. The goal is to make you explicitly reflect about every
Follow the name conventions for naming your classes, variables and constants.
The problem: valid passwords
Computer systems in which you receive or create an account usually asks you to create a safe password imposing a bunch of (sometimes annoying) constraints and requirements, like for example, that has to have certain minimum length and contain numbers and letters. The goal of this homework is to implement a method that checks is a password candidate complies with a series of requirements and constraints. A valid password candidate must:
Start with a letter. It could be lowercase or uppercase.
Contain only letters, numbers or any of the following symbols: '@' , '#', '$', '_', '.' (comma is not a valid character in a password). But not others.
Have at least 1 uppercase letter, and at least 1 lowercase letter.
Have at least one of the following symbols: '@', '#', '$'
Have at least one number (digits: 0-9)
Be at least 10 characters long.
A) You need to implement one method for each of the restrictions listed above, therefore you will have 6 methods. You have to decide the name of the methods, but all of them will have return type boolean and a String parameter to receive the password candidate to be checked. For example, the first method will check if the password starts with a letter and its return type, signature and modifiers would be:
public static boolean startWithLetter(String password)
B) Implement the method
public static boolean checkPassword(String password)
This method uses all other 6 methods implemented in (A) to do the whole check process. An important aspect of this method is that it should output messages for each of the constraints that is violated. For example, calling the method in the following way
checkPassword("3testing")
Will return false (remember that the method return type is boolean) and will show several messages in console:
The password is not valid because:
- It should start with a letter
- It should have at least one uppercase letter
- It should contain at least one of these special character: @ # $ - It should be 10 or more characters long
C) Write a program that asks the user to input a password, checks it (using checkPassword method) and asks again if is not valid. The program should keep asking the user until the user enters a valid formed password (TIP: you can use a do-while loop).
Important Note: you should be able to implement all the methods requested in this assignment using what we have covered during lectures. Other solutions may be more complicated and you should avoid them unless you are motivated to expand your knowledge, and as long as you still follow ALL the instructions presented above. For example, you might see that validation of passwords given the constraints listed before can be done using regular expressions. We will not cover regular expressions, but if you want to learn it and apply them to this assignment is ok, but you still have to define and implement the methods requested.
Explanation / Answer
C)
As previously answered, you should chek all the password characters first. Count your digits and finally check if count is smaller than 2. Here is the referring code.
As previously answered, you should chek all the password characters first. Count your digits and finally check if count is smaller than 2. Here is the referring code.
if (password.length() < 8) { return false; } else { char c; int count = 0; for (int i = 0; i < password.length(); i++) { c = password.charAt(i); if (!Character.isLetterOrDigit(c)) { return false; } else if (Character.isDigit(c)) { count++; } } if (count < 2) { return false; } } return true; } Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.